2
2
3
3
namespace Basemkhirat \Elasticsearch ;
4
4
5
- use Basemkhirat \Elasticsearch \Query ;
6
- use Illuminate \Support \Collection ;
5
+ use ArrayAccess ;
7
6
8
7
/**
9
8
* Elasticsearch data model
10
9
* Class Model
11
- *
12
10
* @package Basemkhirat\Elasticsearch
13
11
*/
14
- class Model
12
+ class Model implements ArrayAccess
15
13
{
16
14
17
15
/**
18
16
* Model connection name
19
- *
20
17
* @var string
21
18
*/
22
19
protected $ connection ;
23
20
24
21
/**
25
22
* Model index name
26
- *
27
23
* @var string
28
24
*/
29
25
protected $ index ;
30
26
31
27
/**
32
28
* Model type name
33
- *
34
29
* @var string
35
30
*/
36
31
protected $ type ;
37
32
38
33
/**
39
34
* Attribute data type
40
- *
41
35
* @available boolean, bool, integer, int, float, double, string, array, object, null
42
36
* @var array
43
37
*/
44
38
protected $ casts = [];
45
39
46
40
/**
47
41
* Model attributes
48
- *
49
42
* @var array
50
43
*/
51
44
protected $ attributes = [];
52
45
53
46
54
47
/**
55
48
* Model flag indicates row exists in database
56
- *
57
49
* @var bool
58
50
*/
59
51
protected $ exists = false ;
60
52
61
53
/**
62
54
* Additional custom attributes
63
- *
64
55
* @var array
65
56
*/
66
57
protected $ appends = [];
67
58
68
59
/**
69
60
* Allowed casts
70
- *
71
61
* @var array
72
62
*/
73
63
private $ castTypes = [
@@ -86,9 +76,8 @@ class Model
86
76
87
77
/**
88
78
* Create a new Elasticsearch model instance.
89
- *
90
79
* @param array $attributes
91
- * @param bool $exists
80
+ * @param bool $exists
92
81
*/
93
82
function __construct ($ attributes = [], $ exists = false )
94
83
{
@@ -101,7 +90,6 @@ function __construct($attributes = [], $exists = false)
101
90
102
91
/**
103
92
* Get current connection
104
- *
105
93
* @return string
106
94
*/
107
95
public function getConnection ()
@@ -111,7 +99,7 @@ public function getConnection()
111
99
112
100
/**
113
101
* Set current connection
114
- *
102
+ * @param $connection
115
103
* @return void
116
104
*/
117
105
public function setConnection ($ connection )
@@ -131,7 +119,7 @@ public function getIndex()
131
119
132
120
/**
133
121
* Set index name
134
- *
122
+ * @param $index
135
123
* @return void
136
124
*/
137
125
public function setIndex ($ index )
@@ -141,7 +129,6 @@ public function setIndex($index)
141
129
142
130
/**
143
131
* Get type name
144
- *
145
132
* @return string
146
133
*/
147
134
public function getType ()
@@ -151,7 +138,7 @@ public function getType()
151
138
152
139
/**
153
140
* Set type name
154
- *
141
+ * @param $type
155
142
* @return void
156
143
*/
157
144
public function setType ($ type )
@@ -161,9 +148,7 @@ public function setType($type)
161
148
162
149
/**
163
150
* Magic getter for model properties
164
- *
165
151
* @param $name
166
- *
167
152
* @return null
168
153
*/
169
154
public function __get ($ name )
@@ -191,9 +176,7 @@ public function __get($name)
191
176
192
177
/**
193
178
* Get original model attribute
194
- *
195
179
* @param $name
196
- *
197
180
* @return mixed
198
181
*/
199
182
protected function getOriginalAttribute ($ name )
@@ -205,9 +188,7 @@ protected function getOriginalAttribute($name)
205
188
206
189
/**
207
190
* Get Appends model attribute
208
- *
209
191
* @param $name
210
- *
211
192
* @return mixed
212
193
*/
213
194
protected function getAppendsAttribute ($ name )
@@ -219,10 +200,8 @@ protected function getAppendsAttribute($name)
219
200
220
201
/**
221
202
* Set attributes casting
222
- *
223
203
* @param $name
224
204
* @param $value
225
- *
226
205
* @return mixed
227
206
*/
228
207
protected function setAttributeType ($ name , $ value )
@@ -239,7 +218,6 @@ protected function setAttributeType($name, $value)
239
218
240
219
/**
241
220
* Get model as array
242
- *
243
221
* @return array
244
222
*/
245
223
public function toArray ()
@@ -260,9 +238,7 @@ public function toArray()
260
238
261
239
/**
262
240
* Get the collection of items as JSON.
263
- *
264
241
* @param int $options
265
- *
266
242
* @return string
267
243
*/
268
244
public function toJson ($ options = 0 )
@@ -272,10 +248,8 @@ public function toJson($options = 0)
272
248
273
249
/**
274
250
* Handle model properties setter
275
- *
276
251
* @param $name
277
252
* @param $value
278
- *
279
253
* @return null
280
254
*/
281
255
public function __set ($ name , $ value )
@@ -298,12 +272,11 @@ public function __set($name, $value)
298
272
299
273
/**
300
274
* Create a new model query
301
- *
302
- * @return mixed
275
+ * @return Query
303
276
*/
304
277
protected function newQuery ()
305
278
{
306
- $ query = app ("es " )->setModel ($ this );
279
+ $ query = app ("es " )->connectionByName ()-> setModel ($ this );
307
280
308
281
$ query ->connection ($ this ->getConnection ());
309
282
@@ -320,7 +293,7 @@ protected function newQuery()
320
293
321
294
/**
322
295
* Get all model records
323
- *
296
+ * @throws \Exception
324
297
* @return mixed
325
298
*/
326
299
public static function all ()
@@ -334,9 +307,8 @@ public static function all()
334
307
335
308
/**
336
309
* Get model by key
337
- *
338
310
* @param $key
339
- *
311
+ * @throws \Exception
340
312
* @return mixed
341
313
*/
342
314
public static function find ($ key )
@@ -354,7 +326,6 @@ public static function find($key)
354
326
355
327
/**
356
328
* Delete model record
357
- *
358
329
* @return $this|bool
359
330
*/
360
331
function delete ()
@@ -373,7 +344,6 @@ function delete()
373
344
374
345
/**
375
346
* Save data to model
376
- *
377
347
* @return string
378
348
*/
379
349
public function save ()
@@ -415,7 +385,6 @@ public function save()
415
385
416
386
/**
417
387
* Check model is exists
418
- *
419
388
* @return bool
420
389
*/
421
390
function exists ()
@@ -425,7 +394,6 @@ function exists()
425
394
426
395
/**
427
396
* Get model key
428
- *
429
397
* @return mixed
430
398
*/
431
399
function getID ()
@@ -434,20 +402,50 @@ function getID()
434
402
}
435
403
436
404
/**
437
- * The "booting" method of the model.
438
- *
405
+ * Determine if an item exists at an offset.
406
+ * @param mixed $key
407
+ * @return bool
408
+ */
409
+ public function offsetExists ($ key )
410
+ {
411
+ return array_key_exists ($ key , $ this ->attributes ) or in_array ($ key , $ this ->appends ) or property_exists ($ this , $ key );
412
+ }
413
+
414
+ /**
415
+ * Get an item at a given offset.
416
+ * @param mixed $key
417
+ * @return mixed
418
+ */
419
+ public function offsetGet ($ key )
420
+ {
421
+ return $ this ->__get ($ key );
422
+ }
423
+
424
+ /**
425
+ * Set the item at a given offset.
426
+ * @param mixed $key
427
+ * @param mixed $value
428
+ * @return void
429
+ */
430
+ public function offsetSet ($ key , $ value )
431
+ {
432
+ $ this ->__set ($ key , $ value );
433
+ }
434
+
435
+ /**
436
+ * Unset the item at a given offset.
437
+ * @param string $key
439
438
* @return void
440
439
*/
441
- public function boot ( $ query )
440
+ public function offsetUnset ( $ key )
442
441
{
442
+ unset($ this ->attributes [$ key ]);
443
443
}
444
444
445
445
/**
446
446
* Handle dynamic static method calls into the method.
447
- *
448
447
* @param string $method
449
- * @param array $parameters
450
- *
448
+ * @param array $parameters
451
449
* @return mixed
452
450
*/
453
451
public static function __callStatic ($ method , $ parameters )
@@ -457,10 +455,8 @@ public static function __callStatic($method, $parameters)
457
455
458
456
/**
459
457
* Handle dynamic method calls into the model.
460
- *
461
458
* @param string $method
462
- * @param array $parameters
463
- *
459
+ * @param array $parameters
464
460
* @return mixed
465
461
*/
466
462
public function __call ($ method , $ parameters )
0 commit comments