$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; } ?>