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

Blueprint   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 348
Duplicated Lines 0 %

Test Coverage

Coverage 98.48%

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 348
ccs 65
cts 66
cp 0.9848
rs 10
c 0
b 0
f 0
wmc 28

26 Methods

Rating   Name   Duplication   Size   Complexity  
A long() 0 3 1
A addField() 0 13 1
A integer() 0 3 1
A nested() 0 3 1
A byte() 0 3 1
A geoShape() 0 3 1
A double() 0 3 1
A keyword() 0 3 1
A ip() 0 3 1
A __construct() 0 4 2
A halfFloat() 0 3 1
A boolean() 0 3 1
A toDsl() 0 3 1
A object() 0 3 1
A date() 0 3 1
A getFields() 0 3 1
A binary() 0 3 1
A geoPoint() 0 3 1
A scaledFloat() 0 3 1
A completion() 0 3 1
A float() 0 3 1
A tokenCount() 0 3 1
A range() 0 16 2
A text() 0 3 1
A short() 0 3 1
A build() 0 11 1
1
<?php
2
namespace Triadev\Leopard\Business\Mapping;
3
4
use Illuminate\Support\Fluent;
5
use Triadev\Leopard\Facade\Leopard;
6
7
class Blueprint
8
{
9
    /** @var Fluent[] */
10
    private $fields = [];
11
    
12
    /**
13
     * Blueprint constructor.
14
     * @param \Closure|null $callback
15
     */
16 13
    public function __construct(\Closure $callback = null)
17
    {
18 13
        if (!is_null($callback)) {
19
            $callback($this);
20
        }
21 13
    }
22
    
23
    /**
24
     * Build mapping
25
     *
26
     * @param string $index
27
     * @param string $type
28
     * @return array
29
     */
30 8
    public function build(string $index, string $type) : array
31
    {
32 8
        return Leopard::putMappingStatement([
33 8
            'index' => $index,
34 8
            'type' => $type,
35
            'body' => [
36
                $type => [
37
                    '_source' => [
38
                        'enabled' => true
39
                    ],
40 8
                    'properties' => $this->toDsl()
41
                ]
42
            ]
43
        ]);
44
    }
45
    
46
    /**
47
     * To dsl
48
     *
49
     * @return array
50
     */
51 10
    public function toDsl() : array
52
    {
53 10
        return (new Compiler())->compileFields($this->fields);
54
    }
55
    
56
    /**
57
     * Get fields
58
     *
59
     * @return array
60
     */
61 3
    public function getFields() : array
62
    {
63 3
        return $this->fields;
64
    }
65
    
66
    /**
67
     * Text
68
     *
69
     * @param string $field
70
     * @param array $attributes
71
     * @return Fluent
72
     */
73 10
    public function text(string $field, array $attributes = []) : Fluent
74
    {
75 10
        return $this->addField('text', $field, $attributes);
76
    }
77
    
78
    /**
79
     * Keyword
80
     *
81
     * @param string $field
82
     * @param array $attributes
83
     * @return Fluent
84
     */
85 11
    public function keyword(string $field, array $attributes = []) : Fluent
86
    {
87 11
        return $this->addField('keyword', $field, $attributes);
88
    }
89
    
90
    /**
91
     * Long
92
     *
93
     * @param string $field
94
     * @param array $attributes
95
     * @return Fluent
96
     */
97 1
    public function long(string $field, array $attributes = []) : Fluent
98
    {
99 1
        return $this->addField('long', $field, $attributes);
100
    }
101
    
102
    /**
103
     * Integer
104
     *
105
     * @param string $field
106
     * @param array $attributes
107
     * @return Fluent
108
     */
109 9
    public function integer(string $field, array $attributes = []) : Fluent
110
    {
111 9
        return $this->addField('integer', $field, $attributes);
112
    }
113
    
114
    /**
115
     * Short
116
     *
117
     * @param string $field
118
     * @param array $attributes
119
     * @return Fluent
120
     */
121 1
    public function short(string $field, array $attributes = []) : Fluent
122
    {
123 1
        return $this->addField('short', $field, $attributes);
124
    }
125
    
126
    /**
127
     * Byte
128
     *
129
     * @param string $field
130
     * @param array $attributes
131
     * @return Fluent
132
     */
133 1
    public function byte(string $field, array $attributes = []) : Fluent
134
    {
135 1
        return $this->addField('byte', $field, $attributes);
136
    }
137
    
138
    /**
139
     * Double
140
     *
141
     * @param string $field
142
     * @param array $attributes
143
     * @return Fluent
144
     */
145 1
    public function double(string $field, array $attributes = []) : Fluent
146
    {
147 1
        return $this->addField('double', $field, $attributes);
148
    }
149
    
150
    /**
151
     * Float
152
     *
153
     * @param string $field
154
     * @param array $attributes
155
     * @return Fluent
156
     */
157 1
    public function float(string $field, array $attributes = []) : Fluent
158
    {
159 1
        return $this->addField('float', $field, $attributes);
160
    }
161
    
162
    /**
163
     * Half float
164
     *
165
     * @param string $field
166
     * @param array $attributes
167
     * @return Fluent
168
     */
169 1
    public function halfFloat(string $field, array $attributes = []) : Fluent
170
    {
171 1
        return $this->addField('half_float', $field, $attributes);
172
    }
173
    
174
    /**
175
     * Scaled float
176
     *
177
     * @param string $field
178
     * @param array $attributes
179
     * @return Fluent
180
     */
181 1
    public function scaledFloat(string $field, array $attributes = []) : Fluent
182
    {
183 1
        return $this->addField('scaled_float', $field, $attributes);
184
    }
185
    
186
    /**
187
     * Date
188
     *
189
     * @param string $field
190
     * @param array $attributes
191
     * @return Fluent
192
     */
193 1
    public function date(string $field, array $attributes = []) : Fluent
194
    {
195 1
        return $this->addField('date', $field, $attributes);
196
    }
197
    
198
    /**
199
     * Boolean
200
     *
201
     * @param string $field
202
     * @param array $attributes
203
     * @return Fluent
204
     */
205 1
    public function boolean(string $field, array $attributes = []) : Fluent
206
    {
207 1
        return $this->addField('boolean', $field, $attributes);
208
    }
209
    
210
    /**
211
     * Binary
212
     *
213
     * @param string $field
214
     * @param array $attributes
215
     * @return Fluent
216
     */
217 1
    public function binary(string $field, array $attributes = []) : Fluent
218
    {
219 1
        return $this->addField('binary', $field, $attributes);
220
    }
221
    
222
    /**
223
     * Range
224
     *
225
     * @param string $field
226
     * @param string $type
227
     * @param array $attributes
228
     * @return Fluent
229
     *
230
     * @throws \InvalidArgumentException
231
     */
232 2
    public function range(string $field, string $type, array $attributes = []) : Fluent
233
    {
234
        $validTypes = [
235 2
            'integer',
236
            'float',
237
            'long',
238
            'double',
239
            'date',
240
            'ip'
241
        ];
242
        
243 2
        if (!in_array($type, $validTypes)) {
244 1
            throw new \InvalidArgumentException();
245
        }
246
        
247 1
        return $this->addField(strtolower($type) . '_range', $field, $attributes);
248
    }
249
    
250
    /**
251
     * Nested
252
     *
253
     * @param string $field
254
     * @param array $attributes
255
     * @return Fluent
256
     */
257 1
    public function nested(string $field, array $attributes = []) : Fluent
258
    {
259 1
        return $this->addField('nested', $field, $attributes);
260
    }
261
    
262
    /**
263
     * Object
264
     *
265
     * @param string $field
266
     * @param array $attributes
267
     * @return Fluent
268
     */
269 1
    public function object(string $field, array $attributes = []) : Fluent
270
    {
271 1
        return $this->addField('object', $field, $attributes);
272
    }
273
    
274
    /**
275
     * Geo point
276
     *
277
     * @param string $field
278
     * @param array $attributes
279
     * @return Fluent
280
     */
281 1
    public function geoPoint(string $field, array $attributes = []) : Fluent
282
    {
283 1
        return $this->addField('geo_point', $field, $attributes);
284
    }
285
    
286
    /**
287
     * Geo shape
288
     *
289
     * @param string $field
290
     * @param array $attributes
291
     * @return Fluent
292
     */
293 1
    public function geoShape(string $field, array $attributes = []) : Fluent
294
    {
295 1
        return $this->addField('geo_shape', $field, $attributes);
296
    }
297
    
298
    /**
299
     * Ip
300
     *
301
     * @param string $field
302
     * @param array $attributes
303
     * @return Fluent
304
     */
305 1
    public function ip(string $field, array $attributes = []) : Fluent
306
    {
307 1
        return $this->addField('ip', $field, $attributes);
308
    }
309
    
310
    /**
311
     * Completion
312
     *
313
     * @param string $field
314
     * @param array $attributes
315
     * @return Fluent
316
     */
317 1
    public function completion(string $field, array $attributes = []) : Fluent
318
    {
319 1
        return $this->addField('completion', $field, $attributes);
320
    }
321
    
322
    /**
323
     * Token count
324
     *
325
     * @param string $field
326
     * @param array $attributes
327
     * @return Fluent
328
     */
329 1
    public function tokenCount(string $field, array $attributes = []) : Fluent
330
    {
331 1
        return $this->addField('token_count', $field, $attributes);
332
    }
333
    
334
    /**
335
     * Add field
336
     *
337
     * @param string $type
338
     * @param string $name
339
     * @param array $attributes
340
     * @return Fluent
341
     */
342 12
    public function addField(string $type, string $name, array $attributes = []) : Fluent
343
    {
344 12
        $this->fields[] = $field = new Fluent(
345 12
            array_merge(
346 12
                compact(
347 12
                    'type',
348 12
                    'name'
349
                ),
350 12
                $attributes
351
            )
352
        );
353
        
354 12
        return $field;
355
    }
356
}
357