1,
* 'name' => 'Smith',
* 'date' => '2014/01/22',
* 'amount' => '4,323.23',
* 'relations' => ['spouse', 'children']
* ];
* foreach ($data as $k=>$v) {
* echo "$k: " . Enum::getType($v) . "
";
* }
* ~~~
*
* @param string $var the variable to be parsed
*
* @return string
*/
public static function getType( $var )
{
if (is_array($var)) {
return static::TYPE_ARRAY;
} elseif (is_object($var)) {
return "object";
//return $var::className();
} elseif (is_resource($var)) {
return 'resource';
} elseif (is_null($var)) {
return static::TYPE_NULL;
} elseif (is_bool($var)) {
return static::TYPE_BOOL;
} elseif (is_float($var) || (is_numeric(str_replace(',', '', $var)) && strpos($var, '.') > 0 &&
is_float((float)str_replace(',', '', $var)))
) {
return static::TYPE_FLOAT;
} elseif (is_int($var) || (is_numeric($var) && is_int((int)$var))) {
return static::TYPE_INT;
} elseif (is_scalar($var) && strtotime($var) !== false) {
return static::TYPE_DATETIME;
} elseif (is_scalar($var)) {
return static::TYPE_STRING;
}
return 'unknown';
}
/**
* Check if a variable is empty or not set.
*
* Example:
*
* ~~~
* if (Enum::isEmpty([]) {
* echo 'Not empty';
* }
* ~~~
*
* @param mixed $var variable to perform the check
*
* @return boolean
*/
public static function isEmpty($var)
{
return !isset($var) ? true : (is_array($var) ? empty($var) : ($var === null || $var === ''));
}
/**
* Format and convert "bytes" to its optimal higher metric unit.
*
* Example:
*
* ~~~
* echo Enum::formatBytes(120.32); // returns: 1.17 KB
* echo Enum::formatBytes(28434322.25); // returns: 27.12 MB
* echo Enum::formatBytes(17328347842.25, 3); // returns: 16.138 GB
* ~~~
*
* @param double $bytes number of bytes
* @param integer $precision the number of decimal places to round off
*
* @return string
*/
public static function formatBytes($bytes, $precision = 2)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
}