blob: 32ead7c277722d1c6ad1f4bb6547426378577846 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\VarDumper\Caster;
13
14use Symfony\Component\VarDumper\Cloner\Stub;
15
16/**
17 * Casts a caster's Stub.
18 *
19 * @author Nicolas Grekas <p@tchwork.com>
20 *
21 * @final
22 */
23class StubCaster
24{
25 public static function castStub(Stub $c, array $a, Stub $stub, bool $isNested)
26 {
27 if ($isNested) {
28 $stub->type = $c->type;
29 $stub->class = $c->class;
30 $stub->value = $c->value;
31 $stub->handle = $c->handle;
32 $stub->cut = $c->cut;
33 $stub->attr = $c->attr;
34
35 if (Stub::TYPE_REF === $c->type && !$c->class && \is_string($c->value) && !preg_match('//u', $c->value)) {
36 $stub->type = Stub::TYPE_STRING;
37 $stub->class = Stub::STRING_BINARY;
38 }
39
40 $a = [];
41 }
42
43 return $a;
44 }
45
46 public static function castCutArray(CutArrayStub $c, array $a, Stub $stub, bool $isNested)
47 {
48 return $isNested ? $c->preservedSubset : $a;
49 }
50
51 public static function cutInternals($obj, array $a, Stub $stub, bool $isNested)
52 {
53 if ($isNested) {
54 $stub->cut += \count($a);
55
56 return [];
57 }
58
59 return $a;
60 }
61
62 public static function castEnum(EnumStub $c, array $a, Stub $stub, bool $isNested)
63 {
64 if ($isNested) {
65 $stub->class = $c->dumpKeys ? '' : null;
66 $stub->handle = 0;
67 $stub->value = null;
68 $stub->cut = $c->cut;
69 $stub->attr = $c->attr;
70
71 $a = [];
72
73 if ($c->value) {
74 foreach (array_keys($c->value) as $k) {
75 $keys[] = !isset($k[0]) || "\0" !== $k[0] ? Caster::PREFIX_VIRTUAL.$k : $k;
76 }
77 // Preserve references with array_combine()
78 $a = array_combine($keys, $c->value);
79 }
80 }
81
82 return $a;
83 }
84}