StubCaster.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. * Casts a caster's Stub.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. *
  17. * @final
  18. */
  19. class StubCaster
  20. {
  21. public static function castStub(Stub $c, array $a, Stub $stub, bool $isNested)
  22. {
  23. if ($isNested) {
  24. $stub->type = $c->type;
  25. $stub->class = $c->class;
  26. $stub->value = $c->value;
  27. $stub->handle = $c->handle;
  28. $stub->cut = $c->cut;
  29. $stub->attr = $c->attr;
  30. if (Stub::TYPE_REF === $c->type && !$c->class && \is_string($c->value) && !preg_match('//u', $c->value)) {
  31. $stub->type = Stub::TYPE_STRING;
  32. $stub->class = Stub::STRING_BINARY;
  33. }
  34. $a = [];
  35. }
  36. return $a;
  37. }
  38. public static function castCutArray(CutArrayStub $c, array $a, Stub $stub, bool $isNested)
  39. {
  40. return $isNested ? $c->preservedSubset : $a;
  41. }
  42. public static function cutInternals($obj, array $a, Stub $stub, bool $isNested)
  43. {
  44. if ($isNested) {
  45. $stub->cut += \count($a);
  46. return [];
  47. }
  48. return $a;
  49. }
  50. public static function castEnum(EnumStub $c, array $a, Stub $stub, bool $isNested)
  51. {
  52. if ($isNested) {
  53. $stub->class = $c->dumpKeys ? '' : null;
  54. $stub->handle = 0;
  55. $stub->value = null;
  56. $stub->cut = $c->cut;
  57. $stub->attr = $c->attr;
  58. $a = [];
  59. if ($c->value) {
  60. foreach (array_keys($c->value) as $k) {
  61. $keys[] = !isset($k[0]) || "\0" !== $k[0] ? Caster::PREFIX_VIRTUAL.$k : $k;
  62. }
  63. // Preserve references with array_combine()
  64. $a = array_combine($keys, $c->value);
  65. }
  66. }
  67. return $a;
  68. }
  69. }