StubCaster.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * @internal since Symfony 7.3
  20. */
  21. class StubCaster
  22. {
  23. public static function castStub(Stub $c, array $a, Stub $stub, bool $isNested): array
  24. {
  25. if ($isNested) {
  26. $stub->type = $c->type;
  27. $stub->class = $c->class;
  28. $stub->value = $c->value;
  29. $stub->handle = $c->handle;
  30. $stub->cut = $c->cut;
  31. $stub->attr = $c->attr;
  32. if (Stub::TYPE_REF === $c->type && !$c->class && \is_string($c->value) && !preg_match('//u', $c->value)) {
  33. $stub->type = Stub::TYPE_STRING;
  34. $stub->class = Stub::STRING_BINARY;
  35. }
  36. $a = [];
  37. }
  38. return $a;
  39. }
  40. public static function castCutArray(CutArrayStub $c, array $a, Stub $stub, bool $isNested): array
  41. {
  42. return $isNested ? $c->preservedSubset : $a;
  43. }
  44. public static function cutInternals($obj, array $a, Stub $stub, bool $isNested): array
  45. {
  46. if ($isNested) {
  47. $stub->cut += \count($a);
  48. return [];
  49. }
  50. return $a;
  51. }
  52. public static function castEnum(EnumStub $c, array $a, Stub $stub, bool $isNested): array
  53. {
  54. if ($isNested) {
  55. $stub->class = $c->dumpKeys ? '' : null;
  56. $stub->handle = 0;
  57. $stub->value = null;
  58. $stub->cut = $c->cut;
  59. $stub->attr = $c->attr;
  60. $a = [];
  61. if ($c->value) {
  62. foreach (array_keys($c->value) as $k) {
  63. $keys[] = !isset($k[0]) || "\0" !== $k[0] ? Caster::PREFIX_VIRTUAL.$k : $k;
  64. }
  65. // Preserve references with array_combine()
  66. $a = array_combine($keys, $c->value);
  67. }
  68. }
  69. return $a;
  70. }
  71. public static function castScalar(ScalarStub $scalarStub, array $a, Stub $stub): array
  72. {
  73. $stub->type = Stub::TYPE_SCALAR;
  74. $stub->attr['value'] = $scalarStub->value;
  75. return $a;
  76. }
  77. }