Completed
Push — master ( 8286de...ae3e1f )
by Dominik
02:45
created

QueryBuilder::clo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Saxulum\ElasticSearchQueryBuilder;
4
5
use Saxulum\ElasticSearchQueryBuilder\Node\AbstractNode;
6
use Saxulum\ElasticSearchQueryBuilder\Node\ArrayNode;
7
use Saxulum\ElasticSearchQueryBuilder\Node\CallbackNode;
8
use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode;
9
use Saxulum\ElasticSearchQueryBuilder\Node\ScalarNode;
10
11
class QueryBuilder
12
{
13
    /**
14
     * @var ObjectNode
15
     */
16
    protected $rootNode;
17
18
    /**
19
     * @var AbstractNode
20
     */
21
    protected $node;
22
23 14
    public function __construct()
24
    {
25 14
        $this->rootNode = new ObjectNode();
26 14
        $this->node = $this->rootNode;
27 14
    }
28
29
    /**
30
     * @return Expr
31
     */
32 2
    public function arr()
33
    {
34 2
        return new Expr(new ArrayNode());
35
    }
36
37
    /**
38
     * @param \Closure $callback
39
     *
40
     * @return Expr
41
     */
42 2
    public function clo(\Closure $callback)
43
    {
44 2
        return new Expr(new CallbackNode($callback));
45
    }
46
47
    /**
48
     * @return Expr
49
     */
50 14
    public function obj()
51
    {
52 14
        return new Expr(new ObjectNode());
53
    }
54
55
    /**
56
     * @param string|float|int|bool|null $value
57
     *
58
     * @return Expr
59
     */
60 13
    public function sca($value = null)
61
    {
62 13
        return new Expr(new ScalarNode($value));
63
    }
64
65
    /**
66
     * @param Expr $expr
67
     *
68
     * @return $this
69
     */
70 14
    public function add(Expr $expr)
71
    {
72 14
        $this->addChild($expr);
73 14
        $this->reassignParent($expr);
74
75 14
        return $this;
76
    }
77
78
    /**
79
     * @param Expr $expr
80
     */
81 14
    protected function addChild(Expr $expr)
82
    {
83 14
        $node = $expr->getNode();
84 14
        if ($this->node instanceof ArrayNode) {
85 2
            $this->node->add($node, $expr->isAllowDefault());
86 14
        } elseif ($this->node instanceof ObjectNode) {
87 14
            $this->node->add($expr->getKey(), $node, $expr->isAllowDefault());
88 14
        }
89 14
    }
90
91
    /**
92
     * @param Expr $expr
93
     */
94 14
    protected function reassignParent(Expr $expr)
95
    {
96 14
        $node = $expr->getNode();
97 14
        if ($node instanceof ArrayNode || $node instanceof ObjectNode) {
98 14
            $this->node = $node;
99 14
        }
100 14
    }
101
102
    /**
103
     * @return $this
104
     */
105 1
    public function end()
106
    {
107 1
        $this->node = $this->node->getParent();
108
109 1
        return $this;
110
    }
111
112
    /**
113
     * @return Query
114
     */
115 14
    public function query()
116
    {
117 14
        return new Query($this->rootNode);
118
    }
119
}
120