152 lines
2.8 KiB
PHP
152 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace core;
|
|
|
|
/**
|
|
* convert nearly anything to a string (not protected for html, use html_special_chars)
|
|
*
|
|
* @param \mixed $var Variable to convert
|
|
*
|
|
* @return \string string conversion result
|
|
*/
|
|
function display(mixed $var) : string
|
|
{
|
|
if (\is_string($var))
|
|
{
|
|
return $var;
|
|
}
|
|
if (\is_bool($var))
|
|
{
|
|
return ($var ? 'True' : 'False');
|
|
}
|
|
if (\is_int($var) || \is_float($var))
|
|
{
|
|
return (string) $var;
|
|
}
|
|
if (\is_null($var))
|
|
{
|
|
return 'null';
|
|
}
|
|
if (\is_resource($var))
|
|
{
|
|
return \get_resource_type($var);
|
|
}
|
|
if (\is_array($var))
|
|
{
|
|
$ret = 'array (';
|
|
foreach ($var as $key => $el)
|
|
{
|
|
$ret .= ' ' . \core\display($key) . ' => ' . \core\display($el);
|
|
}
|
|
return $ret . ' )';
|
|
}
|
|
if (\is_object($var))
|
|
{
|
|
if (!\method_exists($var, '__toString'))
|
|
{
|
|
return \get_class($var) . ' : ' . \core\display(\core\table($var, 3)); // NOTE: arbitrary hard-coded recursion value
|
|
}
|
|
else
|
|
{
|
|
return $var->__toString();
|
|
}
|
|
}
|
|
if (\is_callable($var))
|
|
{
|
|
if ($var instanceOf \Closure)
|
|
{
|
|
return 'a closure';
|
|
}
|
|
return 'unknown callable';
|
|
}
|
|
if (\is_iterable($var))
|
|
{
|
|
return 'iterable';
|
|
}
|
|
return 'cannot convert the variable that should be here to a string';
|
|
}
|
|
|
|
/**
|
|
* substitution helper
|
|
*
|
|
* substitute {element} with element for each element in token
|
|
* Not safe to display without sanitization on a webpage
|
|
*
|
|
* @param \string $content
|
|
*
|
|
* @param \array $tokens
|
|
*
|
|
* @return \string
|
|
*/
|
|
function substitute(string $content, array $tokens) : string
|
|
{
|
|
$parts = \preg_split('/({(?:\\}|[^\\}])+})/Um', $content, -1, \PREG_SPLIT_DELIM_CAPTURE);
|
|
|
|
if (\count($tokens) > 0)
|
|
{
|
|
foreach ($tokens as $name => $value)
|
|
{
|
|
if (\in_array('{' . $name . '}', $parts))
|
|
{
|
|
foreach (\array_keys($parts, '{' . $name . '}') as $key)
|
|
{
|
|
$parts[$key] = \core\display($value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return \implode($parts);
|
|
}
|
|
|
|
/**
|
|
* convert any object into a table
|
|
*
|
|
* @param \object $object Object to convert
|
|
*
|
|
* @param \int $depth Depth of the recursion if there is an object. -1 for infinite, 0 for no recursion.
|
|
* Default to 0.
|
|
*
|
|
* @return \array
|
|
*/
|
|
function table(object $object, int $depth = 0) : array
|
|
{
|
|
if (\in_array(\core\Base::class, \class_uses($object)))
|
|
{
|
|
return $object->table();
|
|
}
|
|
|
|
$attributes = [];
|
|
|
|
if ($depth < -1)
|
|
{
|
|
throw new \Exception('Depth cannot be below -1');
|
|
}
|
|
|
|
foreach (\array_keys(\get_class_vars(\get_class($object))) as $attribute)
|
|
{
|
|
if (\is_object($object->$attribute))
|
|
{
|
|
if ($depth === 0)
|
|
{
|
|
$attributes[$attribute] = $object->$attribute;
|
|
}
|
|
else if ($depth > 0)
|
|
{
|
|
$attributes[$attribute] = \core\table($object->$attribute, $depth - 1);
|
|
}
|
|
else // depth === -1
|
|
{
|
|
$attributes[$attribute] = \core\table($object->$attribute, $depth);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$attributes[$attribute] = $object->$attribute;
|
|
}
|
|
}
|
|
|
|
return $attributes;
|
|
}
|
|
|
|
?>
|