1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\JsonParser\ValueObjects; |
4
|
|
|
|
5
|
|
|
use Cerbero\JsonParser\Decoders\JsonDecoder; |
6
|
|
|
use Cerbero\JsonParser\Decoders\DecodedValue; |
7
|
|
|
use Cerbero\JsonParser\Decoders\Decoder; |
8
|
|
|
use Cerbero\JsonParser\Decoders\SimdjsonDecoder; |
9
|
|
|
use Cerbero\JsonParser\Exceptions\DecodingException; |
10
|
|
|
use Cerbero\JsonParser\Exceptions\SyntaxException; |
11
|
|
|
use Cerbero\JsonParser\Pointers\Pointer; |
12
|
|
|
use Cerbero\JsonParser\Pointers\Pointers; |
13
|
|
|
use Cerbero\JsonParser\Tokens\Parser; |
14
|
|
|
use Closure; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The configuration. |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
final class Config |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The JSON decoder. |
24
|
|
|
* |
25
|
|
|
* @var Decoder |
26
|
|
|
*/ |
27
|
|
|
public Decoder $decoder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The JSON pointers. |
31
|
|
|
* |
32
|
|
|
* @var Pointers |
33
|
|
|
*/ |
34
|
|
|
public Pointers $pointers; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The number of bytes to read in each chunk. |
38
|
|
|
* |
39
|
|
|
* @var int<1, max> |
40
|
|
|
*/ |
41
|
|
|
public int $bytes = 1024 * 8; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The callback to run during a decoding error. |
45
|
|
|
* |
46
|
|
|
* @var Closure |
47
|
|
|
*/ |
48
|
|
|
public Closure $onDecodingError; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The callback to run during a syntax error. |
52
|
|
|
* |
53
|
|
|
* @var Closure |
54
|
|
|
*/ |
55
|
|
|
public Closure $onSyntaxError; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The callback to run for wrapping the parser. |
59
|
|
|
* |
60
|
|
|
* @var Closure |
61
|
|
|
*/ |
62
|
|
|
public Closure $wrapper; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Instantiate the class |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
373 |
|
public function __construct() |
69
|
|
|
{ |
70
|
373 |
|
$this->decoder = extension_loaded('simdjson') ? new SimdjsonDecoder() : new JsonDecoder(); |
71
|
373 |
|
$this->pointers = new Pointers(); |
72
|
373 |
|
$this->onDecodingError = fn (DecodedValue $decoded) => throw new DecodingException($decoded); |
73
|
373 |
|
$this->onSyntaxError = fn (SyntaxException $e) => throw $e; |
74
|
373 |
|
$this->wrapper = fn (Parser $parser) => $parser; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Clone the configuration |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
34 |
|
public function __clone(): void |
83
|
|
|
{ |
84
|
34 |
|
$this->pointers = new Pointers(); |
85
|
34 |
|
$this->pointers->add(new Pointer('', true)); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|