Completed
Push — master ( c16c79...ab8250 )
by Edward
04:36
created

Processor::select()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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