| Server IP : 54.94.228.101 / Your IP : 172.28.20.13 Web Server : Apache System : Linux ip-172-28-29-189 6.5.0-1014-aws #14~22.04.1-Ubuntu SMP Thu Feb 15 15:27:06 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.2.34-43+ubuntu22.04.1+deb.sury.org+1 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/vinumday2_0/vendor/anahkiasen/underscore-php/src/ |
Upload File : |
<?php
namespace Underscore;
use Underscore\Methods\ArraysMethods;
/**
* Parse from various formats to various formats.
*/
class Parse
{
////////////////////////////////////////////////////////////////////
/////////////////////////////// JSON ///////////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Converts data from JSON.
*
* @param string $data The data to parse
*
* @return mixed
*/
public static function fromJSON($data)
{
return json_decode($data, true);
}
/**
* Converts data to JSON.
*
* @param array|object $data The data to convert
*
* @return string Converted data
*/
public static function toJSON($data)
{
return json_encode($data);
}
////////////////////////////////////////////////////////////////////
//////////////////////////////// XML ///////////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Converts data from XML.
*
* @param string $xml The data to parse
*
* @return array
*/
public static function fromXML($xml)
{
$xml = simplexml_load_string($xml);
$xml = json_encode($xml);
$xml = json_decode($xml, true);
return $xml;
}
////////////////////////////////////////////////////////////////////
//////////////////////////////// CSV ///////////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Converts data from CSV.
*
* @param string $data The data to parse
* @param bool $hasHeaders Whether the CSV has headers
*
* @return mixed
*/
public static function fromCSV($data, $hasHeaders = false)
{
$data = trim($data);
// Explodes rows
$data = static::explodeWith($data, [PHP_EOL, "\r", "\n"]);
$data = array_map(function ($row) {
return Parse::explodeWith($row, [';', "\t", ',']);
}, $data);
// Get headers
$headers = $hasHeaders ? $data[0] : array_keys($data[0]);
if ($hasHeaders) {
array_shift($data);
}
// Parse the columns in each row
$array = [];
foreach ($data as $row => $columns) {
foreach ($columns as $columnNumber => $column) {
$array[$row][$headers[$columnNumber]] = $column;
}
}
return $array;
}
/**
* Converts data to CSV.
*
* @param mixed $data The data to convert
* @param string $delimiter
* @param bool $exportHeaders
*
* @return string Converted data
*/
public static function toCSV($data, $delimiter = ';', $exportHeaders = false)
{
$csv = [];
// Convert objects to arrays
if (is_object($data)) {
$data = (array) $data;
}
// Don't convert if it's not an array
if (!is_array($data)) {
return $data;
}
// Fetch headers if requested
if ($exportHeaders) {
$headers = array_keys(ArraysMethods::first($data));
$csv[] = implode($delimiter, $headers);
}
// Quote values and create row
foreach ($data as $header => $row) {
// If single column
if (!is_array($row)) {
$csv[] = '"'.$header.'"'.$delimiter.'"'.$row.'"';
continue;
}
// Else add values
foreach ($row as $key => $value) {
$row[$key] = '"'.stripslashes($value).'"';
}
$csv[] = implode($delimiter, $row);
}
return implode(PHP_EOL, $csv);
}
////////////////////////////////////////////////////////////////////
///////////////////////// TYPES SWITCHERS //////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Converts data to an array.
*
* @param string|object $data
*
* @return array
*/
public static function toArray($data)
{
// Look for common array conversion patterns in objects
if (is_object($data) and method_exists($data, 'toArray')) {
$data = $data->toArray();
}
return (array) $data;
}
/**
* Converts data to a string.
*
* @param array|object $data
*
* @return string
*/
public static function toString($data)
{
// Avoid Array to Strings conversion exception
if (is_array($data)) {
return static::toJSON($data);
}
return (string) $data;
}
/**
* Converts data to an integer.
*
* @param array|string|object $data
*
* @return int
*/
public static function toInteger($data)
{
// Returns size of arrays
if (is_array($data)) {
return count($data);
}
// Returns size of strings
if (is_string($data) and !preg_match('/[0-9. ,]+/', $data)) {
return strlen($data);
}
return (int) $data;
}
/**
* Converts data to a boolean.
*
* @param array|sring|object $data
*
* @return bool
*/
public static function toBoolean($data)
{
return (bool) $data;
}
/**
* Converts data to an object.
*
* @param array|string $data
*
* @return object
*/
public static function toObject($data)
{
return (object) $data;
}
////////////////////////////////////////////////////////////////////
///////////////////////////// HELPERS //////////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Tries to explode a string with an array of delimiters.
*
* @param string $string The string
* @param array $delimiters An array of delimiters
*
* @return array
*/
public static function explodeWith($string, array $delimiters)
{
$array = $string;
foreach ($delimiters as $delimiter) {
$array = explode($delimiter, $string);
if (count($array) === 1) {
continue;
} else {
return $array;
}
}
return $array;
}
}