StrictElementMatcher::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Path\Runtime\Matcher;
6
7
use Remorhaz\JSON\Data\Value\NodeValueInterface;
8
9
use function array_search;
10
use function in_array;
11
use function is_int;
12
13
final class StrictElementMatcher implements SortedChildMatcherInterface
14
{
15
16
    private $indexes;
17
18 3
    public function __construct(int ...$indexes)
19
    {
20 3
        $this->indexes = $indexes;
21 3
    }
22
23 3
    public function match($address, NodeValueInterface $value, NodeValueInterface $container): bool
24
    {
25 3
        return in_array($address, $this->indexes, true);
26
    }
27
28
    public function getSortIndex($address, NodeValueInterface $value, NodeValueInterface $container): int
29
    {
30
        $index = array_search($address, $this->indexes);
31
32
        if (is_int($index)) {
33
            return $index;
34
        }
35
36
        throw new Exception\AddressNotSortableException($address);
37
    }
38
}
39