Passed
Push — master ( c2f53d...1e149c )
by Edward
02:16
created

LazyQuery::getLoadedQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
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 $path;
17
18
    private $parser;
19
20
    private $astTranslator;
21
22
    public function __construct(string $path, ParserInterface $parser, QueryAstTranslatorInterface $astTranslator)
23
    {
24
        $this->path = $path;
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
    public function isDefinite(): bool
35
    {
36
        return $this
37
            ->getLoadedQuery()
38
            ->isDefinite();
39
    }
40
41
    private function getLoadedQuery(): QueryInterface
42
    {
43
        if (!isset($this->loadedQuery)) {
44
            $this->loadedQuery = $this->loadQuery();
45
        }
46
47
        return $this->loadedQuery;
48
    }
49
50
    private function loadQuery(): QueryInterface
51
    {
52
        $queryAst = $this
53
            ->parser
54
            ->buildQueryAst($this->path);
55
56
        return $this
57
            ->astTranslator
58
            ->buildQuery($queryAst);
59
    }
60
}
61