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

ConfigurableDecoder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Cerbero\JsonParser\Decoders;
4
5
use Cerbero\JsonParser\Tokens\Parser;
6
use Cerbero\JsonParser\ValueObjects\Config;
7
8
/**
9
 * The configurable decoder.
10
 *
11
 */
12
final class ConfigurableDecoder
13
{
14
    /**
15
     * Instantiate the class.
16
     *
17
     * @param Config $config
18
     */
19 371
    public function __construct(private readonly Config $config)
20
    {
21 371
    }
22
23
    /**
24
     * Decode the given value.
25
     *
26
     * @param Parser|string|int $value
27
     * @return mixed
28
     */
29 263
    public function decode(Parser|string|int $value): mixed
30
    {
31 263
        if (!is_string($value)) {
32 137
            return $value;
33
        }
34
35 258
        $decoded = $this->config->decoder->decode($value);
36
37 258
        if (!$decoded->succeeded) {
38 6
            ($this->config->onDecodingError)($decoded);
39
        }
40
41 257
        return $decoded->value;
42
    }
43
}
44