Passed
Push — master ( ccee81...9f8f58 )
by Dominik
04:31
created

QueryBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Saxulum\ElasticSearchQueryBuilder;
6
7
use Saxulum\ElasticSearchQueryBuilder\Node\AbstractNode;
8
use Saxulum\ElasticSearchQueryBuilder\Node\ArrayNode;
9
use Saxulum\ElasticSearchQueryBuilder\Node\BoolNode;
10
use Saxulum\ElasticSearchQueryBuilder\Node\FloatNode;
11
use Saxulum\ElasticSearchQueryBuilder\Node\IntNode;
12
use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode;
13
use Saxulum\ElasticSearchQueryBuilder\Node\ScalarNode;
14
use Saxulum\ElasticSearchQueryBuilder\Node\StringNode;
15
16
final class QueryBuilder implements QueryBuilderInterface
17
{
18
    /**
19
     * @var ObjectNode
20
     */
21
    private $rootNode;
22
23
    /**
24
     * @var AbstractNode
25
     */
26
    private $node;
27
28 19
    public function __construct()
29
    {
30 19
        $this->rootNode = new ObjectNode();
31 19
        $this->node = $this->rootNode;
32 19
    }
33
34
    /**
35
     * @param AbstractNode $node
36
     * @param bool         $allowDefault
37
     *
38
     * @return QueryBuilderInterface
39
     *
40
     * @throws \Exception
41
     */
42 5 View Code Duplication
    public function addToArrayNode(AbstractNode $node, bool $allowDefault = false): QueryBuilderInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44 5
        if (!$this->node instanceof ArrayNode) {
45 1
            throw new \Exception(sprintf('You cannot call %s on node type: %s', __FUNCTION__, get_class($this->node)));
46
        }
47
48 4
        $this->node->add($node, $allowDefault);
49 4
        $this->reassignParent($node);
50
51 4
        return $this;
52
    }
53
54
    /**
55
     * @param string       $key
56
     * @param AbstractNode $node
57
     * @param bool         $allowDefault
58
     *
59
     * @return QueryBuilderInterface
60
     *
61
     * @throws \Exception
62
     */
63 17 View Code Duplication
    public function addToObjectNode(string $key, AbstractNode $node, bool $allowDefault = false): QueryBuilderInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65 17
        if (!$this->node instanceof ObjectNode) {
66 1
            throw new \Exception(sprintf('You cannot call %s on node type: %s', __FUNCTION__, get_class($this->node)));
67
        }
68
69 17
        $this->node->add($key, $node, $allowDefault);
70 17
        $this->reassignParent($node);
71
72 17
        return $this;
73
    }
74
75
    /**
76
     * @param AbstractNode $node
77
     */
78 17
    private function reassignParent(AbstractNode $node)
79
    {
80 17
        if ($node instanceof ArrayNode || $node instanceof ObjectNode) {
81 17
            $this->node = $node;
82
        }
83 17
    }
84
85
    /**
86
     * @return QueryBuilderInterface
87
     *
88
     * @throws \Exception
89
     */
90 2
    public function end(): QueryBuilderInterface
91
    {
92 2
        if (null === $this->node = $this->node->getParent()) {
93 1
            throw new \Exception(sprintf('You cannot call %s on main node', __FUNCTION__));
94
        }
95
96 1
        return $this;
97
    }
98
99
    /**
100
     * @return ArrayNode
101
     */
102 5
    public function arrayNode(): ArrayNode
103
    {
104 5
        return new ArrayNode();
105
    }
106
107
        /**
108
     * @param bool|null
109
     * @return BoolNode
110
     */
111 1
    public function boolNode($value = null): BoolNode
112
    {
113 1
        return new BoolNode($value);
114
    }
115
116
    /**
117
     * @param float|null
118
     * @return FloatNode
119
     */
120 1
    public function floatNode($value = null): FloatNode
121
    {
122 1
        return new FloatNode($value);
123
    }
124
125
    /**
126
     * @param int|null
127
     * @return IntNode
128
     */
129 4
    public function intNode($value = null): IntNode
130
    {
131 4
        return new IntNode($value);
132
    }
133
134
    /**
135
     * @return ObjectNode
136
     */
137 16
    public function objectNode(): ObjectNode
138
    {
139 16
        return new ObjectNode();
140
    }
141
142
    /**
143
     * @deprecated use boolNode|floatNode|intNode|stringNode
144
     * @param string|float|int|bool|null $value
145
     * @return ScalarNode
146
     */
147 1
    public function scalarNode($value = null): ScalarNode
148
    {
149 1
        return new ScalarNode($value);
0 ignored issues
show
Deprecated Code introduced by
The class Saxulum\ElasticSearchQueryBuilder\Node\ScalarNode has been deprecated with message: use BoolNode|FloatNode|IntNode|StringNode

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
150
    }
151
152
    /**
153
     * @param string|null
154
     * @return StringNode
155
     */
156 13
    public function stringNode($value = null): StringNode
157
    {
158 13
        return new StringNode($value);
159
    }
160
161
    /**
162
     * @return \stdClass|null
163
     */
164 16
    public function serialize()
165
    {
166 16
        return $this->rootNode->serialize();
167
    }
168
169
    /**
170
     * @param boolean $beautify
171
     * @return string
172
     */
173 16
    public function json(bool $beautify = false): string
174
    {
175 16
        if (null === $serialized = $this->serialize()) {
176 2
            return '';
177
        }
178
179 14
        if ($beautify) {
180 1
            return json_encode($serialized, JSON_PRETTY_PRINT);
181
        }
182
183 13
        return json_encode($serialized);
184
    }
185
}
186