StubCaster.php 2.1 KB

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