Total Complexity | 37 |
Total Lines | 480 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
6 | class Compiler |
||
7 | { |
||
8 | /** |
||
9 | * Compile: text |
||
10 | * |
||
11 | * @param Fluent $fluent |
||
12 | * @return array |
||
13 | */ |
||
14 | 11 | public function compileText(Fluent $fluent) : array |
|
15 | { |
||
16 | 11 | return $this->formatAttributes([ |
|
1 ignored issue
–
show
|
|||
17 | 11 | 'type' => 'text', |
|
1 ignored issue
–
show
|
|||
18 | 11 | 'analyzer' => $fluent->analyzer, |
|
1 ignored issue
–
show
|
|||
19 | 11 | 'boost' => $fluent->boost, |
|
1 ignored issue
–
show
|
|||
20 | 11 | 'eager_global_ordinals' => $fluent->eager_global_ordinals, |
|
1 ignored issue
–
show
|
|||
21 | 11 | 'fielddata' => $fluent->fielddata, |
|
1 ignored issue
–
show
|
|||
22 | 11 | 'fielddata_frequency_filter' => $fluent->fielddata_frequency_filter, |
|
1 ignored issue
–
show
|
|||
23 | 11 | 'fields' => $fluent->fields, |
|
1 ignored issue
–
show
|
|||
24 | 11 | 'index' => $fluent->index, |
|
1 ignored issue
–
show
|
|||
25 | 11 | 'index_options' => $fluent->index_options, |
|
1 ignored issue
–
show
|
|||
26 | 11 | 'index_prefixes' => $fluent->index_prefixes, |
|
1 ignored issue
–
show
|
|||
27 | 11 | 'index_phrases' => $fluent->index_phrases, |
|
1 ignored issue
–
show
|
|||
28 | 11 | 'norms' => $fluent->norms, |
|
1 ignored issue
–
show
|
|||
29 | 11 | 'position_increment_gap' => $fluent->position_increment_gap, |
|
1 ignored issue
–
show
|
|||
30 | 11 | 'store' => $fluent->store, |
|
1 ignored issue
–
show
|
|||
31 | 11 | 'search_analyzer' => $fluent->search_analyzer, |
|
1 ignored issue
–
show
|
|||
32 | 11 | 'search_quote_analyzer' => $fluent->search_quote_analyzer, |
|
1 ignored issue
–
show
|
|||
33 | 11 | 'similarity' => $fluent->similarity, |
|
1 ignored issue
–
show
|
|||
34 | 11 | 'term_vector' => $fluent->term_vector |
|
1 ignored issue
–
show
|
|||
35 | ]); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Compile: keyword |
||
40 | * |
||
41 | * @param Fluent $fluent |
||
42 | * @return array |
||
43 | */ |
||
44 | 12 | public function compileKeyword(Fluent $fluent) : array |
|
45 | { |
||
46 | 12 | return $this->formatAttributes([ |
|
1 ignored issue
–
show
|
|||
47 | 12 | 'type' => 'keyword', |
|
1 ignored issue
–
show
|
|||
48 | 12 | 'boost' => $fluent->boost, |
|
1 ignored issue
–
show
|
|||
49 | 12 | 'doc_values' => $fluent->doc_values, |
|
1 ignored issue
–
show
|
|||
50 | 12 | 'eager_global_ordinals' => $fluent->eager_global_ordinals, |
|
1 ignored issue
–
show
|
|||
51 | 12 | 'fields' => $fluent->fields, |
|
1 ignored issue
–
show
|
|||
52 | 12 | 'ignore_above' => $fluent->ignore_above, |
|
1 ignored issue
–
show
|
|||
53 | 12 | 'index' => $fluent->index, |
|
1 ignored issue
–
show
|
|||
54 | 12 | 'index_options' => $fluent->index_options, |
|
1 ignored issue
–
show
|
|||
55 | 12 | 'norms' => $fluent->norms, |
|
1 ignored issue
–
show
|
|||
56 | 12 | 'null_value' => $fluent->null_value, |
|
1 ignored issue
–
show
|
|||
57 | 12 | 'store' => $fluent->store, |
|
1 ignored issue
–
show
|
|||
58 | 12 | 'similarity' => $fluent->similarity, |
|
1 ignored issue
–
show
|
|||
59 | 12 | 'normalizer' => $fluent->normalizer, |
|
1 ignored issue
–
show
|
|||
60 | 12 | 'split_queries_on_whitespace' => $fluent->split_queries_on_whitespace |
|
1 ignored issue
–
show
|
|||
61 | ]); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Compile: long |
||
66 | * |
||
67 | * @param Fluent $fluent |
||
68 | * @return array |
||
69 | */ |
||
70 | 2 | public function compileLong(Fluent $fluent) : array |
|
71 | { |
||
72 | 2 | return $this->compileNumeric($fluent); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Compile: integer |
||
77 | * |
||
78 | * @param Fluent $fluent |
||
79 | * @return array |
||
80 | */ |
||
81 | 10 | public function compileInteger(Fluent $fluent) : array |
|
82 | { |
||
83 | 10 | return $this->compileNumeric($fluent); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Compile: short |
||
88 | * |
||
89 | * @param Fluent $fluent |
||
90 | * @return array |
||
91 | */ |
||
92 | 2 | public function compileShort(Fluent $fluent) : array |
|
93 | { |
||
94 | 2 | return $this->compileNumeric($fluent); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Compile: byte |
||
99 | * |
||
100 | * @param Fluent $fluent |
||
101 | * @return array |
||
102 | */ |
||
103 | 2 | public function compileByte(Fluent $fluent) : array |
|
104 | { |
||
105 | 2 | return $this->compileNumeric($fluent); |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Compile: double |
||
110 | * |
||
111 | * @param Fluent $fluent |
||
112 | * @return array |
||
113 | */ |
||
114 | 2 | public function compileDouble(Fluent $fluent) : array |
|
115 | { |
||
116 | 2 | return $this->compileNumeric($fluent); |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Compile: float |
||
121 | * |
||
122 | * @param Fluent $fluent |
||
123 | * @return array |
||
124 | */ |
||
125 | 2 | public function compileFloat(Fluent $fluent) : array |
|
126 | { |
||
127 | 2 | return $this->compileNumeric($fluent); |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Compile: half float |
||
132 | * |
||
133 | * @param Fluent $fluent |
||
134 | * @return array |
||
135 | */ |
||
136 | 2 | public function compileHalfFloat(Fluent $fluent) : array |
|
137 | { |
||
138 | 2 | return $this->compileNumeric($fluent); |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Compile: scaled float |
||
143 | * |
||
144 | * @param Fluent $fluent |
||
145 | * @return array |
||
146 | */ |
||
147 | 2 | public function compileScaledFloat(Fluent $fluent) : array |
|
148 | { |
||
149 | 2 | return $this->compileNumeric($fluent); |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * Compile: numeric |
||
154 | * |
||
155 | * @param Fluent $fluent |
||
156 | * @return array |
||
157 | */ |
||
158 | 17 | public function compileNumeric(Fluent $fluent) : array |
|
159 | { |
||
160 | 17 | return $this->formatAttributes([ |
|
1 ignored issue
–
show
|
|||
161 | 17 | 'type' => $fluent->type, |
|
1 ignored issue
–
show
|
|||
162 | 17 | 'coerce' => $fluent->coerce, |
|
1 ignored issue
–
show
|
|||
163 | 17 | 'boost' => $fluent->boost, |
|
1 ignored issue
–
show
|
|||
164 | 17 | 'doc_values' => $fluent->doc_values, |
|
1 ignored issue
–
show
|
|||
165 | 17 | 'ignore_malformed' => $fluent->ignore_malformed, |
|
1 ignored issue
–
show
|
|||
166 | 17 | 'index' => $fluent->index, |
|
1 ignored issue
–
show
|
|||
167 | 17 | 'null_value' => $fluent->null_value, |
|
1 ignored issue
–
show
|
|||
168 | 17 | 'store' => $fluent->store |
|
1 ignored issue
–
show
|
|||
169 | ]); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Compile: date |
||
174 | * |
||
175 | * @param Fluent $fluent |
||
176 | * @return array |
||
177 | */ |
||
178 | 2 | public function compileDate(Fluent $fluent) : array |
|
179 | { |
||
180 | 2 | return $this->formatAttributes([ |
|
1 ignored issue
–
show
|
|||
181 | 2 | 'type' => 'date', |
|
1 ignored issue
–
show
|
|||
182 | 2 | 'boost' => $fluent->boost, |
|
1 ignored issue
–
show
|
|||
183 | 2 | 'doc_values' => $fluent->doc_values, |
|
1 ignored issue
–
show
|
|||
184 | 2 | 'format' => $fluent->format, |
|
1 ignored issue
–
show
|
|||
185 | 2 | 'locale' => $fluent->locale, |
|
1 ignored issue
–
show
|
|||
186 | 2 | 'ignore_malformed' => $fluent->ignore_malformed, |
|
1 ignored issue
–
show
|
|||
187 | 2 | 'index' => $fluent->index, |
|
1 ignored issue
–
show
|
|||
188 | 2 | 'null_value' => $fluent->null_value, |
|
1 ignored issue
–
show
|
|||
189 | 2 | 'store' => $fluent->store |
|
1 ignored issue
–
show
|
|||
190 | ]); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Compile: boolean |
||
195 | * |
||
196 | * @param Fluent $fluent |
||
197 | * @return array |
||
198 | */ |
||
199 | 2 | public function compileBoolean(Fluent $fluent) : array |
|
200 | { |
||
201 | 2 | return $this->formatAttributes([ |
|
1 ignored issue
–
show
|
|||
202 | 2 | 'type' => 'boolean', |
|
1 ignored issue
–
show
|
|||
203 | 2 | 'boost' => $fluent->boost, |
|
1 ignored issue
–
show
|
|||
204 | 2 | 'doc_values' => $fluent->doc_values, |
|
1 ignored issue
–
show
|
|||
205 | 2 | 'index' => $fluent->index, |
|
1 ignored issue
–
show
|
|||
206 | 2 | 'null_value' => $fluent->null_value, |
|
1 ignored issue
–
show
|
|||
207 | 2 | 'store' => $fluent->store |
|
1 ignored issue
–
show
|
|||
208 | ]); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Compile: binary |
||
213 | * |
||
214 | * @param Fluent $fluent |
||
215 | * @return array |
||
216 | */ |
||
217 | 2 | public function compileBinary(Fluent $fluent) : array |
|
223 | ]); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Compile: integer range |
||
228 | * |
||
229 | * @param Fluent $fluent |
||
230 | * @return array |
||
231 | */ |
||
232 | 4 | public function compileIntegerRange(Fluent $fluent) : array |
|
233 | { |
||
234 | 4 | return $this->compileRange($fluent); |
|
235 | } |
||
236 | |||
237 | /** |
||
238 | * Compile: float range |
||
239 | * |
||
240 | * @param Fluent $fluent |
||
241 | * @return array |
||
242 | */ |
||
243 | 2 | public function compileFloatRange(Fluent $fluent) : array |
|
244 | { |
||
245 | 2 | return $this->compileRange($fluent); |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * Compile: long range |
||
250 | * |
||
251 | * @param Fluent $fluent |
||
252 | * @return array |
||
253 | */ |
||
254 | 2 | public function compileLongRange(Fluent $fluent) : array |
|
255 | { |
||
256 | 2 | return $this->compileRange($fluent); |
|
257 | } |
||
258 | |||
259 | /** |
||
260 | * Compile: double range |
||
261 | * |
||
262 | * @param Fluent $fluent |
||
263 | * @return array |
||
264 | */ |
||
265 | 2 | public function compileDoubleRange(Fluent $fluent) : array |
|
266 | { |
||
267 | 2 | return $this->compileRange($fluent); |
|
268 | } |
||
269 | |||
270 | /** |
||
271 | * Compile: date range |
||
272 | * |
||
273 | * @param Fluent $fluent |
||
274 | * @return array |
||
275 | */ |
||
276 | 1 | public function compileDateRange(Fluent $fluent) : array |
|
277 | { |
||
278 | 1 | return $this->compileRange($fluent); |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * Compile: ip range |
||
283 | * |
||
284 | * @param Fluent $fluent |
||
285 | * @return array |
||
286 | */ |
||
287 | 1 | public function compileIpRange(Fluent $fluent) : array |
|
288 | { |
||
289 | 1 | return $this->compileRange($fluent); |
|
290 | } |
||
291 | |||
292 | /** |
||
293 | * Compile: range |
||
294 | * |
||
295 | * @param Fluent $fluent |
||
296 | * @return array |
||
297 | */ |
||
298 | 7 | public function compileRange(Fluent $fluent) : array |
|
306 | ]); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Compile: nested |
||
311 | * |
||
312 | * @param Fluent $fluent |
||
313 | * @return array |
||
314 | */ |
||
315 | 2 | public function compileNested(Fluent $fluent) : array |
|
316 | { |
||
317 | 2 | return $this->formatAttributes([ |
|
1 ignored issue
–
show
|
|||
318 | 2 | 'type' => 'nested', |
|
1 ignored issue
–
show
|
|||
319 | 2 | 'dynamic' => $fluent->dynamic, |
|
1 ignored issue
–
show
|
|||
320 | 2 | 'properties' => $this->compileFields( |
|
1 ignored issue
–
show
|
|||
321 | 2 | $this->blueprintCallback( |
|
322 | 2 | $fluent |
|
323 | 2 | )->getFields() |
|
324 | ) |
||
325 | ]); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Compile: object |
||
330 | * |
||
331 | * @param Fluent $fluent |
||
332 | * @return array |
||
333 | */ |
||
334 | 2 | public function compileObject(Fluent $fluent) : array |
|
335 | { |
||
336 | 2 | return $this->formatAttributes([ |
|
1 ignored issue
–
show
|
|||
337 | 2 | 'type' => 'nested', |
|
1 ignored issue
–
show
|
|||
338 | 2 | 'dynamic' => $fluent->dynamic, |
|
1 ignored issue
–
show
|
|||
339 | 2 | 'enabled' => $fluent->enabled, |
|
1 ignored issue
–
show
|
|||
340 | 2 | 'properties' => $this->compileFields( |
|
1 ignored issue
–
show
|
|||
341 | 2 | $this->blueprintCallback( |
|
342 | 2 | $fluent |
|
343 | 2 | )->getFields() |
|
344 | ) |
||
345 | ]); |
||
346 | } |
||
347 | |||
348 | 3 | private function blueprintCallback(Fluent $fluent) : Blueprint |
|
349 | { |
||
350 | 3 | $blueprint = new Blueprint(); |
|
351 | |||
352 | /** @var \Closure $callback */ |
||
353 | 3 | $callback = $fluent->callback; |
|
354 | |||
355 | 3 | if (is_callable($callback)) { |
|
356 | 3 | $callback($blueprint); |
|
357 | } |
||
358 | |||
359 | 3 | return $blueprint; |
|
360 | } |
||
361 | |||
362 | /** |
||
363 | * Compile: geo point |
||
364 | * |
||
365 | * @param Fluent $fluent |
||
366 | * @return array |
||
367 | */ |
||
368 | 2 | public function compileGeoPoint(Fluent $fluent) : array |
|
369 | { |
||
370 | 2 | return $this->formatAttributes([ |
|
1 ignored issue
–
show
|
|||
371 | 2 | 'type' => 'geo_point', |
|
1 ignored issue
–
show
|
|||
372 | 2 | 'ignore_malformed' => $fluent->ignore_malformed, |
|
1 ignored issue
–
show
|
|||
373 | 2 | 'ignore_z_value' => $fluent->ignore_z_value, |
|
1 ignored issue
–
show
|
|||
374 | 2 | 'null_value' => $fluent->null_value |
|
1 ignored issue
–
show
|
|||
375 | ]); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Compile: geo shape |
||
380 | * |
||
381 | * @param Fluent $fluent |
||
382 | * @return array |
||
383 | */ |
||
384 | 2 | public function compileGeoShape(Fluent $fluent) : array |
|
385 | { |
||
386 | 2 | return $this->formatAttributes([ |
|
1 ignored issue
–
show
|
|||
387 | 2 | 'type' => 'geo_shape', |
|
1 ignored issue
–
show
|
|||
388 | 2 | 'tree' => $fluent->tree, |
|
1 ignored issue
–
show
|
|||
389 | 2 | 'precision' => $fluent->precision, |
|
1 ignored issue
–
show
|
|||
390 | 2 | 'tree_levels' => $fluent->tree_levels, |
|
1 ignored issue
–
show
|
|||
391 | 2 | 'strategy' => $fluent->strategy, |
|
1 ignored issue
–
show
|
|||
392 | 2 | 'distance_error_pct' => $fluent->distance_error_pct, |
|
1 ignored issue
–
show
|
|||
393 | 2 | 'orientation' => $fluent->orientation, |
|
1 ignored issue
–
show
|
|||
394 | 2 | 'points_only' => $fluent->points_only, |
|
1 ignored issue
–
show
|
|||
395 | 2 | 'ignore_malformed' => $fluent->ignore_malformed, |
|
1 ignored issue
–
show
|
|||
396 | 2 | 'ignore_z_value' => $fluent->ignore_z_value |
|
1 ignored issue
–
show
|
|||
397 | ]); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Compile: ip |
||
402 | * |
||
403 | * @param Fluent $fluent |
||
404 | * @return array |
||
405 | */ |
||
406 | 2 | public function compileIp(Fluent $fluent) : array |
|
415 | ]); |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Compile: completion |
||
420 | * |
||
421 | * @param Fluent $fluent |
||
422 | * @return array |
||
423 | */ |
||
424 | 2 | public function compileCompletion(Fluent $fluent) : array |
|
433 | ]); |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Compile: token count |
||
438 | * |
||
439 | * @param Fluent $fluent |
||
440 | * @return array |
||
441 | */ |
||
442 | 2 | public function compileTokenCount(Fluent $fluent) : array |
|
443 | { |
||
444 | 2 | return $this->formatAttributes([ |
|
1 ignored issue
–
show
|
|||
445 | 2 | 'type' => 'token_count', |
|
1 ignored issue
–
show
|
|||
446 | 2 | 'analyzer' => $fluent->analyzer, |
|
1 ignored issue
–
show
|
|||
447 | 2 | 'enable_position_increments' => $fluent->enable_position_increments, |
|
1 ignored issue
–
show
|
|||
448 | 2 | 'boost' => $fluent->boost, |
|
1 ignored issue
–
show
|
|||
449 | 2 | 'doc_values' => $fluent->doc_values, |
|
1 ignored issue
–
show
|
|||
450 | 2 | 'index' => $fluent->index, |
|
1 ignored issue
–
show
|
|||
451 | 2 | 'null_value' => $fluent->null_value, |
|
1 ignored issue
–
show
|
|||
452 | 2 | 'store' => $fluent->store |
|
1 ignored issue
–
show
|
|||
453 | ]); |
||
454 | } |
||
455 | |||
456 | 36 | private function formatAttributes(array $attributes) : array |
|
457 | { |
||
458 | 36 | return array_filter($attributes); |
|
459 | } |
||
460 | |||
461 | /** |
||
462 | * Compile fields |
||
463 | * |
||
464 | * @param array $fields |
||
465 | * @return array |
||
466 | */ |
||
467 | 12 | public function compileFields(array $fields) : array |
|
468 | { |
||
469 | 12 | $statements = []; |
|
470 | |||
471 | 12 | foreach ($fields as $field) { |
|
472 | 12 | if ($field instanceof Fluent && !is_null($field->type)) { |
|
473 | 12 | $method = 'compile' . ucfirst($this->camelize($field->type)); |
|
474 | 12 | if (method_exists($this, $method)) { |
|
475 | 12 | $statements[$field->name] = (array)$this->$method($field); |
|
476 | } |
||
477 | } |
||
478 | } |
||
479 | |||
480 | 12 | return $statements; |
|
481 | } |
||
482 | |||
483 | 12 | private function camelize(string $input, string $separator = '_') : string |
|
486 | } |
||
487 | } |
||
488 |