Passed
Push — master ( f13532...bf7dba )
by Andrea Marco
02:44 queued 12s
created

State::mutateByToken()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 7
rs 8.8333
1
<?php
2
3
namespace Cerbero\JsonParser\ValueObjects;
4
5
use Cerbero\JsonParser\Pointers\Pointers;
6
use Cerbero\JsonParser\Tokens\CompoundBegin;
7
use Cerbero\JsonParser\Tokens\Parser;
8
use Cerbero\JsonParser\Tokens\Token;
9
use Cerbero\JsonParser\Tokens\Tokens;
10
use Closure;
11
12
/**
13
 * The JSON parsing state.
14
 *
15
 */
16
final class State
17
{
18
    /**
19
     * The JSON tree.
20
     *
21
     * @var Tree
22
     */
23
    public readonly Tree $tree;
24
25
    /**
26
     * The JSON buffer.
27
     *
28
     * @var Parser|string
29
     */
30
    private Parser|string $buffer = '';
31
32
    /**
33
     * Whether an object key is expected.
34
     *
35
     * @var bool
36
     */
37
    public bool $expectsKey = false;
38
39
    /**
40
     * The expected token.
41
     *
42
     * @var int
43
     */
44
    public int $expectedToken = Tokens::COMPOUND_BEGIN;
45
46
    /**
47
     * Instantiate the class.
48
     *
49
     * @param Pointers $pointers
50
     * @param Closure $lazyLoad
51
     */
52 359
    public function __construct(private readonly Pointers $pointers, private readonly Closure $lazyLoad)
53
    {
54 359
        $this->tree = new Tree($pointers);
0 ignored issues
show
Bug introduced by
The property tree is declared read-only in Cerbero\JsonParser\ValueObjects\State.
Loading history...
55
    }
56
57
    /**
58
     * Retrieve the JSON tree
59
     *
60
     * @return Tree
61
     */
62
    public function tree(): Tree
63
    {
64
        return $this->tree;
65
    }
66
67
    /**
68
     * Determine whether the parser can stop parsing
69
     *
70
     * @return bool
71
     */
72 352
    public function canStopParsing(): bool
73
    {
74 352
        return $this->pointers->wereFoundInTree($this->tree);
75
    }
76
77
    /**
78
     * Call the current pointer callback
79
     *
80
     * @param mixed $value
81
     * @param mixed $key
82
     * @return mixed
83
     */
84 262
    public function callPointer(mixed $value, mixed &$key): mixed
85
    {
86 262
        return $this->pointers->matching()->call($value, $key);
87
    }
88
89
    /**
90
     * Mutate state depending on the given token
91
     *
92
     * @param Token $token
93
     * @return void
94
     */
95 355
    public function mutateByToken(Token $token): void
96
    {
97 355
        $this->tree->traverseToken($token, $this->expectsKey);
98
99 355
        if ($this->tree->isMatched() && ((!$this->expectsKey && $token->isValue()) || $this->tree->isDeep())) {
100 263
            $pointer = $this->pointers->markAsFound();
101
102 263
            if ($token instanceof CompoundBegin && $pointer->isLazy) {
103 32
                $this->buffer = ($this->lazyLoad)();
104 32
                $token->shouldLazyLoad = true;
105
            } else {
106
                /** @phpstan-ignore-next-line */
107 254
                $this->buffer .= $token;
108
            }
109
        }
110
111 355
        $token->mutateState($this);
112
    }
113
114
    /**
115
     * Determine whether the buffer contains tokens
116
     *
117
     * @return bool
118
     */
119 353
    public function hasBuffer(): bool
120
    {
121 353
        return $this->buffer != '';
122
    }
123
124
    /**
125
     * Retrieve the value from the buffer and reset it
126
     *
127
     * @return Parser|string
128
     */
129 263
    public function value(): Parser|string
130
    {
131 263
        $buffer = $this->buffer;
132
133 263
        $this->buffer = '';
134
135 263
        return $buffer;
136
    }
137
}
138