Changeset View
Changeset View
Standalone View
Standalone View
src/utils/utils.php
Show First 20 Lines • Show All 1,470 Lines • ▼ Show 20 Lines | function phutil_var_export($var) { | ||||
// integer key. | // integer key. | ||||
// | // | ||||
if (is_array($var)) { | if (is_array($var)) { | ||||
if (count($var) === 0) { | if (count($var) === 0) { | ||||
return 'array()'; | return 'array()'; | ||||
} | } | ||||
// Don't show keys for non-associative arrays. | // Don't show keys for non-associative arrays. | ||||
$show_keys = (array_keys($var) !== range(0, count($var) - 1)); | $show_keys = !phutil_is_natural_list($var); | ||||
$output = array(); | $output = array(); | ||||
$output[] = 'array('; | $output[] = 'array('; | ||||
foreach ($var as $key => $value) { | foreach ($var as $key => $value) { | ||||
// Adjust the indentation of the value. | // Adjust the indentation of the value. | ||||
$value = str_replace("\n", "\n ", phutil_var_export($value)); | $value = str_replace("\n", "\n ", phutil_var_export($value)); | ||||
$output[] = ' '. | $output[] = ' '. | ||||
▲ Show 20 Lines • Show All 264 Lines • ▼ Show 20 Lines | if (!is_string($string)) { | ||||
get_class($value))); | get_class($value))); | ||||
} | } | ||||
return $string; | return $string; | ||||
} | } | ||||
return (string)$value; | return (string)$value; | ||||
} | } | ||||
/** | |||||
* Return a short, human-readable description of an object's type. | |||||
* | |||||
* This is mostly useful for raising errors like "expected x() to return a Y, | |||||
* but it returned a Z". | |||||
* | |||||
* This is similar to "get_type()", but describes objects and arrays in more | |||||
* detail. | |||||
amckinley: Unfinished thought. | |||||
* | |||||
* @param wild Anything. | |||||
* @return string Human-readable description of the value's type. | |||||
*/ | |||||
function phutil_describe_type($value) { | |||||
return PhutilTypeSpec::getTypeOf($value); | |||||
} | |||||
/** | |||||
* Test if a list has the natural numbers (1, 2, 3, and so on) as keys, in | |||||
* order. | |||||
* | |||||
* @return bool True if the list is a natural list. | |||||
*/ | |||||
function phutil_is_natural_list(array $list) { | |||||
$expect = 0; | |||||
foreach ($list as $key => $item) { | |||||
if ($key !== $expect) { | |||||
return false; | |||||
} | |||||
$expect++; | |||||
} | |||||
return true; | |||||
} |
Unfinished thought.