Caster.php 5.8 KB

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