4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\VarDumper\Caster;
15 * Helper for filtering out properties in casters.
17 * @author Nicolas Grekas <p@tchwork.com>
21 const EXCLUDE_VERBOSE = 1;
22 const EXCLUDE_VIRTUAL = 2;
23 const EXCLUDE_DYNAMIC = 4;
24 const EXCLUDE_PUBLIC = 8;
25 const EXCLUDE_PROTECTED = 16;
26 const EXCLUDE_PRIVATE = 32;
27 const EXCLUDE_NULL = 64;
28 const EXCLUDE_EMPTY = 128;
29 const EXCLUDE_NOT_IMPORTANT = 256;
30 const EXCLUDE_STRICT = 512;
32 const PREFIX_VIRTUAL = "\0~\0";
33 const PREFIX_DYNAMIC = "\0+\0";
34 const PREFIX_PROTECTED = "\0*\0";
37 * Casts objects to arrays and adds the dynamic property prefix.
39 * @param object $obj The object to cast
40 * @param \ReflectionClass $reflector The class reflector to use for inspecting the object definition
42 * @return array The array-cast of the object, with prefixed dynamic properties
44 public static function castObject($obj, \ReflectionClass $reflector)
46 if ($reflector->hasMethod('__debugInfo')) {
47 $a = $obj->__debugInfo();
48 } elseif ($obj instanceof \Closure) {
56 foreach ($p as $i => $k) {
57 if (isset($k[0]) && "\0" !== $k[0] && !$reflector->hasProperty($k)) {
58 $p[$i] = self::PREFIX_DYNAMIC.$k;
59 } elseif (isset($k[16]) && "\0" === $k[16] && 0 === strpos($k, "\0class@anonymous\0")) {
60 $p[$i] = "\0".$reflector->getParentClass().'@anonymous'.strrchr($k, "\0");
63 $a = array_combine($p, $a);
70 * Filters out the specified properties.
72 * By default, a single match in the $filter bit field filters properties out, following an "or" logic.
73 * When EXCLUDE_STRICT is set, an "and" logic is applied: all bits must match for a property to be removed.
75 * @param array $a The array containing the properties to filter
76 * @param int $filter A bit field of Caster::EXCLUDE_* constants specifying which properties to filter out
77 * @param string[] $listedProperties List of properties to exclude when Caster::EXCLUDE_VERBOSE is set, and to preserve when Caster::EXCLUDE_NOT_IMPORTANT is set
79 * @return array The filtered array
81 public static function filter(array $a, $filter, array $listedProperties = array())
83 foreach ($a as $k => $v) {
84 $type = self::EXCLUDE_STRICT & $filter;
87 $type |= self::EXCLUDE_NULL & $filter;
90 $type |= self::EXCLUDE_EMPTY & $filter;
92 if ((self::EXCLUDE_NOT_IMPORTANT & $filter) && !in_array($k, $listedProperties, true)) {
93 $type |= self::EXCLUDE_NOT_IMPORTANT;
95 if ((self::EXCLUDE_VERBOSE & $filter) && in_array($k, $listedProperties, true)) {
96 $type |= self::EXCLUDE_VERBOSE;
99 if (!isset($k[1]) || "\0" !== $k[0]) {
100 $type |= self::EXCLUDE_PUBLIC & $filter;
101 } elseif ('~' === $k[1]) {
102 $type |= self::EXCLUDE_VIRTUAL & $filter;
103 } elseif ('+' === $k[1]) {
104 $type |= self::EXCLUDE_DYNAMIC & $filter;
105 } elseif ('*' === $k[1]) {
106 $type |= self::EXCLUDE_PROTECTED & $filter;
108 $type |= self::EXCLUDE_PRIVATE & $filter;
111 if ((self::EXCLUDE_STRICT & $filter) ? $type === $filter : $type) {