Caster.php 4.0 KB

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