Caster.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. } else {
  44. $a = (array) $obj;
  45. }
  46. if ($a) {
  47. $p = array_keys($a);
  48. foreach ($p as $i => $k) {
  49. if (!isset($k[0]) || ("\0" !== $k[0] && !$reflector->hasProperty($k))) {
  50. $p[$i] = self::PREFIX_DYNAMIC.$k;
  51. }
  52. }
  53. $a = array_combine($p, $a);
  54. }
  55. return $a;
  56. }
  57. /**
  58. * Filters out the specified properties.
  59. *
  60. * By default, a single match in the $filter bit field filters properties out, following an "or" logic.
  61. * When EXCLUDE_STRICT is set, an "and" logic is applied: all bits must match for a property to be removed.
  62. *
  63. * @param array $a The array containing the properties to filter.
  64. * @param int $filter A bit field of Caster::EXCLUDE_* constants specifying which properties to filter out.
  65. * @param string[] $listedProperties List of properties to exclude when Caster::EXCLUDE_VERBOSE is set, and to preserve when Caster::EXCLUDE_NOT_IMPORTANT is set.
  66. *
  67. * @return array The filtered array
  68. */
  69. public static function filter(array $a, $filter, array $listedProperties = array())
  70. {
  71. foreach ($a as $k => $v) {
  72. $type = self::EXCLUDE_STRICT & $filter;
  73. if (null === $v) {
  74. $type |= self::EXCLUDE_NULL & $filter;
  75. }
  76. if (empty($v)) {
  77. $type |= self::EXCLUDE_EMPTY & $filter;
  78. }
  79. if ((self::EXCLUDE_NOT_IMPORTANT & $filter) && !in_array($k, $listedProperties, true)) {
  80. $type |= self::EXCLUDE_NOT_IMPORTANT;
  81. }
  82. if ((self::EXCLUDE_VERBOSE & $filter) && in_array($k, $listedProperties, true)) {
  83. $type |= self::EXCLUDE_VERBOSE;
  84. }
  85. if (!isset($k[1]) || "\0" !== $k[0]) {
  86. $type |= self::EXCLUDE_PUBLIC & $filter;
  87. } elseif ('~' === $k[1]) {
  88. $type |= self::EXCLUDE_VIRTUAL & $filter;
  89. } elseif ('+' === $k[1]) {
  90. $type |= self::EXCLUDE_DYNAMIC & $filter;
  91. } elseif ('*' === $k[1]) {
  92. $type |= self::EXCLUDE_PROTECTED & $filter;
  93. } else {
  94. $type |= self::EXCLUDE_PRIVATE & $filter;
  95. }
  96. if ((self::EXCLUDE_STRICT & $filter) ? $type === $filter : $type) {
  97. unset($a[$k]);
  98. }
  99. }
  100. return $a;
  101. }
  102. }