Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\VarDumper\Caster;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\VarDumper\Cloner\Stub;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Helper for filtering out properties in casters.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @author Nicolas Grekas <p@tchwork.com>
|
Chris@0
|
20 *
|
Chris@0
|
21 * @final
|
Chris@0
|
22 */
|
Chris@0
|
23 class Caster
|
Chris@0
|
24 {
|
Chris@0
|
25 const EXCLUDE_VERBOSE = 1;
|
Chris@0
|
26 const EXCLUDE_VIRTUAL = 2;
|
Chris@0
|
27 const EXCLUDE_DYNAMIC = 4;
|
Chris@0
|
28 const EXCLUDE_PUBLIC = 8;
|
Chris@0
|
29 const EXCLUDE_PROTECTED = 16;
|
Chris@0
|
30 const EXCLUDE_PRIVATE = 32;
|
Chris@0
|
31 const EXCLUDE_NULL = 64;
|
Chris@0
|
32 const EXCLUDE_EMPTY = 128;
|
Chris@0
|
33 const EXCLUDE_NOT_IMPORTANT = 256;
|
Chris@0
|
34 const EXCLUDE_STRICT = 512;
|
Chris@0
|
35
|
Chris@0
|
36 const PREFIX_VIRTUAL = "\0~\0";
|
Chris@0
|
37 const PREFIX_DYNAMIC = "\0+\0";
|
Chris@0
|
38 const PREFIX_PROTECTED = "\0*\0";
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Casts objects to arrays and adds the dynamic property prefix.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @param object $obj The object to cast
|
Chris@0
|
44 * @param string $class The class of the object
|
Chris@0
|
45 * @param bool $hasDebugInfo Whether the __debugInfo method exists on $obj or not
|
Chris@0
|
46 *
|
Chris@0
|
47 * @return array The array-cast of the object, with prefixed dynamic properties
|
Chris@0
|
48 */
|
Chris@0
|
49 public static function castObject($obj, $class, $hasDebugInfo = false)
|
Chris@0
|
50 {
|
Chris@0
|
51 if ($class instanceof \ReflectionClass) {
|
Chris@17
|
52 @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
|
53 $hasDebugInfo = $class->hasMethod('__debugInfo');
|
Chris@0
|
54 $class = $class->name;
|
Chris@0
|
55 }
|
Chris@0
|
56 if ($hasDebugInfo) {
|
Chris@0
|
57 $a = $obj->__debugInfo();
|
Chris@0
|
58 } elseif ($obj instanceof \Closure) {
|
Chris@17
|
59 $a = [];
|
Chris@0
|
60 } else {
|
Chris@0
|
61 $a = (array) $obj;
|
Chris@0
|
62 }
|
Chris@0
|
63 if ($obj instanceof \__PHP_Incomplete_Class) {
|
Chris@0
|
64 return $a;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 if ($a) {
|
Chris@17
|
68 static $publicProperties = [];
|
Chris@0
|
69
|
Chris@0
|
70 $i = 0;
|
Chris@17
|
71 $prefixedKeys = [];
|
Chris@0
|
72 foreach ($a as $k => $v) {
|
Chris@0
|
73 if (isset($k[0]) ? "\0" !== $k[0] : \PHP_VERSION_ID >= 70200) {
|
Chris@0
|
74 if (!isset($publicProperties[$class])) {
|
Chris@0
|
75 foreach (get_class_vars($class) as $prop => $v) {
|
Chris@0
|
76 $publicProperties[$class][$prop] = true;
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|
Chris@0
|
79 if (!isset($publicProperties[$class][$k])) {
|
Chris@0
|
80 $prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k;
|
Chris@0
|
81 }
|
Chris@0
|
82 } elseif (isset($k[16]) && "\0" === $k[16] && 0 === strpos($k, "\0class@anonymous\0")) {
|
Chris@0
|
83 $prefixedKeys[$i] = "\0".get_parent_class($class).'@anonymous'.strrchr($k, "\0");
|
Chris@0
|
84 }
|
Chris@0
|
85 ++$i;
|
Chris@0
|
86 }
|
Chris@0
|
87 if ($prefixedKeys) {
|
Chris@0
|
88 $keys = array_keys($a);
|
Chris@0
|
89 foreach ($prefixedKeys as $i => $k) {
|
Chris@0
|
90 $keys[$i] = $k;
|
Chris@0
|
91 }
|
Chris@0
|
92 $a = array_combine($keys, $a);
|
Chris@0
|
93 }
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 return $a;
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * Filters out the specified properties.
|
Chris@0
|
101 *
|
Chris@0
|
102 * By default, a single match in the $filter bit field filters properties out, following an "or" logic.
|
Chris@0
|
103 * When EXCLUDE_STRICT is set, an "and" logic is applied: all bits must match for a property to be removed.
|
Chris@0
|
104 *
|
Chris@0
|
105 * @param array $a The array containing the properties to filter
|
Chris@0
|
106 * @param int $filter A bit field of Caster::EXCLUDE_* constants specifying which properties to filter out
|
Chris@0
|
107 * @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
|
108 * @param int &$count Set to the number of removed properties
|
Chris@0
|
109 *
|
Chris@0
|
110 * @return array The filtered array
|
Chris@0
|
111 */
|
Chris@17
|
112 public static function filter(array $a, $filter, array $listedProperties = [], &$count = 0)
|
Chris@0
|
113 {
|
Chris@0
|
114 $count = 0;
|
Chris@0
|
115
|
Chris@0
|
116 foreach ($a as $k => $v) {
|
Chris@0
|
117 $type = self::EXCLUDE_STRICT & $filter;
|
Chris@0
|
118
|
Chris@0
|
119 if (null === $v) {
|
Chris@0
|
120 $type |= self::EXCLUDE_NULL & $filter;
|
Chris@12
|
121 $type |= self::EXCLUDE_EMPTY & $filter;
|
Chris@17
|
122 } elseif (false === $v || '' === $v || '0' === $v || 0 === $v || 0.0 === $v || [] === $v) {
|
Chris@0
|
123 $type |= self::EXCLUDE_EMPTY & $filter;
|
Chris@0
|
124 }
|
Chris@17
|
125 if ((self::EXCLUDE_NOT_IMPORTANT & $filter) && !\in_array($k, $listedProperties, true)) {
|
Chris@0
|
126 $type |= self::EXCLUDE_NOT_IMPORTANT;
|
Chris@0
|
127 }
|
Chris@17
|
128 if ((self::EXCLUDE_VERBOSE & $filter) && \in_array($k, $listedProperties, true)) {
|
Chris@0
|
129 $type |= self::EXCLUDE_VERBOSE;
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 if (!isset($k[1]) || "\0" !== $k[0]) {
|
Chris@0
|
133 $type |= self::EXCLUDE_PUBLIC & $filter;
|
Chris@0
|
134 } elseif ('~' === $k[1]) {
|
Chris@0
|
135 $type |= self::EXCLUDE_VIRTUAL & $filter;
|
Chris@0
|
136 } elseif ('+' === $k[1]) {
|
Chris@0
|
137 $type |= self::EXCLUDE_DYNAMIC & $filter;
|
Chris@0
|
138 } elseif ('*' === $k[1]) {
|
Chris@0
|
139 $type |= self::EXCLUDE_PROTECTED & $filter;
|
Chris@0
|
140 } else {
|
Chris@0
|
141 $type |= self::EXCLUDE_PRIVATE & $filter;
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 if ((self::EXCLUDE_STRICT & $filter) ? $type === $filter : $type) {
|
Chris@0
|
145 unset($a[$k]);
|
Chris@0
|
146 ++$count;
|
Chris@0
|
147 }
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 return $a;
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 public static function castPhpIncompleteClass(\__PHP_Incomplete_Class $c, array $a, Stub $stub, $isNested)
|
Chris@0
|
154 {
|
Chris@0
|
155 if (isset($a['__PHP_Incomplete_Class_Name'])) {
|
Chris@0
|
156 $stub->class .= '('.$a['__PHP_Incomplete_Class_Name'].')';
|
Chris@0
|
157 unset($a['__PHP_Incomplete_Class_Name']);
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 return $a;
|
Chris@0
|
161 }
|
Chris@0
|
162 }
|