Completed
Push — master ( 373aee...88365a )
by Dominik
02:29
created

QueryBuilder::end()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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 AbstractNode
15
     */
16
    protected $node;
17
18 14
    public function __construct()
19
    {
20 14
        $this->node = new ObjectNode();
21 14
    }
22
23
    /**
24
     * @return Expr
25
     */
26 1
    public function a()
27
    {
28 1
        return new Expr(new ArrayNode());
29
    }
30
31
    /**
32
     * @param \Closure $callback
33
     *
34
     * @return Expr
35
     */
36 2
    public function c(\Closure $callback)
37
    {
38 2
        return new Expr(new CallbackNode($callback));
39
    }
40
41
    /**
42
     * @return Expr
43
     */
44 13
    public function o()
45
    {
46 13
        return new Expr(new ObjectNode());
47
    }
48
49
    /**
50
     * @return Expr
51
     */
52 13
    public function s($value = null)
53
    {
54 13
        return new Expr(new ScalarNode($value));
55
    }
56
57
    /**
58
     * @param Expr $expr
59
     *
60
     * @return $this
61
     */
62 14
    public function add(Expr $expr)
63
    {
64 14
        $node = $expr->getNode();
65 14
        if ($this->node instanceof ArrayNode) {
66 1
            $this->node->add($node, $expr->isAllowDefault());
67 14
        } elseif ($this->node instanceof ObjectNode) {
68 14
            $this->node->add($expr->getKey(), $node, $expr->isAllowDefault());
69 14
        } else {
70 1
            throw new \InvalidArgumentException(sprintf('Node does not support a child!'));
71
        }
72
73 14
        $this->node = $node;
74
75 14
        return $this;
76
    }
77
78
    /**
79
     * @return $this
80
     */
81 13
    public function end()
82
    {
83 13
        $this->node = $this->node->getParent();
84
85 13
        return $this;
86
    }
87
88
    /**
89
     * @return Query
90
     */
91 13
    public function query()
92
    {
93 13
        return new Query($this->node);
94
    }
95
}
96