Passed
Branch master (8cb5b2)
by Christopher
04:37
created

Compiler::compileText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 1
dl 0
loc 21
ccs 20
cts 20
cp 1
crap 1
rs 9.6333
c 0
b 0
f 0
1
<?php
2
namespace Triadev\Leopard\Business\Mapping;
3
4
use Illuminate\Support\Fluent;
5
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([
17 11
            'type' => 'text',
18 11
            'analyzer' => $fluent->analyzer,
19 11
            'boost' => $fluent->boost,
20 11
            'eager_global_ordinals' => $fluent->eager_global_ordinals,
21 11
            'fielddata' => $fluent->fielddata,
22 11
            'fielddata_frequency_filter' => $fluent->fielddata_frequency_filter,
23 11
            'fields' => $fluent->fields,
24 11
            'index' => $fluent->index,
25 11
            'index_options' => $fluent->index_options,
26 11
            'index_prefixes' => $fluent->index_prefixes,
27 11
            'index_phrases' => $fluent->index_phrases,
28 11
            'norms' => $fluent->norms,
29 11
            'position_increment_gap' => $fluent->position_increment_gap,
30 11
            'store' => $fluent->store,
31 11
            'search_analyzer' => $fluent->search_analyzer,
32 11
            'search_quote_analyzer' => $fluent->search_quote_analyzer,
33 11
            'similarity' => $fluent->similarity,
34 11
            'term_vector' => $fluent->term_vector
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([
47 12
            'type' => 'keyword',
48 12
            'boost' => $fluent->boost,
49 12
            'doc_values' => $fluent->doc_values,
50 12
            'eager_global_ordinals' => $fluent->eager_global_ordinals,
51 12
            'fields' => $fluent->fields,
52 12
            'ignore_above' => $fluent->ignore_above,
53 12
            'index' => $fluent->index,
54 12
            'index_options' => $fluent->index_options,
55 12
            'norms' => $fluent->norms,
56 12
            'null_value' => $fluent->null_value,
57 12
            'store' => $fluent->store,
58 12
            'similarity' => $fluent->similarity,
59 12
            'normalizer' => $fluent->normalizer,
60 12
            'split_queries_on_whitespace' => $fluent->split_queries_on_whitespace
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([
161 17
            'type' => $fluent->type,
162 17
            'coerce' => $fluent->coerce,
163 17
            'boost' => $fluent->boost,
164 17
            'doc_values' => $fluent->doc_values,
165 17
            'ignore_malformed' => $fluent->ignore_malformed,
166 17
            'index' => $fluent->index,
167 17
            'null_value' => $fluent->null_value,
168 17
            'store' => $fluent->store
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([
181 2
            'type' => 'date',
182 2
            'boost' => $fluent->boost,
183 2
            'doc_values' => $fluent->doc_values,
184 2
            'format' => $fluent->format,
185 2
            'locale' => $fluent->locale,
186 2
            'ignore_malformed' => $fluent->ignore_malformed,
187 2
            'index' => $fluent->index,
188 2
            'null_value' => $fluent->null_value,
189 2
            'store' => $fluent->store
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([
202 2
            'type' => 'boolean',
203 2
            'boost' => $fluent->boost,
204 2
            'doc_values' => $fluent->doc_values,
205 2
            'index' => $fluent->index,
206 2
            'null_value' => $fluent->null_value,
207 2
            'store' => $fluent->store
208
        ]);
209
    }
210
    
211
    /**
212
     * Compile: binary
213
     *
214
     * @param Fluent $fluent
215
     * @return array
216
     */
217 2
    public function compileBinary(Fluent $fluent) : array
218
    {
219 2
        return $this->formatAttributes([
220 2
            'type' => 'binary',
221 2
            'doc_values' => $fluent->doc_values,
222 2
            'store' => $fluent->store
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
299
    {
300 7
        return $this->formatAttributes([
301 7
            'type' => $fluent->type,
302 7
            'coerce' => $fluent->coerce,
303 7
            'boost' => $fluent->boost,
304 7
            'index' => $fluent->index,
305 7
            'store' => $fluent->store
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([
318 2
            'type' => 'nested',
319 2
            'dynamic' => $fluent->dynamic,
320 2
            'properties' => $this->compileFields(
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([
337 2
            'type' => 'nested',
338 2
            'dynamic' => $fluent->dynamic,
339 2
            'enabled' => $fluent->enabled,
340 2
            'properties' => $this->compileFields(
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([
371 2
            'type' => 'geo_point',
372 2
            'ignore_malformed' => $fluent->ignore_malformed,
373 2
            'ignore_z_value' => $fluent->ignore_z_value,
374 2
            'null_value' => $fluent->null_value
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([
387 2
            'type' => 'geo_shape',
388 2
            'tree' => $fluent->tree,
389 2
            'precision' => $fluent->precision,
390 2
            'tree_levels' => $fluent->tree_levels,
391 2
            'strategy' => $fluent->strategy,
392 2
            'distance_error_pct' => $fluent->distance_error_pct,
393 2
            'orientation' => $fluent->orientation,
394 2
            'points_only' => $fluent->points_only,
395 2
            'ignore_malformed' => $fluent->ignore_malformed,
396 2
            'ignore_z_value' => $fluent->ignore_z_value
397
        ]);
398
    }
399
    
400
    /**
401
     * Compile: ip
402
     *
403
     * @param Fluent $fluent
404
     * @return array
405
     */
406 2
    public function compileIp(Fluent $fluent) : array
407
    {
408 2
        return $this->formatAttributes([
409 2
            'type' => 'ip',
410 2
            'boost' => $fluent->boost,
411 2
            'doc_values' => $fluent->doc_values,
412 2
            'index' => $fluent->index,
413 2
            'null_value' => $fluent->null_value,
414 2
            'store' => $fluent->store
415
        ]);
416
    }
417
    
418
    /**
419
     * Compile: completion
420
     *
421
     * @param Fluent $fluent
422
     * @return array
423
     */
424 2
    public function compileCompletion(Fluent $fluent) : array
425
    {
426 2
        return $this->formatAttributes([
427 2
            'type' => 'completion',
428 2
            'analyzer' => $fluent->analyzer,
429 2
            'search_analyzer' => $fluent->search_analyzer,
430 2
            'preserve_separators' => $fluent->preserve_separators,
431 2
            'preserve_position_increments' => $fluent->preserve_position_increments,
432 2
            'max_input_length' => $fluent->max_input_length
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([
445 2
            'type' => 'token_count',
446 2
            'analyzer' => $fluent->analyzer,
447 2
            'enable_position_increments' => $fluent->enable_position_increments,
448 2
            'boost' => $fluent->boost,
449 2
            'doc_values' => $fluent->doc_values,
450 2
            'index' => $fluent->index,
451 2
            'null_value' => $fluent->null_value,
452 2
            'store' => $fluent->store
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
484
    {
485 12
        return str_replace($separator, '', ucwords($input, $separator));
486
    }
487
}
488