Passed
Push — master ( c6e62a...162b02 )
by Edward
03:02
created

Processor::selectOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 9
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\Export\Decoder;
8
use Remorhaz\JSON\Data\Export\Encoder;
9
use Remorhaz\JSON\Data\Iterator\ValueIteratorFactory;
10
use Remorhaz\JSON\Data\Value\NodeValueInterface;
11
use Remorhaz\JSON\Path\Query\QueryInterface;
12
use Remorhaz\JSON\Path\Runtime\Aggregator\AggregatorCollection;
13
use Remorhaz\JSON\Path\Runtime\Comparator\ComparatorCollection;
14
use Remorhaz\JSON\Path\Runtime\Evaluator;
15
use Remorhaz\JSON\Path\Runtime\Fetcher;
16
use Remorhaz\JSON\Path\Runtime\Runtime;
17
use Remorhaz\JSON\Path\Runtime\RuntimeInterface;
18
19
final class Processor implements ProcessorInterface
20
{
21
22
    private $runtime;
23
24
    private $resultFactory;
25
26
    private $queryValidator;
27
28
    public static function create(): ProcessorInterface
29
    {
30
        $valueIteratorFactory = new ValueIteratorFactory;
31
        $runtime = new Runtime(
32
            new Fetcher($valueIteratorFactory),
33
            new Evaluator(
34
                new ComparatorCollection($valueIteratorFactory, new Collator('UTF-8')),
35
                new AggregatorCollection($valueIteratorFactory)
36
            )
37
        );
38
        $jsonDecoder = new Decoder($valueIteratorFactory);
39
        $jsonEncoder = new Encoder($jsonDecoder);
40
41
        return new self(
42
            $runtime,
43
            new ResultFactory($jsonEncoder, $jsonDecoder, new PathEncoder),
44
            new QueryValidator
45
        );
46
    }
47
48
    public function __construct(
49
        RuntimeInterface $runtime,
50
        ResultFactoryInterface $resultFactory,
51
        QueryValidatorInterface $queryValidator
52
    ) {
53
        $this->runtime = $runtime;
54
        $this->resultFactory = $resultFactory;
55
        $this->queryValidator = $queryValidator;
56
    }
57
58
    public function select(QueryInterface $query, NodeValueInterface $rootNode): SelectResultInterface
59
    {
60
        $values = $query($this->runtime, $rootNode);
61
62
        return $this
63
            ->resultFactory
64
            ->createSelectResult($values);
65
    }
66
67
    public function selectOne(QueryInterface $query, NodeValueInterface $rootNode): SelectOneResultInterface
68
    {
69
        $values = $this
70
            ->queryValidator
71
            ->getDefiniteQuery($query)($this->runtime, $rootNode);
72
73
        return $this
74
            ->resultFactory
75
            ->createSelectOneResult($values);
76
    }
77
78
    public function selectPaths(QueryInterface $query, NodeValueInterface $rootNode): SelectPathsResultInterface
79
    {
80
        $values = $this
81
            ->queryValidator
82
            ->getPathQuery($query)($this->runtime, $rootNode);
83
84
        return $this
85
            ->resultFactory
86
            ->createSelectPathsResult($values);
87
    }
88
89
    public function selectOnePath(QueryInterface $query, NodeValueInterface $rootNode): SelectOnePathResultInterface
90
    {
91
        $values = $this
92
            ->queryValidator
93
            ->getPathQuery($query)($this->runtime, $rootNode);
94
95
        return $this
96
            ->resultFactory
97
            ->createSelectOnePathResult($values);
98
    }
99
}
100