1 | <?php |
||
2 | |||
3 | namespace Cerbero\JsonParser\Tokens; |
||
4 | |||
5 | use Cerbero\JsonParser\ValueObjects\State; |
||
6 | |||
7 | /** |
||
8 | * The token that begins compound data (JSON arrays or objects). |
||
9 | * |
||
10 | */ |
||
11 | final class CompoundBegin extends Token |
||
12 | { |
||
13 | /** |
||
14 | * Whether this compound should be lazy loaded. |
||
15 | * |
||
16 | * @var bool |
||
17 | */ |
||
18 | public bool $shouldLazyLoad = false; |
||
19 | |||
20 | /** |
||
21 | * Mutate the given state |
||
22 | * |
||
23 | * @param State $state |
||
24 | * @return void |
||
25 | */ |
||
26 | 357 | public function mutateState(State $state): void |
|
27 | { |
||
28 | 357 | if ($this->shouldLazyLoad = $this->shouldLazyLoad && $state->tree->depth() >= 0) { |
|
0 ignored issues
–
show
Comprehensibility
introduced
by
![]() |
|||
29 | 34 | $state->expectedToken = $state->tree->inObject() ? Tokens::AFTER_OBJECT_VALUE : Tokens::AFTER_ARRAY_VALUE; |
|
30 | 34 | return; |
|
31 | } |
||
32 | |||
33 | 357 | $state->expectsKey = $beginsObject = $this->value == '{'; |
|
34 | 357 | $state->expectedToken = $beginsObject ? Tokens::AFTER_OBJECT_BEGIN : Tokens::AFTER_ARRAY_BEGIN; |
|
35 | 357 | $state->tree->deepen($beginsObject); |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Set the token value |
||
40 | * |
||
41 | * @param string $value |
||
42 | * @return static |
||
43 | */ |
||
44 | 357 | public function setValue(string $value): static |
|
45 | { |
||
46 | 357 | $this->shouldLazyLoad = false; |
|
47 | |||
48 | 357 | return parent::setValue($value); |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * Determine whether this token ends a JSON chunk |
||
53 | * |
||
54 | * @return bool |
||
55 | */ |
||
56 | 357 | public function endsChunk(): bool |
|
57 | { |
||
58 | 357 | return $this->shouldLazyLoad; |
|
59 | } |
||
60 | } |
||
61 |