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\NullNode; |
13
|
|
|
use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode; |
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 array ...$arguments |
36
|
|
|
* @return QueryBuilderInterface |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
16 |
|
public function add(...$arguments): QueryBuilderInterface |
40
|
|
|
{ |
41
|
16 |
|
if ($this->node instanceof ObjectNode) { |
42
|
16 |
|
return $this->addToObjectNode(...$arguments); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
4 |
|
return $this->addToArrayNode(...$arguments); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param AbstractNode $node |
50
|
|
|
* |
51
|
|
|
* @return QueryBuilderInterface |
52
|
|
|
* |
53
|
|
|
* @throws \Exception |
54
|
|
|
*/ |
55
|
5 |
View Code Duplication |
public function addToArrayNode(AbstractNode $node): QueryBuilderInterface |
|
|
|
|
56
|
|
|
{ |
57
|
5 |
|
if (!$this->node instanceof ArrayNode) { |
58
|
1 |
|
throw new \Exception(sprintf('You cannot call %s on node type: %s', __FUNCTION__, get_class($this->node))); |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
$this->node->add($node); |
62
|
4 |
|
$this->reassignParent($node); |
63
|
|
|
|
64
|
4 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $key |
69
|
|
|
* @param AbstractNode $node |
70
|
|
|
* |
71
|
|
|
* @return QueryBuilderInterface |
72
|
|
|
* |
73
|
|
|
* @throws \Exception |
74
|
|
|
*/ |
75
|
17 |
View Code Duplication |
public function addToObjectNode(string $key, AbstractNode $node): QueryBuilderInterface |
|
|
|
|
76
|
|
|
{ |
77
|
17 |
|
if (!$this->node instanceof ObjectNode) { |
78
|
1 |
|
throw new \Exception(sprintf('You cannot call %s on node type: %s', __FUNCTION__, get_class($this->node))); |
79
|
|
|
} |
80
|
|
|
|
81
|
17 |
|
$this->node->add($key, $node); |
82
|
17 |
|
$this->reassignParent($node); |
83
|
|
|
|
84
|
17 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param AbstractNode $node |
89
|
|
|
*/ |
90
|
17 |
|
private function reassignParent(AbstractNode $node) |
91
|
|
|
{ |
92
|
17 |
|
if ($node instanceof ArrayNode || $node instanceof ObjectNode) { |
93
|
17 |
|
$this->node = $node; |
94
|
|
|
} |
95
|
17 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return QueryBuilderInterface |
99
|
|
|
* |
100
|
|
|
* @throws \Exception |
101
|
|
|
*/ |
102
|
2 |
|
public function end(): QueryBuilderInterface |
103
|
|
|
{ |
104
|
2 |
|
if (null === $this->node = $this->node->getParent()) { |
105
|
1 |
|
throw new \Exception(sprintf('You cannot call %s on main node', __FUNCTION__)); |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param bool $allowDefault |
113
|
|
|
* @return ArrayNode |
114
|
|
|
*/ |
115
|
5 |
|
public function arrayNode(bool $allowDefault = false): ArrayNode |
116
|
|
|
{ |
117
|
5 |
|
return new ArrayNode($allowDefault); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param bool|null $value |
122
|
|
|
* @param bool $allowDefault |
123
|
|
|
* @return BoolNode |
124
|
|
|
*/ |
125
|
1 |
|
public function boolNode($value = null, bool $allowDefault = false): BoolNode |
126
|
|
|
{ |
127
|
1 |
|
return new BoolNode($value, $allowDefault); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param float|null $value |
132
|
|
|
* @param bool $allowDefault |
133
|
|
|
* @return FloatNode |
134
|
|
|
*/ |
135
|
1 |
|
public function floatNode($value = null, bool $allowDefault = false): FloatNode |
136
|
|
|
{ |
137
|
1 |
|
return new FloatNode($value, $allowDefault); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param int|null $value |
142
|
|
|
* @param bool $allowDefault |
143
|
|
|
* @return IntNode |
144
|
|
|
*/ |
145
|
4 |
|
public function intNode($value = null, bool $allowDefault = false): IntNode |
146
|
|
|
{ |
147
|
4 |
|
return new IntNode($value, $allowDefault); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return NullNode |
152
|
|
|
*/ |
153
|
1 |
|
public function nullNode(): NullNode |
154
|
|
|
{ |
155
|
1 |
|
return new NullNode; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param bool $allowDefault |
160
|
|
|
* @return ObjectNode |
161
|
|
|
*/ |
162
|
16 |
|
public function objectNode(bool $allowDefault = false): ObjectNode |
163
|
|
|
{ |
164
|
16 |
|
return new ObjectNode($allowDefault); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string|null $value |
169
|
|
|
* @param bool $allowDefault |
170
|
|
|
* @return StringNode |
171
|
|
|
*/ |
172
|
13 |
|
public function stringNode($value = null, bool $allowDefault = false): StringNode |
173
|
|
|
{ |
174
|
13 |
|
return new StringNode($value, $allowDefault); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return \stdClass|null |
179
|
|
|
*/ |
180
|
16 |
|
public function serialize() |
181
|
|
|
{ |
182
|
16 |
|
return $this->rootNode->serialize(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param boolean $beautify |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
16 |
|
public function json(bool $beautify = false): string |
190
|
|
|
{ |
191
|
16 |
|
if (null === $serialized = $this->serialize()) { |
192
|
2 |
|
return ''; |
193
|
|
|
} |
194
|
|
|
|
195
|
14 |
|
if ($beautify) { |
196
|
1 |
|
return json_encode($serialized, JSON_PRETTY_PRINT); |
197
|
|
|
} |
198
|
|
|
|
199
|
13 |
|
return json_encode($serialized); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
This check looks for function calls that miss required arguments.