Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\VarDumper\Caster; Chris@0: Chris@0: use Symfony\Component\VarDumper\Cloner\Stub; Chris@0: Chris@0: /** Chris@0: * Helper for filtering out properties in casters. Chris@0: * Chris@0: * @author Nicolas Grekas
Chris@0: * Chris@0: * @final Chris@0: */ Chris@0: class Caster Chris@0: { Chris@0: const EXCLUDE_VERBOSE = 1; Chris@0: const EXCLUDE_VIRTUAL = 2; Chris@0: const EXCLUDE_DYNAMIC = 4; Chris@0: const EXCLUDE_PUBLIC = 8; Chris@0: const EXCLUDE_PROTECTED = 16; Chris@0: const EXCLUDE_PRIVATE = 32; Chris@0: const EXCLUDE_NULL = 64; Chris@0: const EXCLUDE_EMPTY = 128; Chris@0: const EXCLUDE_NOT_IMPORTANT = 256; Chris@0: const EXCLUDE_STRICT = 512; Chris@0: Chris@0: const PREFIX_VIRTUAL = "\0~\0"; Chris@0: const PREFIX_DYNAMIC = "\0+\0"; Chris@0: const PREFIX_PROTECTED = "\0*\0"; Chris@0: Chris@0: /** Chris@0: * Casts objects to arrays and adds the dynamic property prefix. Chris@0: * Chris@0: * @param object $obj The object to cast Chris@0: * @param string $class The class of the object Chris@0: * @param bool $hasDebugInfo Whether the __debugInfo method exists on $obj or not Chris@0: * Chris@0: * @return array The array-cast of the object, with prefixed dynamic properties Chris@0: */ Chris@0: public static function castObject($obj, $class, $hasDebugInfo = false) Chris@0: { Chris@0: if ($class instanceof \ReflectionClass) { Chris@17: @trigger_error(sprintf('Passing a ReflectionClass to "%s()" is deprecated since Symfony 3.3 and will be unsupported in 4.0. Pass the class name as string instead.', __METHOD__), E_USER_DEPRECATED); Chris@0: $hasDebugInfo = $class->hasMethod('__debugInfo'); Chris@0: $class = $class->name; Chris@0: } Chris@0: if ($hasDebugInfo) { Chris@0: $a = $obj->__debugInfo(); Chris@0: } elseif ($obj instanceof \Closure) { Chris@17: $a = []; Chris@0: } else { Chris@0: $a = (array) $obj; Chris@0: } Chris@0: if ($obj instanceof \__PHP_Incomplete_Class) { Chris@0: return $a; Chris@0: } Chris@0: Chris@0: if ($a) { Chris@17: static $publicProperties = []; Chris@0: Chris@0: $i = 0; Chris@17: $prefixedKeys = []; Chris@0: foreach ($a as $k => $v) { Chris@0: if (isset($k[0]) ? "\0" !== $k[0] : \PHP_VERSION_ID >= 70200) { Chris@0: if (!isset($publicProperties[$class])) { Chris@0: foreach (get_class_vars($class) as $prop => $v) { Chris@0: $publicProperties[$class][$prop] = true; Chris@0: } Chris@0: } Chris@0: if (!isset($publicProperties[$class][$k])) { Chris@0: $prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k; Chris@0: } Chris@0: } elseif (isset($k[16]) && "\0" === $k[16] && 0 === strpos($k, "\0class@anonymous\0")) { Chris@0: $prefixedKeys[$i] = "\0".get_parent_class($class).'@anonymous'.strrchr($k, "\0"); Chris@0: } Chris@0: ++$i; Chris@0: } Chris@0: if ($prefixedKeys) { Chris@0: $keys = array_keys($a); Chris@0: foreach ($prefixedKeys as $i => $k) { Chris@0: $keys[$i] = $k; Chris@0: } Chris@0: $a = array_combine($keys, $a); Chris@0: } Chris@0: } Chris@0: Chris@0: return $a; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Filters out the specified properties. Chris@0: * Chris@0: * By default, a single match in the $filter bit field filters properties out, following an "or" logic. Chris@0: * When EXCLUDE_STRICT is set, an "and" logic is applied: all bits must match for a property to be removed. Chris@0: * Chris@0: * @param array $a The array containing the properties to filter Chris@0: * @param int $filter A bit field of Caster::EXCLUDE_* constants specifying which properties to filter out Chris@0: * @param string[] $listedProperties List of properties to exclude when Caster::EXCLUDE_VERBOSE is set, and to preserve when Caster::EXCLUDE_NOT_IMPORTANT is set Chris@0: * @param int &$count Set to the number of removed properties Chris@0: * Chris@0: * @return array The filtered array Chris@0: */ Chris@17: public static function filter(array $a, $filter, array $listedProperties = [], &$count = 0) Chris@0: { Chris@0: $count = 0; Chris@0: Chris@0: foreach ($a as $k => $v) { Chris@0: $type = self::EXCLUDE_STRICT & $filter; Chris@0: Chris@0: if (null === $v) { Chris@0: $type |= self::EXCLUDE_NULL & $filter; Chris@12: $type |= self::EXCLUDE_EMPTY & $filter; Chris@17: } elseif (false === $v || '' === $v || '0' === $v || 0 === $v || 0.0 === $v || [] === $v) { Chris@0: $type |= self::EXCLUDE_EMPTY & $filter; Chris@0: } Chris@17: if ((self::EXCLUDE_NOT_IMPORTANT & $filter) && !\in_array($k, $listedProperties, true)) { Chris@0: $type |= self::EXCLUDE_NOT_IMPORTANT; Chris@0: } Chris@17: if ((self::EXCLUDE_VERBOSE & $filter) && \in_array($k, $listedProperties, true)) { Chris@0: $type |= self::EXCLUDE_VERBOSE; Chris@0: } Chris@0: Chris@0: if (!isset($k[1]) || "\0" !== $k[0]) { Chris@0: $type |= self::EXCLUDE_PUBLIC & $filter; Chris@0: } elseif ('~' === $k[1]) { Chris@0: $type |= self::EXCLUDE_VIRTUAL & $filter; Chris@0: } elseif ('+' === $k[1]) { Chris@0: $type |= self::EXCLUDE_DYNAMIC & $filter; Chris@0: } elseif ('*' === $k[1]) { Chris@0: $type |= self::EXCLUDE_PROTECTED & $filter; Chris@0: } else { Chris@0: $type |= self::EXCLUDE_PRIVATE & $filter; Chris@0: } Chris@0: Chris@0: if ((self::EXCLUDE_STRICT & $filter) ? $type === $filter : $type) { Chris@0: unset($a[$k]); Chris@0: ++$count; Chris@0: } Chris@0: } Chris@0: Chris@0: return $a; Chris@0: } Chris@0: Chris@0: public static function castPhpIncompleteClass(\__PHP_Incomplete_Class $c, array $a, Stub $stub, $isNested) Chris@0: { Chris@0: if (isset($a['__PHP_Incomplete_Class_Name'])) { Chris@0: $stub->class .= '('.$a['__PHP_Incomplete_Class_Name'].')'; Chris@0: unset($a['__PHP_Incomplete_Class_Name']); Chris@0: } Chris@0: Chris@0: return $a; Chris@0: } Chris@0: }