|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipbox/spark/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipbox/spark |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\spark\services; |
|
10
|
|
|
|
|
11
|
|
|
use craft\helpers\Json as JsonHelper; |
|
12
|
|
|
use flipbox\spark\exceptions\ObjectNotFoundException; |
|
13
|
|
|
use flipbox\spark\helpers\ArrayHelper; |
|
14
|
|
|
use flipbox\spark\helpers\ObjectHelper; |
|
15
|
|
|
use flipbox\spark\Records\Record; |
|
16
|
|
|
use yii\base\Component; |
|
17
|
|
|
use yii\base\InvalidConfigException; |
|
18
|
|
|
use yii\base\Object as BaseObject; |
|
19
|
|
|
use yii\db\QueryInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Flipbox Factory <[email protected]> |
|
23
|
|
|
* @since 1.0.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract class Object extends Component |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
use traits\Object; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var BaseObject[] |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $cacheAll; |
|
34
|
|
|
|
|
35
|
|
|
/******************************************* |
|
36
|
|
|
* OBJECT CLASSES |
|
37
|
|
|
*******************************************/ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract public static function objectClass(): string; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function objectClassInstance(): string |
|
48
|
|
|
{ |
|
49
|
|
|
return BaseObject::class; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/******************************************* |
|
53
|
|
|
* CREATE |
|
54
|
|
|
*******************************************/ |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array|Record $config |
|
58
|
|
|
* @param string|null $toScenario |
|
59
|
|
|
* @throws InvalidConfigException |
|
60
|
|
|
* @return BaseObject |
|
61
|
|
|
*/ |
|
62
|
|
|
public function create($config = [], string $toScenario = null): BaseObject |
|
63
|
|
|
{ |
|
64
|
|
|
|
|
65
|
|
|
// Treat records as known data and set via config |
|
66
|
|
|
if ($config instanceof Record) { |
|
67
|
|
|
return $this->createFromRecord($config, $toScenario); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Force Array |
|
71
|
|
|
if (!is_array($config)) { |
|
72
|
|
|
$config = ArrayHelper::toArray($config, [], false); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Auto-set the class |
|
76
|
|
|
if ($class = static::objectClass()) { |
|
77
|
|
|
$config['class'] = $class; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return ObjectHelper::create( |
|
81
|
|
|
$config, |
|
82
|
|
|
static::objectClassInstance() |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param Record $record |
|
88
|
|
|
* @param string|null $toScenario |
|
89
|
|
|
* @throws InvalidConfigException |
|
90
|
|
|
* @return BaseObject |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function createFromRecord(Record $record, string $toScenario = null): BaseObject |
|
93
|
|
|
{ |
|
94
|
|
|
|
|
95
|
|
|
if (null !== $toScenario) { |
|
96
|
|
|
$record->setScenario($toScenario); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$config = $record->toArray(); |
|
100
|
|
|
|
|
101
|
|
|
// Auto-set the class |
|
102
|
|
|
if ($class = static::objectClass()) { |
|
103
|
|
|
$config['class'] = $class; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return ObjectHelper::create( |
|
107
|
|
|
$config, |
|
108
|
|
|
static::objectClassInstance() |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/******************************************* |
|
114
|
|
|
* FIND/GET ALL |
|
115
|
|
|
*******************************************/ |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $toScenario |
|
119
|
|
|
* @return BaseObject[] |
|
120
|
|
|
*/ |
|
121
|
|
|
public function findAll(string $toScenario = null) |
|
122
|
|
|
{ |
|
123
|
|
|
|
|
124
|
|
|
// Check addToCache |
|
125
|
|
|
if (is_null($this->cacheAll)) { |
|
126
|
|
|
$this->cacheAll = []; |
|
127
|
|
|
|
|
128
|
|
|
// Find record in db |
|
129
|
|
|
if ($records = $this->findAllRecords()) { |
|
130
|
|
|
foreach ($records as $record) { |
|
131
|
|
|
$this->cacheAll[] = $this->findByRecord($record, $toScenario); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this->cacheAll; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $toScenario |
|
141
|
|
|
* @return BaseObject[] |
|
142
|
|
|
* @throws ObjectNotFoundException |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getAll(string $toScenario = null): array |
|
145
|
|
|
{ |
|
146
|
|
|
|
|
147
|
|
|
if (!$objects = $this->findAll($toScenario)) { |
|
148
|
|
|
$this->notFoundException(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $objects; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/******************************************* |
|
155
|
|
|
* FIND/GET |
|
156
|
|
|
*******************************************/ |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param $identifier |
|
160
|
|
|
* @param string $toScenario |
|
161
|
|
|
* @return BaseObject|null |
|
162
|
|
|
*/ |
|
163
|
|
|
public function find($identifier, string $toScenario = null) |
|
164
|
|
|
{ |
|
165
|
|
|
|
|
166
|
|
|
if ($identifier instanceof BaseObject) { |
|
167
|
|
|
$this->addToCache($identifier); |
|
168
|
|
|
|
|
169
|
|
|
return $identifier; |
|
170
|
|
|
} elseif ($identifier instanceof Record) { |
|
171
|
|
|
return $this->findByRecord($identifier, $toScenario); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return null; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param $identifier |
|
179
|
|
|
* @param string $toScenario |
|
180
|
|
|
* @return BaseObject |
|
181
|
|
|
* @throws ObjectNotFoundException |
|
182
|
|
|
*/ |
|
183
|
|
|
public function get($identifier, string $toScenario = null): BaseObject |
|
184
|
|
|
{ |
|
185
|
|
|
|
|
186
|
|
|
// Find model by ID |
|
187
|
|
|
if (!$object = $this->find($identifier, $toScenario)) { |
|
188
|
|
|
$this->notFoundException(); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $object; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/******************************************* |
|
195
|
|
|
* FIND/GET BY QUERY |
|
196
|
|
|
*******************************************/ |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param QueryInterface $query |
|
200
|
|
|
* @param string $toScenario |
|
201
|
|
|
* @return BaseObject[] |
|
202
|
|
|
*/ |
|
203
|
|
|
public function findAllByQuery(QueryInterface $query, string $toScenario = null): array |
|
204
|
|
|
{ |
|
205
|
|
|
|
|
206
|
|
|
$objects = array(); |
|
207
|
|
|
|
|
208
|
|
|
foreach ($query->all() as $record) { |
|
209
|
|
|
$objects[] = $this->findByRecord($record, $toScenario); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return $objects; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param QueryInterface $query |
|
217
|
|
|
* @param string $toScenario |
|
218
|
|
|
* @return BaseObject|null |
|
219
|
|
|
*/ |
|
220
|
|
|
public function findByQuery(QueryInterface $query, string $toScenario = null) |
|
221
|
|
|
{ |
|
222
|
|
|
|
|
223
|
|
|
/** @var Record $record */ |
|
224
|
|
|
if (!$record = $query->one()) { |
|
225
|
|
|
return null; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $this->findByRecord($record, $toScenario); |
|
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/******************************************* |
|
232
|
|
|
* FIND/GET BY CONDITION |
|
233
|
|
|
*******************************************/ |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @param $condition |
|
237
|
|
|
* @param string $toScenario |
|
238
|
|
|
* @return BaseObject[] |
|
239
|
|
|
*/ |
|
240
|
|
|
public function findAllByCondition($condition, string $toScenario = null): array |
|
241
|
|
|
{ |
|
242
|
|
|
|
|
243
|
|
|
$objects = []; |
|
244
|
|
|
|
|
245
|
|
|
// Find record in db |
|
246
|
|
|
if ($records = $this->findAllRecordsByCondition($condition)) { |
|
247
|
|
|
foreach ($records as $record) { |
|
248
|
|
|
$objects[] = $this->findByRecord($record, $toScenario); |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
return $objects; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param $condition |
|
257
|
|
|
* @param string $toScenario |
|
258
|
|
|
* @return BaseObject[] |
|
259
|
|
|
* @throws ObjectNotFoundException |
|
260
|
|
|
*/ |
|
261
|
|
|
public function getAllByCondition($condition, string $toScenario = null): array |
|
262
|
|
|
{ |
|
263
|
|
|
|
|
264
|
|
|
if (!$objects = $this->findAllByCondition($condition, $toScenario)) { |
|
265
|
|
|
$this->notFoundByConditionException($condition); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return $objects; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @param $condition |
|
273
|
|
|
* @param string $toScenario |
|
274
|
|
|
* @return BaseObject|null |
|
275
|
|
|
*/ |
|
276
|
|
|
public function findByCondition($condition, string $toScenario = null) |
|
277
|
|
|
{ |
|
278
|
|
|
|
|
279
|
|
|
// Find record in db |
|
280
|
|
|
if ($record = $this->findRecordByCondition($condition)) { |
|
281
|
|
|
return $this->findByRecord($record, $toScenario); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
return null; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* @param $condition |
|
289
|
|
|
* @param string $toScenario |
|
290
|
|
|
* @return BaseObject |
|
291
|
|
|
* @throws ObjectNotFoundException |
|
292
|
|
|
*/ |
|
293
|
|
|
public function getByCondition($condition, string $toScenario = null): BaseObject |
|
294
|
|
|
{ |
|
295
|
|
|
|
|
296
|
|
|
if (!$object = $this->findByCondition($condition, $toScenario)) { |
|
297
|
|
|
$this->notFoundByConditionException($condition); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
return $object; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/******************************************* |
|
304
|
|
|
* FIND/GET BY CRITERIA |
|
305
|
|
|
*******************************************/ |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @param $criteria |
|
309
|
|
|
* @param string $toScenario |
|
310
|
|
|
* @return BaseObject[] |
|
311
|
|
|
*/ |
|
312
|
|
|
public function findAllByCriteria($criteria, string $toScenario = null): array |
|
313
|
|
|
{ |
|
314
|
|
|
|
|
315
|
|
|
$objects = []; |
|
316
|
|
|
|
|
317
|
|
|
// Find record in db |
|
318
|
|
|
if ($records = $this->findAllRecordsByCriteria($criteria) |
|
319
|
|
|
) { |
|
320
|
|
|
foreach ($records as $record) { |
|
321
|
|
|
$objects[] = $this->findByRecord($record, $toScenario); |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
return $objects; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @param $criteria |
|
330
|
|
|
* @param string $toScenario |
|
331
|
|
|
* @return BaseObject[] |
|
332
|
|
|
* @throws ObjectNotFoundException |
|
333
|
|
|
*/ |
|
334
|
|
|
public function getAllByCriteria($criteria, string $toScenario = null): array |
|
335
|
|
|
{ |
|
336
|
|
|
|
|
337
|
|
|
if (!$objects = $this->findAllByCriteria($criteria, $toScenario)) { |
|
338
|
|
|
$this->notFoundByCriteriaException($criteria); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
return $objects; |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* @param $criteria |
|
346
|
|
|
* @param string $toScenario |
|
347
|
|
|
* @return BaseObject|null |
|
348
|
|
|
*/ |
|
349
|
|
|
public function findByCriteria($criteria, string $toScenario = null) |
|
350
|
|
|
{ |
|
351
|
|
|
|
|
352
|
|
|
// Find record in db |
|
353
|
|
|
if ($record = $this->findRecordByCriteria($criteria)) { |
|
354
|
|
|
return $this->findByRecord($record, $toScenario); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
return null; |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* @param $criteria |
|
362
|
|
|
* @param string $toScenario |
|
363
|
|
|
* @return BaseObject |
|
364
|
|
|
* @throws ObjectNotFoundException |
|
365
|
|
|
*/ |
|
366
|
|
|
public function getByCriteria($criteria, string $toScenario = null): BaseObject |
|
367
|
|
|
{ |
|
368
|
|
|
|
|
369
|
|
|
if (!$object = $this->findByCriteria($criteria, $toScenario)) { |
|
370
|
|
|
$this->notFoundByCriteriaException($criteria); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
return $object; |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
|
|
377
|
|
|
/******************************************* |
|
378
|
|
|
* FIND/GET BY RECORD |
|
379
|
|
|
*******************************************/ |
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* @param Record $record |
|
383
|
|
|
* @param string $toScenario |
|
384
|
|
|
* @return BaseObject |
|
385
|
|
|
*/ |
|
386
|
|
|
public function findByRecord(Record $record, string $toScenario = null): BaseObject |
|
387
|
|
|
{ |
|
388
|
|
|
|
|
389
|
|
|
// Check addToCache |
|
390
|
|
|
if (!$object = $this->findCacheByRecord($record)) { |
|
391
|
|
|
// New model |
|
392
|
|
|
$object = $this->createFromRecord($record, $toScenario); |
|
393
|
|
|
|
|
394
|
|
|
// Cache it |
|
395
|
|
|
$this->addToCache($object); |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
return $object; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* @param Record $record |
|
403
|
|
|
* @param string $toScenario |
|
404
|
|
|
* @return BaseObject |
|
405
|
|
|
*/ |
|
406
|
|
|
public function getByRecord(Record $record, string $toScenario = null): BaseObject |
|
407
|
|
|
{ |
|
408
|
|
|
return $this->findByRecord($record, $toScenario); |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
|
|
412
|
|
|
/******************************************* |
|
413
|
|
|
* CACHE |
|
414
|
|
|
*******************************************/ |
|
415
|
|
|
|
|
416
|
|
|
/** |
|
417
|
|
|
* @param $identifier |
|
418
|
|
|
* @return BaseObject|null |
|
419
|
|
|
*/ |
|
420
|
|
|
public function findCache($identifier) |
|
421
|
|
|
{ |
|
422
|
|
|
|
|
423
|
|
|
if ($identifier instanceof Record) { |
|
424
|
|
|
return $this->findCacheByRecord($identifier); |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
return null; |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* @param Record $record |
|
432
|
|
|
* @return BaseObject|null |
|
433
|
|
|
*/ |
|
434
|
|
|
public function findCacheByRecord(Record $record) |
|
435
|
|
|
{ |
|
436
|
|
|
return null; |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* @param BaseObject $object |
|
441
|
|
|
* @return static |
|
442
|
|
|
*/ |
|
443
|
|
|
public function addToCache(BaseObject $object) |
|
444
|
|
|
{ |
|
445
|
|
|
return $this; |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
|
|
449
|
|
|
/******************************************* |
|
450
|
|
|
* EXCEPTIONS |
|
451
|
|
|
*******************************************/ |
|
452
|
|
|
|
|
453
|
|
|
/** |
|
454
|
|
|
* @throws ObjectNotFoundException |
|
455
|
|
|
*/ |
|
456
|
|
|
protected function notFoundException() |
|
457
|
|
|
{ |
|
458
|
|
|
|
|
459
|
|
|
throw new ObjectNotFoundException( |
|
460
|
|
|
sprintf( |
|
461
|
|
|
"Object does not exist." |
|
462
|
|
|
) |
|
463
|
|
|
); |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
/** |
|
467
|
|
|
* @param null $criteria |
|
468
|
|
|
* @throws ObjectNotFoundException |
|
469
|
|
|
*/ |
|
470
|
|
|
protected function notFoundByCriteriaException($criteria = null) |
|
471
|
|
|
{ |
|
472
|
|
|
|
|
473
|
|
|
throw new ObjectNotFoundException( |
|
474
|
|
|
sprintf( |
|
475
|
|
|
'Object does not exist with the criteria "%s".', |
|
476
|
|
|
(string)JsonHelper::encode($criteria) |
|
477
|
|
|
) |
|
478
|
|
|
); |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
/** |
|
482
|
|
|
* @param null $condition |
|
483
|
|
|
* @throws ObjectNotFoundException |
|
484
|
|
|
*/ |
|
485
|
|
|
protected function notFoundByConditionException($condition = null) |
|
486
|
|
|
{ |
|
487
|
|
|
|
|
488
|
|
|
throw new ObjectNotFoundException( |
|
489
|
|
|
sprintf( |
|
490
|
|
|
'Object does not exist with the condition "%s".', |
|
491
|
|
|
(string)JsonHelper::encode($condition) |
|
492
|
|
|
) |
|
493
|
|
|
); |
|
494
|
|
|
} |
|
495
|
|
|
} |
|
496
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: