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

LazyQuery::getSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Path\Query;
5
6
use Remorhaz\JSON\Data\Value\NodeValueInterface;
7
use Remorhaz\JSON\Path\Value\ValueListInterface;
8
use Remorhaz\JSON\Path\Parser\ParserInterface;
9
use Remorhaz\JSON\Path\Runtime\RuntimeInterface;
10
11
final class LazyQuery implements QueryInterface
12
{
13
14
    private $loadedQuery;
15
16
    private $source;
17
18
    private $parser;
19
20
    private $astTranslator;
21
22
    public function __construct(string $source, ParserInterface $parser, QueryAstTranslatorInterface $astTranslator)
23
    {
24
        $this->source = $source;
25
        $this->parser = $parser;
26
        $this->astTranslator = $astTranslator;
27
    }
28
29
    public function __invoke(RuntimeInterface $runtime, NodeValueInterface $rootNode): ValueListInterface
30
    {
31
        return $this->getLoadedQuery()($runtime, $rootNode);
32
    }
33
34
    /**
35
     * @return QueryCapabilitiesInterface
36
     * @deprecated
37
     */
38
    public function getProperties(): QueryCapabilitiesInterface
39
    {
40
        return $this->getCapabilities();
41
    }
42
43
    public function getCapabilities(): QueryCapabilitiesInterface
44
    {
45
        return $this
46
            ->getLoadedQuery()
47
            ->getCapabilities();
48
    }
49
50
    public function getSource(): string
51
    {
52
        return $this->source;
53
    }
54
55
    private function getLoadedQuery(): QueryInterface
56
    {
57
        if (!isset($this->loadedQuery)) {
58
            $this->loadedQuery = $this->loadQuery();
59
        }
60
61
        return $this->loadedQuery;
62
    }
63
64
    private function loadQuery(): QueryInterface
65
    {
66
        $queryAst = $this
67
            ->parser
68
            ->buildQueryAst($this->source);
69
70
        return $this
71
            ->astTranslator
72
            ->buildQuery($this->source, $queryAst);
73
    }
74
}
75