1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 ekow. |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace ntentan\nibii; |
28
|
|
|
|
29
|
|
|
use ntentan\utils\Text; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* An active record wrapper for database records. |
33
|
|
|
*/ |
34
|
|
|
class RecordWrapper implements \ArrayAccess, \Countable, \Iterator |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* An associative array of models to which this model has a one to may relationship. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $hasMany = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* An associative array of models which have a one-to-many relationship with this model. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $belongsTo = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* An associative array of models with which this model has a many to many relationship. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $manyHaveMany = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The name of the database table. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $table; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The name of the schema to which this table belongs. |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $schema; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Temporary data held in the model object. |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
protected $modelData = []; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* A quoted string of the table name used for building queries. |
81
|
|
|
* |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
private $quotedTable; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The raw table name without any quotes. |
88
|
|
|
* |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
private $unquotedTable; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* An array of fields that contain validation errors after an attempted save. |
95
|
|
|
* |
96
|
|
|
* @var array |
97
|
|
|
*/ |
98
|
|
|
private $invalidFields; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* An instance of the operations dispatcher. |
102
|
|
|
* |
103
|
|
|
* @var Operations |
104
|
|
|
*/ |
105
|
|
|
private $dynamicOperations; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Location of the RecordWrapper's internal iterator. |
109
|
|
|
* |
110
|
|
|
* @var int |
111
|
|
|
*/ |
112
|
|
|
private $index = 0; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* This flag is set whenever data is manually put in the model with the setData method. |
116
|
|
|
* |
117
|
|
|
* @var bool |
118
|
|
|
*/ |
119
|
|
|
private $dataSet = false; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* The name of the class for this model obtained through reflection. |
123
|
|
|
* |
124
|
|
|
* @var string |
125
|
|
|
*/ |
126
|
|
|
private $className; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* An instance of the driver adapter for interacting with the database. |
130
|
|
|
* |
131
|
|
|
* @var DriverAdapter |
132
|
|
|
*/ |
133
|
|
|
private $adapter; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* An instance of the ORMContext through which this model is operating. |
137
|
|
|
* |
138
|
|
|
* @var ORMContext |
139
|
|
|
*/ |
140
|
|
|
private $context; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Keys for the various fields when model is accessed as an associative array. |
144
|
|
|
* |
145
|
|
|
* @var array |
146
|
|
|
*/ |
147
|
|
|
private $keys = []; |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* This flag is set after the model has been properly initialized. |
151
|
|
|
* Useful after model is unserialized or accessed through the static interface. |
152
|
|
|
* |
153
|
|
|
* @var bool |
154
|
|
|
*/ |
155
|
|
|
private $initialized = false; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Initialize the record wrapper and setup the adapters, drivers, tables and schemas. |
159
|
|
|
* After initialization, this method sets the initialized flag. |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
36 |
|
protected function initialize(): void |
163
|
|
|
{ |
164
|
36 |
|
if ($this->initialized) { |
165
|
36 |
|
return; |
166
|
|
|
} |
167
|
36 |
|
$this->context = ORMContext::getInstance(); |
168
|
36 |
|
$this->adapter = $this->context->getDriverAdapter(); |
169
|
36 |
|
$table = $this->table ?? $this->context->getModelTable($this); |
170
|
36 |
|
$driver = $this->context->getDbContext()->getDriver(); |
171
|
36 |
|
$this->adapter->setContext($this->context); |
172
|
36 |
|
$this->className = (new \ReflectionClass($this))->getName(); |
173
|
36 |
|
if (is_string($table)) { |
174
|
36 |
|
$this->table = $this->unquotedTable = $table; |
175
|
|
|
} else { |
176
|
|
|
$this->table = $table['table']; |
177
|
|
|
$this->schema = $table['schema']; |
178
|
|
|
} |
179
|
36 |
|
$this->quotedTable = ($this->schema ? "{$driver->quoteIdentifier($this->schema)}." : "") . $driver->quoteIdentifier($this->table); |
180
|
36 |
|
$this->unquotedTable = ($this->schema ? "{$this->schema}." : "") . $this->table; |
181
|
36 |
|
$this->adapter->setModel($this, $this->quotedTable); |
182
|
36 |
|
$this->initialized = true; |
183
|
36 |
|
} |
184
|
|
|
|
185
|
|
|
public function __debugInfo() |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
$data = $this->getData(); |
188
|
|
|
return $this->hasMultipleItems() ? $data : isset($data[0]) ? $data[0] : []; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Return a description of the model wrapped by this wrapper. |
193
|
|
|
* @return ModelDescription |
194
|
|
|
*/ |
195
|
28 |
|
public function getDescription() : ModelDescription |
196
|
|
|
{ |
197
|
28 |
|
$this->initialize(); |
198
|
28 |
|
return $this->context->getCache()->read( |
199
|
|
|
"{$this->className}::desc", function () { |
200
|
28 |
|
return $this->context->getModelDescription($this); |
201
|
28 |
|
} |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Return the number of items stored in the model or matched by the query. |
207
|
|
|
* Depending on the state of the model, the count method will return different values. For models that have data |
208
|
|
|
* values set with calls to setData, this method returns the number of records that were added. On the other hand, |
209
|
|
|
* for models that do not have data set, this method queries the database to find out the number of records that |
210
|
|
|
* are either in the model, or for models that have been filtered, the number of records that match the filter. |
211
|
|
|
* |
212
|
|
|
* @param int|array|QueryParameters $query |
213
|
|
|
* @return int |
|
|
|
|
214
|
|
|
*/ |
215
|
14 |
|
public function count($query = null) |
216
|
|
|
{ |
217
|
14 |
|
if ($this->dataSet) { |
218
|
14 |
|
return count($this->getData()); |
219
|
|
|
} |
220
|
|
|
return $this->__call('count', [$query]); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Retrieve an item stored in the record. |
225
|
|
|
* This method returns items that are directly stored in the model or lazy loads related items. The key could be a |
226
|
|
|
* field in the model's table or the name of a related model. |
227
|
|
|
* |
228
|
|
|
* @param string $key A key identifying the item to be retrieved. |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
12 |
|
private function retrieveItem($key) |
232
|
|
|
{ |
233
|
12 |
|
$relationships = $this->getDescription()->getRelationships(); |
234
|
12 |
|
$decamelizedKey = Text::deCamelize($key); |
235
|
12 |
|
if (isset($relationships[$key])) { |
236
|
8 |
|
return $this->fetchRelatedFields($relationships[$key]); |
237
|
|
|
} |
238
|
4 |
|
return isset($this->modelData[$decamelizedKey]) ? $this->modelData[$decamelizedKey] : null; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Calls dynamic methods. |
243
|
|
|
* |
244
|
|
|
* @param string $name |
245
|
|
|
* @param array $arguments |
246
|
|
|
* @return type |
247
|
|
|
* @throws exceptions\NibiiException |
248
|
|
|
*/ |
249
|
34 |
|
public function __call($name, $arguments) |
250
|
|
|
{ |
251
|
34 |
|
$this->initialize(); |
252
|
34 |
|
if ($this->dynamicOperations === null) { |
253
|
34 |
|
$this->dynamicOperations = new Operations($this, $this->quotedTable); |
254
|
|
|
} |
255
|
34 |
|
return $this->dynamicOperations->perform($name, $arguments); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Set a value for a field in the model. |
260
|
|
|
* |
261
|
|
|
* @param string $name |
262
|
|
|
* @param mixed $value |
263
|
|
|
*/ |
264
|
8 |
|
public function __set($name, $value) |
265
|
|
|
{ |
266
|
8 |
|
$this->dataSet = true; |
267
|
8 |
|
$this->modelData[Text::deCamelize($name)] = $value; |
268
|
8 |
|
} |
269
|
|
|
|
270
|
12 |
|
public function __get($name) |
271
|
|
|
{ |
272
|
12 |
|
return $this->retrieveItem($name); |
273
|
|
|
} |
274
|
|
|
|
275
|
10 |
|
public function save() |
276
|
|
|
{ |
277
|
10 |
|
$return = $this->__call('save', [$this->hasMultipleItems()]); |
278
|
10 |
|
$this->invalidFields = $this->dynamicOperations->getInvalidFields(); |
279
|
10 |
|
return $return; |
280
|
|
|
} |
281
|
|
|
|
282
|
18 |
|
private function hasMultipleItems() |
283
|
|
|
{ |
284
|
18 |
|
if (count($this->modelData) > 0) { |
285
|
16 |
|
return is_numeric(array_keys($this->modelData)[0]); |
286
|
|
|
} else { |
287
|
2 |
|
return false; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
18 |
|
public function getData() |
292
|
|
|
{ |
293
|
18 |
|
$data = []; |
294
|
|
|
|
295
|
18 |
|
if (count($this->modelData) == 0) { |
296
|
6 |
|
$data = $this->modelData; |
297
|
16 |
|
} else if ($this->hasMultipleItems()) { |
298
|
6 |
|
$data = $this->modelData; |
299
|
12 |
|
} else if (count($this->modelData) > 0) { |
300
|
12 |
|
$data[] = $this->modelData; |
301
|
|
|
} |
302
|
|
|
|
303
|
18 |
|
return $data; |
304
|
|
|
} |
305
|
|
|
|
306
|
26 |
|
public function setData($data) |
307
|
|
|
{ |
308
|
26 |
|
$this->dataSet = is_array($data) ? true : false; |
309
|
26 |
|
$this->modelData = $data; |
310
|
26 |
|
} |
311
|
|
|
|
312
|
|
|
public function mergeData($data) |
313
|
|
|
{ |
314
|
|
|
foreach ($data as $key => $value) { |
315
|
|
|
$this->modelData[$key] = $value; |
316
|
|
|
} |
317
|
|
|
$this->dataSet = true; |
318
|
|
|
} |
319
|
|
|
|
320
|
2 |
|
public function offsetExists($offset) |
321
|
|
|
{ |
322
|
2 |
|
return isset($this->modelData[$offset]); |
323
|
|
|
} |
324
|
|
|
|
325
|
2 |
|
public function offsetGet($offset) |
326
|
|
|
{ |
327
|
2 |
|
if (is_numeric($offset)) { |
328
|
2 |
|
return $this->wrap($offset); |
329
|
|
|
} else { |
330
|
2 |
|
return $this->retrieveItem($offset); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
334
|
2 |
|
public function offsetSet($offset, $value) |
335
|
|
|
{ |
336
|
2 |
|
$this->dataSet = true; |
337
|
2 |
|
$this->modelData[$offset] = $value; |
338
|
2 |
|
} |
339
|
|
|
|
340
|
|
|
public function offsetUnset($offset) |
341
|
|
|
{ |
342
|
|
|
unset($this->modelData[$offset]); |
343
|
|
|
} |
344
|
|
|
|
345
|
6 |
|
private function wrap($offset) |
346
|
|
|
{ |
347
|
6 |
|
$this->initialize(); |
348
|
6 |
|
if (isset($this->modelData[$offset])) { |
349
|
6 |
|
$className = $this->className; |
350
|
6 |
|
$newInstance = new $className(); |
351
|
6 |
|
$newInstance->initialize(); |
352
|
6 |
|
$newInstance->setData($this->modelData[$offset]); |
353
|
6 |
|
return $newInstance; |
354
|
|
|
} else { |
355
|
|
|
return null; |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
4 |
|
public function getInvalidFields() |
360
|
|
|
{ |
361
|
4 |
|
return $this->invalidFields; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
public function getHasMany() |
365
|
|
|
{ |
366
|
|
|
return $this->hasMany; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function getBelongsTo() |
370
|
|
|
{ |
371
|
|
|
return $this->belongsTo; |
372
|
|
|
} |
373
|
|
|
|
374
|
4 |
|
public function current() |
375
|
|
|
{ |
376
|
4 |
|
return $this->wrap($this->keys[$this->index]); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
public function key() |
380
|
|
|
{ |
381
|
|
|
return $this->keys[$this->index]; |
382
|
|
|
} |
383
|
|
|
|
384
|
4 |
|
public function next() |
385
|
|
|
{ |
386
|
4 |
|
$this->index++; |
387
|
4 |
|
} |
388
|
|
|
|
389
|
4 |
|
public function rewind() |
390
|
|
|
{ |
391
|
4 |
|
$this->keys = array_keys($this->modelData); |
392
|
4 |
|
$this->index = 0; |
393
|
4 |
|
} |
394
|
|
|
|
395
|
4 |
|
public function valid() |
396
|
|
|
{ |
397
|
4 |
|
return isset($this->keys[$this->index]) && isset($this->modelData[$this->keys[$this->index]]); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* A custom validator for the record wrapper. |
402
|
|
|
* |
403
|
|
|
* @return mixed |
404
|
|
|
*/ |
405
|
10 |
|
public function validate() |
406
|
|
|
{ |
407
|
10 |
|
return []; |
408
|
|
|
} |
409
|
|
|
|
410
|
12 |
|
private function fetchRelatedFields($relationship, $index = null) |
|
|
|
|
411
|
|
|
{ |
412
|
12 |
|
if ($index === null) { |
413
|
12 |
|
$data = $this->modelData; |
414
|
|
|
} else { |
415
|
|
|
$data = $this->modelData[$index]; |
416
|
|
|
} |
417
|
12 |
|
$model = $relationship->getModelInstance(); |
418
|
12 |
|
if (empty($data)) { |
419
|
|
|
return $model; |
420
|
|
|
} |
421
|
12 |
|
$query = $relationship->prepareQuery($data); |
422
|
12 |
|
return $query ? $model->fetch($query) : $model; |
423
|
|
|
} |
424
|
|
|
|
425
|
28 |
|
public function getRelationships() |
426
|
|
|
{ |
427
|
|
|
return [ |
428
|
28 |
|
'HasMany' => $this->hasMany, |
429
|
28 |
|
'BelongsTo' => $this->belongsTo, |
430
|
28 |
|
'ManyHaveMany' => $this->manyHaveMany |
431
|
|
|
]; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
public function usetField($field) |
435
|
|
|
{ |
436
|
|
|
unset($this->modelData[$field]); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Callback for when a record is either added or modified. |
441
|
|
|
*/ |
442
|
10 |
|
public function preSaveCallback() |
443
|
|
|
{ |
444
|
|
|
|
445
|
10 |
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Callback for when a record has been added or modified. |
449
|
|
|
* @param $id |
450
|
|
|
*/ |
451
|
6 |
|
public function postSaveCallback() |
452
|
|
|
{ |
453
|
|
|
|
454
|
6 |
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Callback for when a new record is about to be created. |
458
|
|
|
*/ |
459
|
8 |
|
public function preCreateCallback() |
460
|
|
|
{ |
461
|
|
|
|
462
|
8 |
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Callback for when a new record has been created. |
466
|
|
|
* This callback can be most useful for obtaining the primary key of a newly created record. |
467
|
|
|
* |
468
|
|
|
* @param $id |
469
|
|
|
*/ |
470
|
4 |
|
public function postCreateCallback($id) |
|
|
|
|
471
|
|
|
{ |
472
|
|
|
|
473
|
4 |
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* Callback for when a record is about to be updated. |
477
|
|
|
*/ |
478
|
2 |
|
public function preUpdateCallback() |
479
|
|
|
{ |
480
|
|
|
|
481
|
2 |
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Callback for when a record has been updated. |
485
|
|
|
*/ |
486
|
2 |
|
public function postUpdateCallback() |
487
|
|
|
{ |
488
|
|
|
|
489
|
2 |
|
} |
490
|
|
|
|
491
|
36 |
|
public function getDBStoreInformation() |
492
|
|
|
{ |
493
|
36 |
|
$this->initialize(); |
494
|
|
|
return [ |
495
|
36 |
|
'schema' => $this->schema, |
496
|
36 |
|
'table' => $this->table, |
497
|
36 |
|
'quoted_table' => $this->quotedTable, |
498
|
36 |
|
'unquoted_table' => $this->unquotedTable |
499
|
|
|
]; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* |
504
|
|
|
* @return DriverAdapter |
505
|
|
|
*/ |
506
|
36 |
|
public function getAdapter() |
507
|
|
|
{ |
508
|
36 |
|
$this->initialize(); |
509
|
36 |
|
return $this->adapter; |
510
|
|
|
} |
511
|
|
|
|
512
|
4 |
|
private function expandArrayValue($array, $relationships, $depth, $expandableModels = [], $index = null) |
|
|
|
|
513
|
|
|
{ |
514
|
4 |
|
if (empty($expandableModels)) { |
515
|
4 |
|
foreach ($relationships as $name => $relationship) { |
516
|
4 |
|
$array[$name] = $this->fetchRelatedFields($relationship, $index)->toArray($depth); |
517
|
|
|
} |
518
|
|
|
} else { |
519
|
|
|
foreach ($expandableModels as $name) { |
520
|
|
|
$array[$name] = $this->fetchRelatedFields($relationships[$name], $index)->toArray($depth, $expandableModels); |
521
|
|
|
} |
522
|
|
|
} |
523
|
4 |
|
return $array; |
524
|
|
|
} |
525
|
|
|
|
526
|
24 |
|
public function toArray($depth = 0, $expandableModels = []) |
|
|
|
|
527
|
|
|
{ |
528
|
24 |
|
$relationships = $this->getDescription()->getRelationships(); |
529
|
24 |
|
$array = $this->modelData; |
530
|
24 |
|
if ($depth > 0) { |
531
|
4 |
|
if ($this->hasMultipleItems()) { |
532
|
|
|
foreach ($array as $i => $value) { |
533
|
|
|
$array[$i] = $this->expandArrayValue($value, $relationships, $depth - 1, $expandableModels, $i); |
534
|
|
|
} |
535
|
|
|
} else { |
536
|
4 |
|
$array = $this->expandArrayValue($array, $relationships, $depth - 1, $expandableModels); |
537
|
|
|
} |
538
|
|
|
} |
539
|
24 |
|
return $array; |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
} |
543
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.