Completed
Push — master ( 3af46d...3c2931 )
by Dominik
03:28
created

QueryBuilder::reassignParent()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
crap 3
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 a()
33
    {
34 2
        return new Expr(new ArrayNode());
35
    }
36
37
    /**
38
     * @param \Closure $callback
39
     *
40
     * @return Expr
41
     */
42 2
    public function c(\Closure $callback)
43
    {
44 2
        return new Expr(new CallbackNode($callback));
45
    }
46
47
    /**
48
     * @return Expr
49
     */
50 14
    public function o()
51
    {
52 14
        return new Expr(new ObjectNode());
53
    }
54
55
    /**
56
     * @return Expr
57
     */
58 13
    public function s($value = null)
59
    {
60 13
        return new Expr(new ScalarNode($value));
61
    }
62
63
    /**
64
     * @param Expr $expr
65
     *
66
     * @return $this
67
     */
68 14
    public function add(Expr $expr)
69
    {
70 14
        $this->addChild($expr);
71 14
        $this->reassignParent($expr);
72
73 14
        return $this;
74
    }
75
76
    /**
77
     * @param Expr $expr
78
     */
79 14
    protected function addChild(Expr $expr)
80
    {
81 14
        $node = $expr->getNode();
82 14
        if ($this->node instanceof ArrayNode) {
83 2
            $this->node->add($node, $expr->isAllowDefault());
84 14
        } elseif ($this->node instanceof ObjectNode) {
85 14
            $this->node->add($expr->getKey(), $node, $expr->isAllowDefault());
86 14
        }
87 14
    }
88
89
    /**
90
     * @param Expr $expr
91
     */
92 14
    protected function reassignParent(Expr $expr)
93
    {
94 14
        $node = $expr->getNode();
95 14
        if ($node instanceof ArrayNode || $node instanceof ObjectNode) {
96 14
            $this->node = $node;
97 14
        }
98 14
    }
99
100
    /**
101
     * @return $this
102
     */
103 1
    public function end()
104
    {
105 1
        $this->node = $this->node->getParent();
106
107 1
        return $this;
108
    }
109
110
    /**
111
     * @return Query
112
     */
113 14
    public function query()
114
    {
115 14
        return new Query($this->rootNode);
116
    }
117
}
118