StubCaster.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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): array
  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): array
  39. {
  40. return $isNested ? $c->preservedSubset : $a;
  41. }
  42. public static function cutInternals($obj, array $a, Stub $stub, bool $isNested): array
  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): array
  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. public static function castScalar(ScalarStub $scalarStub, array $a, Stub $stub): array
  70. {
  71. $stub->type = Stub::TYPE_SCALAR;
  72. $stub->attr['value'] = $scalarStub->value;
  73. return $a;
  74. }
  75. }