db052c8498a150c79508400d787c295cb96f3a79
[yaffs-website] / vendor / symfony / var-dumper / Caster / Caster.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\VarDumper\Caster;
13
14 /**
15  * Helper for filtering out properties in casters.
16  *
17  * @author Nicolas Grekas <p@tchwork.com>
18  */
19 class Caster
20 {
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;
31
32     const PREFIX_VIRTUAL = "\0~\0";
33     const PREFIX_DYNAMIC = "\0+\0";
34     const PREFIX_PROTECTED = "\0*\0";
35
36     /**
37      * Casts objects to arrays and adds the dynamic property prefix.
38      *
39      * @param object           $obj       The object to cast
40      * @param \ReflectionClass $reflector The class reflector to use for inspecting the object definition
41      *
42      * @return array The array-cast of the object, with prefixed dynamic properties
43      */
44     public static function castObject($obj, \ReflectionClass $reflector)
45     {
46         if ($reflector->hasMethod('__debugInfo')) {
47             $a = $obj->__debugInfo();
48         } elseif ($obj instanceof \Closure) {
49             $a = array();
50         } else {
51             $a = (array) $obj;
52         }
53
54         if ($a) {
55             $p = array_keys($a);
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");
61                 }
62             }
63             $a = array_combine($p, $a);
64         }
65
66         return $a;
67     }
68
69     /**
70      * Filters out the specified properties.
71      *
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.
74      *
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
78      *
79      * @return array The filtered array
80      */
81     public static function filter(array $a, $filter, array $listedProperties = array())
82     {
83         foreach ($a as $k => $v) {
84             $type = self::EXCLUDE_STRICT & $filter;
85
86             if (null === $v) {
87                 $type |= self::EXCLUDE_NULL & $filter;
88             }
89             if (empty($v)) {
90                 $type |= self::EXCLUDE_EMPTY & $filter;
91             }
92             if ((self::EXCLUDE_NOT_IMPORTANT & $filter) && !in_array($k, $listedProperties, true)) {
93                 $type |= self::EXCLUDE_NOT_IMPORTANT;
94             }
95             if ((self::EXCLUDE_VERBOSE & $filter) && in_array($k, $listedProperties, true)) {
96                 $type |= self::EXCLUDE_VERBOSE;
97             }
98
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;
107             } else {
108                 $type |= self::EXCLUDE_PRIVATE & $filter;
109             }
110
111             if ((self::EXCLUDE_STRICT & $filter) ? $type === $filter : $type) {
112                 unset($a[$k]);
113             }
114         }
115
116         return $a;
117     }
118 }