Passed
Branch feature/first-release (4f172a)
by Andrea Marco
01:55
created

Config::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
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 Closure;
14
15
/**
16
 * The configuration.
17
 *
18
 */
19
final class Config
20
{
21
    /**
22
     * The JSON decoder.
23
     *
24
     * @var Decoder
25
     */
26
    public Decoder $decoder;
27
28
    /**
29
     * The JSON pointers.
30
     *
31
     * @var Pointers
32
     */
33
    public Pointers $pointers;
34
35
    /**
36
     * The number of bytes to read in each chunk.
37
     *
38
     * @var int<1, max>
39
     */
40
    public int $bytes = 1024 * 8;
41
42
    /**
43
     * The callback to run during a decoding error.
44
     *
45
     * @var Closure
46
     */
47
    public Closure $onDecodingError;
48
49
    /**
50
     * The callback to run during a syntax error.
51
     *
52
     * @var Closure
53
     */
54
    public Closure $onSyntaxError;
55
56
    /**
57
     * Instantiate the class
58
     *
59
     */
60 342
    public function __construct()
61
    {
62 342
        $this->decoder = extension_loaded('simdjson') ? new SimdjsonDecoder() : new JsonDecoder();
63 342
        $this->pointers = new Pointers();
64 342
        $this->onDecodingError = fn (DecodedValue $decoded) => throw new DecodingException($decoded);
65 342
        $this->onSyntaxError = fn (SyntaxException $e) => throw $e;
66
    }
67
68
    /**
69
     * Clone the configuration
70
     *
71
     * @return void
72
     */
73 30
    public function __clone(): void
74
    {
75 30
        $this->pointers = new Pointers();
76 30
        $this->pointers->add(new Pointer('', true));
77
    }
78
}
79