You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
6.0 KiB

3 years ago
  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. public const EXCLUDE_VERBOSE = 1;
  22. public const EXCLUDE_VIRTUAL = 2;
  23. public const EXCLUDE_DYNAMIC = 4;
  24. public const EXCLUDE_PUBLIC = 8;
  25. public const EXCLUDE_PROTECTED = 16;
  26. public const EXCLUDE_PRIVATE = 32;
  27. public const EXCLUDE_NULL = 64;
  28. public const EXCLUDE_EMPTY = 128;
  29. public const EXCLUDE_NOT_IMPORTANT = 256;
  30. public const EXCLUDE_STRICT = 512;
  31. public const PREFIX_VIRTUAL = "\0~\0";
  32. public const PREFIX_DYNAMIC = "\0+\0";
  33. public const PREFIX_PROTECTED = "\0*\0";
  34. /**
  35. * Casts objects to arrays and adds the dynamic property prefix.
  36. *
  37. * @param bool $hasDebugInfo Whether the __debugInfo method exists on $obj or not
  38. *
  39. * @return array The array-cast of the object, with prefixed dynamic properties
  40. */
  41. public static function castObject(object $obj, string $class, bool $hasDebugInfo = false, string $debugClass = null): array
  42. {
  43. if ($hasDebugInfo) {
  44. try {
  45. $debugInfo = $obj->__debugInfo();
  46. } catch (\Exception $e) {
  47. // ignore failing __debugInfo()
  48. $hasDebugInfo = false;
  49. }
  50. }
  51. $a = $obj instanceof \Closure ? [] : (array) $obj;
  52. if ($obj instanceof \__PHP_Incomplete_Class) {
  53. return $a;
  54. }
  55. if ($a) {
  56. static $publicProperties = [];
  57. $debugClass = $debugClass ?? get_debug_type($obj);
  58. $i = 0;
  59. $prefixedKeys = [];
  60. foreach ($a as $k => $v) {
  61. if ("\0" !== ($k[0] ?? '')) {
  62. if (!isset($publicProperties[$class])) {
  63. foreach ((new \ReflectionClass($class))->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
  64. $publicProperties[$class][$prop->name] = true;
  65. }
  66. }
  67. if (!isset($publicProperties[$class][$k])) {
  68. $prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k;
  69. }
  70. } elseif ($debugClass !== $class && 1 === strpos($k, $class)) {
  71. $prefixedKeys[$i] = "\0".$debugClass.strrchr($k, "\0");
  72. }
  73. ++$i;
  74. }
  75. if ($prefixedKeys) {
  76. $keys = array_keys($a);
  77. foreach ($prefixedKeys as $i => $k) {
  78. $keys[$i] = $k;
  79. }
  80. $a = array_combine($keys, $a);
  81. }
  82. }
  83. if ($hasDebugInfo && \is_array($debugInfo)) {
  84. foreach ($debugInfo as $k => $v) {
  85. if (!isset($k[0]) || "\0" !== $k[0]) {
  86. if (\array_key_exists(self::PREFIX_DYNAMIC.$k, $a)) {
  87. continue;
  88. }
  89. $k = self::PREFIX_VIRTUAL.$k;
  90. }
  91. unset($a[$k]);
  92. $a[$k] = $v;
  93. }
  94. }
  95. return $a;
  96. }
  97. /**
  98. * Filters out the specified properties.
  99. *
  100. * By default, a single match in the $filter bit field filters properties out, following an "or" logic.
  101. * When EXCLUDE_STRICT is set, an "and" logic is applied: all bits must match for a property to be removed.
  102. *
  103. * @param array $a The array containing the properties to filter
  104. * @param int $filter A bit field of Caster::EXCLUDE_* constants specifying which properties to filter out
  105. * @param string[] $listedProperties List of properties to exclude when Caster::EXCLUDE_VERBOSE is set, and to preserve when Caster::EXCLUDE_NOT_IMPORTANT is set
  106. * @param int &$count Set to the number of removed properties
  107. *
  108. * @return array The filtered array
  109. */
  110. public static function filter(array $a, int $filter, array $listedProperties = [], ?int &$count = 0): array
  111. {
  112. $count = 0;
  113. foreach ($a as $k => $v) {
  114. $type = self::EXCLUDE_STRICT & $filter;
  115. if (null === $v) {
  116. $type |= self::EXCLUDE_NULL & $filter;
  117. $type |= self::EXCLUDE_EMPTY & $filter;
  118. } elseif (false === $v || '' === $v || '0' === $v || 0 === $v || 0.0 === $v || [] === $v) {
  119. $type |= self::EXCLUDE_EMPTY & $filter;
  120. }
  121. if ((self::EXCLUDE_NOT_IMPORTANT & $filter) && !\in_array($k, $listedProperties, true)) {
  122. $type |= self::EXCLUDE_NOT_IMPORTANT;
  123. }
  124. if ((self::EXCLUDE_VERBOSE & $filter) && \in_array($k, $listedProperties, true)) {
  125. $type |= self::EXCLUDE_VERBOSE;
  126. }
  127. if (!isset($k[1]) || "\0" !== $k[0]) {
  128. $type |= self::EXCLUDE_PUBLIC & $filter;
  129. } elseif ('~' === $k[1]) {
  130. $type |= self::EXCLUDE_VIRTUAL & $filter;
  131. } elseif ('+' === $k[1]) {
  132. $type |= self::EXCLUDE_DYNAMIC & $filter;
  133. } elseif ('*' === $k[1]) {
  134. $type |= self::EXCLUDE_PROTECTED & $filter;
  135. } else {
  136. $type |= self::EXCLUDE_PRIVATE & $filter;
  137. }
  138. if ((self::EXCLUDE_STRICT & $filter) ? $type === $filter : $type) {
  139. unset($a[$k]);
  140. ++$count;
  141. }
  142. }
  143. return $a;
  144. }
  145. public static function castPhpIncompleteClass(\__PHP_Incomplete_Class $c, array $a, Stub $stub, bool $isNested): array
  146. {
  147. if (isset($a['__PHP_Incomplete_Class_Name'])) {
  148. $stub->class .= '('.$a['__PHP_Incomplete_Class_Name'].')';
  149. unset($a['__PHP_Incomplete_Class_Name']);
  150. }
  151. return $a;
  152. }
  153. }