Passed
Branch feature/first-release (43e0cc)
by Andrea Marco
10:48
created

JsonParser::progress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Cerbero\JsonParser;
4
5
use Cerbero\JsonParser\Decoders\DecodedValue;
6
use Cerbero\JsonParser\Decoders\Decoder;
7
use Cerbero\JsonParser\Decoders\SimdjsonDecoder;
8
use Cerbero\JsonParser\Exceptions\SyntaxException;
9
use Cerbero\JsonParser\Pointers\Pointer;
10
use Cerbero\JsonParser\Sources\AnySource;
11
use Closure;
12
use IteratorAggregate;
13
use Traversable;
14
15
/**
16
 * The JSON parser entry-point.
17
 *
18
 * @implements IteratorAggregate<string|int, mixed>
19
 */
20
final class JsonParser implements IteratorAggregate
21
{
22
    /**
23
     * The configuration.
24
     *
25
     * @var Config
26
     */
27
    private Config $config;
28
29
    /**
30
     * The parser.
31
     *
32
     * @var Parser
33
     */
34
    private Parser $parser;
35
36
    /**
37
     * Instantiate the class.
38
     *
39
     * @param mixed $source
40
     */
41 134
    public function __construct(mixed $source)
42
    {
43 134
        $this->config = new Config();
44 134
        $this->parser = Parser::for(new AnySource($source, $this->config));
45
    }
46
47
    /**
48
     * Statically instantiate the class
49
     *
50
     * @param mixed $source
51
     * @return static
52
     */
53 122
    public static function parse(mixed $source): static
54
    {
55 122
        return new static($source);
56
    }
57
58
    /**
59
     * Retrieve the lazily iterable JSON
60
     *
61
     * @return Traversable<string|int, mixed>
62
     */
63 130
    public function getIterator(): Traversable
64
    {
65
        try {
66 130
            yield from $this->parser;
67 15
        } catch (SyntaxException $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Cerbero\JsonPars...ons\SyntaxException $e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
68 11
            call_user_func($this->config->onSyntaxError, $e);
69
        }
70
    }
71
72
    /**
73
     * Set the JSON pointers
74
     *
75
     * @param string[]|array<string, Closure> $pointers
76
     * @return static
77
     */
78 28
    public function pointers(array $pointers): static
79
    {
80 28
        foreach ($pointers as $pointer => $callback) {
81 28
            $callback instanceof Closure ? $this->pointer($pointer, $callback) : $this->pointer($callback);
82
        }
83
84 28
        return $this;
85
    }
86
87
    /**
88
     * Set a JSON pointer
89
     *
90
     * @param string $pointer
91
     * @param Closure|null $callback
92
     * @return static
93
     */
94 96
    public function pointer(string $pointer, Closure $callback = null): static
95
    {
96 96
        $this->config->pointers[] = new Pointer($pointer, $callback);
97
98 92
        return $this;
99
    }
100
101
    /**
102
     * Traverse the lazily iterable JSON
103
     *
104
     * @param Closure|null $callback
105
     * @return void
106
     */
107 16
    public function traverse(Closure $callback = null): void
108
    {
109 16
        $callback ??= fn () => true;
110
111 16
        foreach ($this as $key => $value) {
112 9
            $callback($value, $key, $this);
113
        }
114
    }
115
116
    /**
117
     * Set the simdjson decoder
118
     *
119
     * @param bool $decodesToArray
120
     * @return static
121
     */
122
    public function simdjson(bool $decodesToArray = true): static
123
    {
124
        return $this->decoder(new SimdjsonDecoder($decodesToArray));
125
    }
126
127
    /**
128
     * Set the JSON decoder
129
     *
130
     * @param Decoder $decoder
131
     * @return static
132
     */
133
    public function decoder(Decoder $decoder): static
134
    {
135
        $this->config->decoder = $decoder;
136
137
        return $this;
138
    }
139
140
    /**
141
     * Retrieve the parsing progress
142
     *
143
     * @return Progress
144
     */
145
    public function progress(): Progress
146
    {
147
        return $this->parser->progress();
148
    }
149
150
    /**
151
     * The number of bytes to read in each chunk
152
     *
153
     * @param int<1, max> $bytes
154
     * @return static
155
     */
156
    public function bytes(int $bytes): static
157
    {
158
        $this->config->bytes = $bytes;
159
160
        return $this;
161
    }
162
163
    /**
164
     * Set the patch to apply during a decoding error
165
     *
166
     * @param mixed $patch
167
     * @return static
168
     */
169 4
    public function patchDecodingError(mixed $patch = null): static
170
    {
171 4
        return $this->onDecodingError(function (DecodedValue $decoded) use ($patch) {
172 4
            $decoded->value = is_callable($patch) ? $patch($decoded) : $patch;
173 4
        });
174
    }
175
176
    /**
177
     * Set the logic to run during a decoding error
178
     *
179
     * @param Closure $callback
180
     * @return static
181
     */
182 5
    public function onDecodingError(Closure $callback): static
183
    {
184 5
        $this->config->onDecodingError = $callback;
185
186 5
        return $this;
187
    }
188
189
    /**
190
     * Set the logic to run during a syntax error
191
     *
192
     * @param Closure $callback
193
     * @return static
194
     */
195 1
    public function onSyntaxError(Closure $callback): static
196
    {
197 1
        $this->config->onSyntaxError = $callback;
198
199 1
        return $this;
200
    }
201
}
202