Processor::selectPaths()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Path\Processor;
6
7
use Collator;
8
use Remorhaz\JSON\Data\Export\EventDecoder;
9
use Remorhaz\JSON\Data\Export\ValueDecoder;
10
use Remorhaz\JSON\Data\Export\ValueEncoder;
11
use Remorhaz\JSON\Data\Value\NodeValueInterface;
12
use Remorhaz\JSON\Data\Walker\ValueWalker;
13
use Remorhaz\JSON\Path\Processor\Mutator\Mutator;
14
use Remorhaz\JSON\Path\Processor\Mutator\MutatorInterface;
15
use Remorhaz\JSON\Path\Processor\Result\ValueResultInterface;
16
use Remorhaz\JSON\Path\Processor\Result\ResultFactory;
17
use Remorhaz\JSON\Path\Processor\Result\ResultFactoryInterface;
18
use Remorhaz\JSON\Path\Processor\Result\SelectOnePathResultInterface;
19
use Remorhaz\JSON\Path\Processor\Result\SelectPathsResultInterface;
20
use Remorhaz\JSON\Path\Processor\Result\SelectResultInterface;
21
use Remorhaz\JSON\Path\Query\QueryInterface;
22
use Remorhaz\JSON\Path\Query\QueryValidator;
23
use Remorhaz\JSON\Path\Query\QueryValidatorInterface;
24
use Remorhaz\JSON\Path\Runtime\Aggregator\AggregatorCollection;
25
use Remorhaz\JSON\Path\Runtime\ComparatorCollection;
26
use Remorhaz\JSON\Path\Runtime\Evaluator;
27
use Remorhaz\JSON\Path\Runtime\LiteralFactory;
28
use Remorhaz\JSON\Path\Runtime\Matcher\MatcherFactory;
29
use Remorhaz\JSON\Path\Runtime\ValueListFetcher;
30
use Remorhaz\JSON\Path\Runtime\Runtime;
31
use Remorhaz\JSON\Path\Runtime\RuntimeInterface;
32
use Remorhaz\JSON\Path\Runtime\ValueFetcher;
33
34
final class Processor implements ProcessorInterface
35
{
36
37
    private $runtime;
38
39
    private $resultFactory;
40
41
    private $queryValidator;
42
43
    private $mutator;
44
45 18
    public static function create(): ProcessorInterface
46
    {
47 18
        $runtime = new Runtime(
48 18
            new ValueListFetcher(new ValueFetcher()),
49 18
            new Evaluator(
50 18
                new ComparatorCollection(new Collator('UTF-8')),
51 18
                new AggregatorCollection(),
52
            ),
53 18
            new LiteralFactory(),
54 18
            new MatcherFactory(),
55
        );
56 18
        $jsonDecoder = new ValueDecoder();
57 18
        $jsonEncoder = new ValueEncoder($jsonDecoder);
58
59 18
        return new self(
60 18
            $runtime,
61 18
            new ResultFactory($jsonEncoder, $jsonDecoder, new PathEncoder()),
62 18
            new QueryValidator(),
63 18
            new Mutator(new ValueWalker(), new EventDecoder()),
64
        );
65
    }
66
67 18
    public function __construct(
68
        RuntimeInterface $runtime,
69
        ResultFactoryInterface $resultFactory,
70
        QueryValidatorInterface $queryValidator,
71
        MutatorInterface $mutator
72
    ) {
73 18
        $this->runtime = $runtime;
74 18
        $this->resultFactory = $resultFactory;
75 18
        $this->queryValidator = $queryValidator;
76 18
        $this->mutator = $mutator;
77 18
    }
78
79 3
    public function select(QueryInterface $query, NodeValueInterface $rootNode): SelectResultInterface
80
    {
81 3
        $values = $query($rootNode, $this->runtime);
82
83
        return $this
84 3
            ->resultFactory
85 3
            ->createSelectResult($values);
86
    }
87
88 3
    public function selectOne(QueryInterface $query, NodeValueInterface $rootNode): ValueResultInterface
89
    {
90
        $values = $this
91 3
            ->queryValidator
92 3
            ->getDefiniteQuery($query)($rootNode, $this->runtime);
93
94
        return $this
95 2
            ->resultFactory
96 2
            ->createSelectOneResult($values);
97
    }
98
99 8
    public function selectPaths(QueryInterface $query, NodeValueInterface $rootNode): SelectPathsResultInterface
100
    {
101
        $values = $this
102 8
            ->queryValidator
103 8
            ->getAddressableQuery($query)($rootNode, $this->runtime);
104
105
        return $this
106 7
            ->resultFactory
107 7
            ->createSelectPathsResult($values);
108
    }
109
110 4
    public function selectOnePath(QueryInterface $query, NodeValueInterface $rootNode): SelectOnePathResultInterface
111
    {
112
        $query = $this
113 4
            ->queryValidator
114 4
            ->getDefiniteQuery($query);
115
        $values = $this
116 3
            ->queryValidator
117 3
            ->getAddressableQuery($query)($rootNode, $this->runtime);
118
119
        return $this
120 2
            ->resultFactory
121 2
            ->createSelectOnePathResult($values);
122
    }
123
124 3
    public function delete(QueryInterface $query, NodeValueInterface $rootNode): ValueResultInterface
125
    {
126
        $paths = $this
127 3
            ->selectPaths($query, $rootNode)
128 2
            ->get();
129
        $value = $this
130 2
            ->mutator
131 2
            ->deletePaths($rootNode, ...$paths);
132
133
        return $this
134 2
            ->resultFactory
135 2
            ->createValueResult($value);
136
    }
137
138 2
    public function replace(
139
        QueryInterface $query,
140
        NodeValueInterface $rootNode,
141
        NodeValueInterface $newNode
142
    ): ValueResultInterface {
143
        $paths = $this
144 2
            ->selectPaths($query, $rootNode)
145 2
            ->get();
146
        $value = $this
147 2
            ->mutator
148 2
            ->replacePaths($rootNode, $newNode, ...$paths);
149
150
        return $this
151 1
            ->resultFactory
152 1
            ->createValueResult($value);
153
    }
154
}
155