Нет описания

Caster.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. class Caster
  18. {
  19. const EXCLUDE_VERBOSE = 1;
  20. const EXCLUDE_VIRTUAL = 2;
  21. const EXCLUDE_DYNAMIC = 4;
  22. const EXCLUDE_PUBLIC = 8;
  23. const EXCLUDE_PROTECTED = 16;
  24. const EXCLUDE_PRIVATE = 32;
  25. const EXCLUDE_NULL = 64;
  26. const EXCLUDE_EMPTY = 128;
  27. const EXCLUDE_NOT_IMPORTANT = 256;
  28. const EXCLUDE_STRICT = 512;
  29. const PREFIX_VIRTUAL = "\0~\0";
  30. const PREFIX_DYNAMIC = "\0+\0";
  31. const PREFIX_PROTECTED = "\0*\0";
  32. /**
  33. * Casts objects to arrays and adds the dynamic property prefix.
  34. *
  35. * @param object $obj The object to cast
  36. * @param \ReflectionClass $reflector The class reflector to use for inspecting the object definition
  37. *
  38. * @return array The array-cast of the object, with prefixed dynamic properties
  39. */
  40. public static function castObject($obj, \ReflectionClass $reflector)
  41. {
  42. if ($reflector->hasMethod('__debugInfo')) {
  43. $a = $obj->__debugInfo();
  44. } elseif ($obj instanceof \Closure) {
  45. $a = array();
  46. } else {
  47. $a = (array) $obj;
  48. }
  49. if ($obj instanceof \__PHP_Incomplete_Class) {
  50. return $a;
  51. }
  52. if ($a) {
  53. $combine = false;
  54. $p = array_keys($a);
  55. foreach ($p as $i => $k) {
  56. if (isset($k[0]) && "\0" !== $k[0] && !$reflector->hasProperty($k)) {
  57. $combine = true;
  58. $p[$i] = self::PREFIX_DYNAMIC.$k;
  59. } elseif (isset($k[16]) && "\0" === $k[16] && 0 === strpos($k, "\0class@anonymous\0")) {
  60. $combine = true;
  61. $p[$i] = "\0".$reflector->getParentClass().'@anonymous'.strrchr($k, "\0");
  62. }
  63. }
  64. if ($combine) {
  65. $a = array_combine($p, $a);
  66. }
  67. }
  68. return $a;
  69. }
  70. /**
  71. * Filters out the specified properties.
  72. *
  73. * By default, a single match in the $filter bit field filters properties out, following an "or" logic.
  74. * When EXCLUDE_STRICT is set, an "and" logic is applied: all bits must match for a property to be removed.
  75. *
  76. * @param array $a The array containing the properties to filter
  77. * @param int $filter A bit field of Caster::EXCLUDE_* constants specifying which properties to filter out
  78. * @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. * @param int &$count Set to the number of removed properties
  80. *
  81. * @return array The filtered array
  82. */
  83. public static function filter(array $a, $filter, array $listedProperties = array(), &$count = 0)
  84. {
  85. $count = 0;
  86. foreach ($a as $k => $v) {
  87. $type = self::EXCLUDE_STRICT & $filter;
  88. if (null === $v) {
  89. $type |= self::EXCLUDE_NULL & $filter;
  90. }
  91. if (empty($v)) {
  92. $type |= self::EXCLUDE_EMPTY & $filter;
  93. }
  94. if ((self::EXCLUDE_NOT_IMPORTANT & $filter) && !in_array($k, $listedProperties, true)) {
  95. $type |= self::EXCLUDE_NOT_IMPORTANT;
  96. }
  97. if ((self::EXCLUDE_VERBOSE & $filter) && in_array($k, $listedProperties, true)) {
  98. $type |= self::EXCLUDE_VERBOSE;
  99. }
  100. if (!isset($k[1]) || "\0" !== $k[0]) {
  101. $type |= self::EXCLUDE_PUBLIC & $filter;
  102. } elseif ('~' === $k[1]) {
  103. $type |= self::EXCLUDE_VIRTUAL & $filter;
  104. } elseif ('+' === $k[1]) {
  105. $type |= self::EXCLUDE_DYNAMIC & $filter;
  106. } elseif ('*' === $k[1]) {
  107. $type |= self::EXCLUDE_PROTECTED & $filter;
  108. } else {
  109. $type |= self::EXCLUDE_PRIVATE & $filter;
  110. }
  111. if ((self::EXCLUDE_STRICT & $filter) ? $type === $filter : $type) {
  112. unset($a[$k]);
  113. ++$count;
  114. }
  115. }
  116. return $a;
  117. }
  118. public static function castPhpIncompleteClass(\__PHP_Incomplete_Class $c, array $a, Stub $stub, $isNested)
  119. {
  120. $stub->class .= '('.$a['__PHP_Incomplete_Class_Name'].')';
  121. unset($a['__PHP_Incomplete_Class_Name']);
  122. return $a;
  123. }
  124. }