1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Starlit Db. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016 Starweb / Ehandelslogik i Lund AB |
6
|
|
|
* @license BSD 3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Starlit\Db; |
10
|
|
|
|
11
|
|
|
use Starlit\Utils\Str; |
12
|
|
|
use Starlit\Utils\Arr; |
13
|
|
|
use Starlit\Utils\Validation\Validator; |
14
|
|
|
use Starlit\Utils\Validation\ValidatorTranslatorInterface; |
15
|
|
|
use Symfony\Component\Translation\TranslatorInterface as SymfonyTranslatorInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Abstract class to model a single database row into an object. |
19
|
|
|
* |
20
|
|
|
* @author Andreas Nilsson <http://github.com/jandreasn> |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractDbEntity implements \Serializable |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The database table name (meant to be overridden). |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected static $dbTableName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Entity's database properties and their attributes (meant to be overridden). |
33
|
|
|
* Example format: |
34
|
|
|
* |
35
|
|
|
* $dbProperties = [ |
36
|
|
|
* 'productId' => ['type' => 'int'], |
37
|
|
|
* 'otherId' => ['type' => 'int', 'required' => true, 'validate' => false], |
38
|
|
|
* 'name' => ['type' => 'string', 'maxLength' => 10, 'required' => true, 'default' => 'Some name'], |
39
|
|
|
* ]; |
40
|
|
|
* |
41
|
|
|
* 'type' => 'int' Corresponding PHP type (required). |
42
|
|
|
* 'validate' => false Turn off data validation, for example on required key fields that are set internally. |
43
|
|
|
* 'required' => true The value have to be set (not '', null, false) |
44
|
|
|
* 'nonEmpty' => true The value should not be empty ('', 0, null) |
45
|
|
|
* |
46
|
|
|
* Properties correspond to database table's columns but words are |
47
|
|
|
* camel cased instead of separated with underscore (_) as in the database. |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected static $dbProperties = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Object database field name that is used for primary key (meant to be overridden). |
55
|
|
|
* Should be camel cased as it maps to the dbFields array. |
56
|
|
|
* |
57
|
|
|
* @var string|array |
58
|
|
|
*/ |
59
|
|
|
protected static $primaryDbPropertyKey; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
private static $cachedDefaultDbData = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
private static $cachedDbPropertyNames; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
private static $cachedDbFieldNames; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
private static $typeDefaults = [ |
80
|
|
|
'string' => '', |
81
|
|
|
'int' => 0, |
82
|
|
|
'float' => 0.0, |
83
|
|
|
'bool' => false, |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Database row data with field names and their values. |
88
|
|
|
* |
89
|
|
|
* @var array |
90
|
|
|
*/ |
91
|
|
|
private $dbData = []; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Database fields that has had their value modified since init/load. |
95
|
|
|
* |
96
|
|
|
* @var array |
97
|
|
|
*/ |
98
|
|
|
private $modifiedDbProperties = []; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var bool |
102
|
|
|
*/ |
103
|
|
|
private $deleteFromDbOnSave = false; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @var bool |
107
|
|
|
*/ |
108
|
|
|
private $deleted = false; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @var bool |
112
|
|
|
*/ |
113
|
|
|
private $forceDbInsertOnSave = false; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Constructor. |
117
|
|
|
* |
118
|
|
|
* @param mixed $primaryDbValueOrRowData |
119
|
|
|
*/ |
120
|
66 |
|
public function __construct($primaryDbValueOrRowData = null) |
121
|
|
|
{ |
122
|
66 |
|
self::checkStaticProperties(); |
123
|
|
|
|
124
|
|
|
// Set default values |
125
|
65 |
|
$this->dbData = $this->getDefaultDbData(); |
126
|
|
|
|
127
|
|
|
// Override default values with provided values |
128
|
65 |
|
if ($primaryDbValueOrRowData !== null) { |
129
|
13 |
|
$this->setPrimaryDbValueOrRowData($primaryDbValueOrRowData); |
130
|
|
|
} |
131
|
65 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Make sure that class has all necessary static properties set. |
135
|
|
|
*/ |
136
|
66 |
|
private static function checkStaticProperties() |
137
|
|
|
{ |
138
|
66 |
|
static $checkedClasses = []; |
139
|
66 |
|
if (!in_array(static::class, $checkedClasses)) { |
140
|
7 |
|
if (empty(static::$dbTableName) |
141
|
6 |
|
|| empty(static::$dbProperties) |
142
|
6 |
|
|| empty(static::$primaryDbPropertyKey) |
143
|
6 |
|
|| (is_scalar(static::$primaryDbPropertyKey) |
144
|
6 |
|
&& !isset(static::$dbProperties[static::$primaryDbPropertyKey]['type'])) |
145
|
6 |
|
|| (is_array(static::$primaryDbPropertyKey) |
146
|
7 |
|
&& !Arr::allIn(static::$primaryDbPropertyKey, array_keys(static::$dbProperties))) |
147
|
|
|
) { |
148
|
1 |
|
throw new \LogicException("All db entity's static properties not set"); |
149
|
|
|
} |
150
|
6 |
|
$checkedClasses[] = static::class; |
151
|
|
|
} |
152
|
65 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param mixed $primaryDbValueOrRowData |
156
|
|
|
*/ |
157
|
13 |
|
public function setPrimaryDbValueOrRowData($primaryDbValueOrRowData = null) |
158
|
|
|
{ |
159
|
|
|
// Row data would ba an associative array (not sequential, that would indicate a multi column primary key) |
160
|
13 |
|
if (is_array($primaryDbValueOrRowData) && !isset($primaryDbValueOrRowData[0])) { |
161
|
1 |
|
$this->setDbDataFromRow($primaryDbValueOrRowData); |
162
|
|
|
} else { |
163
|
12 |
|
$this->setPrimaryDbValue($primaryDbValueOrRowData); |
164
|
|
|
} |
165
|
13 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get all default database values. |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
65 |
|
public function getDefaultDbData() |
173
|
|
|
{ |
174
|
65 |
|
$class = get_called_class(); |
175
|
65 |
|
if (!isset(self::$cachedDefaultDbData[$class])) { |
176
|
6 |
|
self::$cachedDefaultDbData[$class] = []; |
177
|
6 |
|
foreach (array_keys(static::$dbProperties) as $propertyName) { |
178
|
6 |
|
self::$cachedDefaultDbData[$class][$propertyName] = $this->getDefaultDbPropertyValue($propertyName); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
65 |
|
return self::$cachedDefaultDbData[$class]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get default db value (can be overridden if non static default values need to be used). |
187
|
|
|
* |
188
|
|
|
* @param string $propertyName |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
6 |
|
public function getDefaultDbPropertyValue($propertyName) |
192
|
|
|
{ |
193
|
|
|
// A default value is set |
194
|
6 |
|
if (array_key_exists('default', static::$dbProperties[$propertyName])) { |
195
|
4 |
|
$defaultValue = static::$dbProperties[$propertyName]['default']; |
196
|
|
|
// No default value set, use default for type |
197
|
|
|
} else { |
198
|
6 |
|
$defaultValue = self::$typeDefaults[static::$dbProperties[$propertyName]['type']]; |
199
|
|
|
} |
200
|
|
|
|
201
|
6 |
|
return $defaultValue; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return mixed |
206
|
|
|
*/ |
207
|
21 |
|
public function getPrimaryDbValue() |
208
|
|
|
{ |
209
|
21 |
|
if (is_array(static::$primaryDbPropertyKey)) { |
210
|
3 |
|
$primaryValues = []; |
211
|
3 |
|
foreach (static::$primaryDbPropertyKey as $keyPart) { |
212
|
3 |
|
$primaryValues[] = $this->dbData[$keyPart]; |
213
|
|
|
} |
214
|
|
|
|
215
|
3 |
|
return $primaryValues; |
216
|
|
|
} |
217
|
|
|
|
218
|
18 |
|
return $this->dbData[static::$primaryDbPropertyKey]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param mixed $primaryDbValue |
223
|
|
|
*/ |
224
|
17 |
|
public function setPrimaryDbValue($primaryDbValue) |
225
|
|
|
{ |
226
|
17 |
|
if (is_array(static::$primaryDbPropertyKey)) { |
227
|
3 |
|
if (!is_array($primaryDbValue)) { |
228
|
1 |
|
throw new \InvalidArgumentException("Primary db value should be an array"); |
229
|
|
|
} |
230
|
|
|
|
231
|
2 |
|
reset($primaryDbValue); |
232
|
2 |
|
foreach (static::$primaryDbPropertyKey as $keyPart) { |
233
|
2 |
|
$this->dbData[$keyPart] = current($primaryDbValue); |
234
|
2 |
|
next($primaryDbValue); |
235
|
|
|
} |
236
|
|
|
} else { |
237
|
14 |
|
$this->dbData[static::$primaryDbPropertyKey] = $primaryDbValue; |
238
|
|
|
} |
239
|
16 |
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return bool |
243
|
|
|
*/ |
244
|
9 |
|
public function isNewDbEntity() |
245
|
|
|
{ |
246
|
9 |
|
if (is_array(static::$primaryDbPropertyKey)) { |
247
|
|
|
// Multiple column keys have to use explicit force insert because we have no way |
248
|
|
|
// to detect if it's a new entity (can't leave more than one primary field empty on insert because |
249
|
|
|
// db can't have two auto increment columns) |
250
|
1 |
|
throw new \LogicException("Can't detect if multi column primary key is a new entity"); |
251
|
|
|
} |
252
|
|
|
|
253
|
8 |
|
return !$this->getPrimaryDbValue(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
8 |
|
public function shouldInsertOnDbSave() |
260
|
|
|
{ |
261
|
8 |
|
return (!is_array(static::$primaryDbPropertyKey) && $this->isNewDbEntity()) |
262
|
8 |
|
|| $this->shouldForceDbInsertOnSave(); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Set a row field value. |
267
|
|
|
* |
268
|
|
|
* @param string $property |
269
|
|
|
* @param mixed $value |
270
|
|
|
* @param bool $setAsModified |
271
|
|
|
* @param bool $force |
272
|
|
|
*/ |
273
|
25 |
|
protected function setDbValue($property, $value, $setAsModified = true, $force = false) |
274
|
|
|
{ |
275
|
25 |
|
if (!isset(static::$dbProperties[$property])) { |
276
|
1 |
|
throw new \InvalidArgumentException("No database entity property[{$property}] exists"); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
// Don't set type if value is null and allowed (allowed currently indicated by default => null) |
280
|
24 |
|
$nullIsAllowed = (array_key_exists('default', static::$dbProperties[$property]) |
281
|
24 |
|
&& static::$dbProperties[$property]['default'] === null); |
282
|
24 |
|
if (!($value === null && $nullIsAllowed)) { |
283
|
24 |
|
$type = static::$dbProperties[$property]['type']; |
284
|
|
|
// Set null when empty and default is null |
285
|
24 |
|
if ($value === '' && $nullIsAllowed) { |
286
|
1 |
|
$value = null; |
|
|
|
|
287
|
|
|
} else { |
288
|
24 |
|
settype($value, $type); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
24 |
|
if ($this->dbData[$property] !== $value || $force) { |
293
|
22 |
|
$this->dbData[$property] = $value; |
294
|
|
|
|
295
|
22 |
|
if ($setAsModified && !$this->isDbPropertyModified($property)) { |
296
|
15 |
|
$this->modifiedDbProperties[] = $property; |
297
|
|
|
} |
298
|
|
|
} |
299
|
24 |
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Get a database field value. |
303
|
|
|
* |
304
|
|
|
* @param string $property |
305
|
|
|
* @return mixed |
306
|
|
|
*/ |
307
|
8 |
|
protected function getDbValue($property) |
308
|
|
|
{ |
309
|
8 |
|
return $this->dbData[$property]; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Get raw (with underscore as word separator as it is formatted in database) |
314
|
|
|
* field name from a object field property name (camelcased). |
315
|
|
|
* |
316
|
|
|
* @param string $propertyName |
317
|
|
|
* @return string |
318
|
|
|
*/ |
319
|
18 |
|
public static function getDbFieldName($propertyName) |
320
|
|
|
{ |
321
|
18 |
|
if (!isset(self::$cachedDbFieldNames[$propertyName])) { |
322
|
5 |
|
self::$cachedDbFieldNames[$propertyName] = Str::camelToSeparator($propertyName); |
323
|
|
|
} |
324
|
|
|
|
325
|
18 |
|
return self::$cachedDbFieldNames[$propertyName]; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Get object field property name (camelCased) from database field name (underscore separated). |
330
|
|
|
* |
331
|
|
|
* @param string $dbFieldName |
332
|
|
|
* @return string |
333
|
|
|
*/ |
334
|
7 |
|
public static function getDbPropertyName($dbFieldName) |
335
|
|
|
{ |
336
|
7 |
|
if (!isset(self::$cachedDbPropertyNames[$dbFieldName])) { |
337
|
2 |
|
self::$cachedDbPropertyNames[$dbFieldName] = Str::separatorToCamel($dbFieldName); |
338
|
|
|
} |
339
|
|
|
|
340
|
7 |
|
return self::$cachedDbPropertyNames[$dbFieldName]; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @return bool |
345
|
|
|
*/ |
346
|
5 |
|
public function hasModifiedDbProperties() |
347
|
|
|
{ |
348
|
5 |
|
return !empty($this->modifiedDbProperties); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param string $property |
353
|
|
|
* @return bool |
354
|
|
|
*/ |
355
|
16 |
|
public function isDbPropertyModified($property) |
356
|
|
|
{ |
357
|
16 |
|
return in_array($property, $this->modifiedDbProperties); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @return array |
362
|
|
|
*/ |
363
|
5 |
|
public function getModifiedDbData() |
364
|
|
|
{ |
365
|
5 |
|
return array_intersect_key($this->dbData, array_flip($this->modifiedDbProperties)); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @param string $property |
370
|
|
|
*/ |
371
|
|
|
public function clearModifiedDbProperty($property) |
372
|
|
|
{ |
373
|
|
|
if (($key = array_search($property, $this->modifiedDbProperties))) { |
374
|
|
|
unset($this->modifiedDbProperties[$key]); |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
*/ |
380
|
4 |
|
public function clearModifiedDbProperties() |
381
|
|
|
{ |
382
|
4 |
|
$this->modifiedDbProperties = []; |
383
|
4 |
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Magic method used to automate getters & setters for row data. |
387
|
|
|
* |
388
|
|
|
* @param string $name |
389
|
|
|
* @param array $arguments |
390
|
|
|
* @return mixed |
391
|
|
|
*/ |
392
|
19 |
|
public function __call($name, array $arguments = []) |
393
|
|
|
{ |
394
|
19 |
|
$propertyName = lcfirst(substr($name, 3)); |
395
|
|
|
|
396
|
19 |
|
if (strpos($name, 'get') === 0 && isset(static::$dbProperties[$propertyName])) { |
397
|
6 |
|
return $this->getDbValue($propertyName); |
398
|
15 |
|
} elseif (strpos($name, 'set') === 0 && isset(static::$dbProperties[$propertyName])) { |
399
|
14 |
|
$argumentCount = count($arguments); |
400
|
14 |
|
if ($argumentCount >= 1 && $argumentCount <= 3) { |
401
|
13 |
|
return $this->setDbValue($propertyName, ...$arguments); |
402
|
|
|
} else { |
403
|
1 |
|
throw new \BadMethodCallException("Invalid argument count[{$argumentCount}] for {$name}()"); |
404
|
|
|
} |
405
|
|
|
} else { |
406
|
1 |
|
throw new \BadMethodCallException("No method named {$name}()"); |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Set database fields' data. |
412
|
|
|
* |
413
|
|
|
* @param array $data |
414
|
|
|
*/ |
415
|
3 |
|
public function setDbData(array $data) |
416
|
|
|
{ |
417
|
3 |
View Code Duplication |
foreach (array_keys(static::$dbProperties) as $propertyName) { |
|
|
|
|
418
|
3 |
|
if (array_key_exists($propertyName, $data)) { |
419
|
3 |
|
$this->setDbValue($propertyName, $data[$propertyName], true); |
420
|
|
|
} |
421
|
|
|
} |
422
|
3 |
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Set db data from raw database row data with field names in database format. |
426
|
|
|
* |
427
|
|
|
* @param array $rowData |
428
|
|
|
*/ |
429
|
7 |
|
public function setDbDataFromRow(array $rowData) |
430
|
|
|
{ |
431
|
|
|
// If there are less row data than properties, use rows as starting point (optimization) |
432
|
7 |
|
if (count($rowData) < count(static::$dbProperties)) { |
433
|
6 |
|
foreach ($rowData as $dbFieldName => $value) { |
434
|
6 |
|
$propertyName = static::getDbPropertyName($dbFieldName); |
435
|
6 |
|
if (isset(static::$dbProperties[$propertyName])) { |
436
|
6 |
|
$this->setDbValue($propertyName, $value, false); |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
// If there are more row data than properties, use properties as starting point |
440
|
|
|
} else { |
441
|
2 |
View Code Duplication |
foreach (array_keys(static::$dbProperties) as $propertyName) { |
|
|
|
|
442
|
2 |
|
$fieldName = static::getDbFieldName($propertyName); |
443
|
2 |
|
if (array_key_exists($fieldName, $rowData)) { |
444
|
2 |
|
$this->setDbValue($propertyName, $rowData[$fieldName], false); |
445
|
|
|
} |
446
|
|
|
} |
447
|
|
|
} |
448
|
7 |
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* @return array |
452
|
|
|
*/ |
453
|
8 |
|
public function getDbData() |
454
|
|
|
{ |
455
|
8 |
|
return $this->dbData; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* @return array |
460
|
|
|
*/ |
461
|
2 |
|
public function getDbDataWithoutPrimary() |
462
|
|
|
{ |
463
|
2 |
|
$dbDataWithoutPrimary = $this->dbData; |
464
|
|
|
|
465
|
2 |
|
if (is_array(static::$primaryDbPropertyKey)) { |
466
|
1 |
|
foreach (static::$primaryDbPropertyKey as $keyPart) { |
467
|
1 |
|
unset($dbDataWithoutPrimary[$keyPart]); |
468
|
|
|
} |
469
|
|
|
} else { |
470
|
1 |
|
unset($dbDataWithoutPrimary[static::$primaryDbPropertyKey]); |
471
|
|
|
} |
472
|
|
|
|
473
|
2 |
|
return $dbDataWithoutPrimary; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* @param bool $deleteFromDbOnSave |
478
|
|
|
*/ |
479
|
3 |
|
public function setDeleteFromDbOnSave($deleteFromDbOnSave = true) |
480
|
|
|
{ |
481
|
3 |
|
$this->deleteFromDbOnSave = $deleteFromDbOnSave; |
482
|
3 |
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* @return bool |
486
|
|
|
*/ |
487
|
10 |
|
public function shouldBeDeletedFromDbOnSave() |
488
|
|
|
{ |
489
|
10 |
|
return $this->deleteFromDbOnSave; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* @return bool |
494
|
|
|
*/ |
495
|
1 |
|
public function isDeleted() |
496
|
|
|
{ |
497
|
1 |
|
return $this->deleted; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* @param bool $deleted |
502
|
|
|
*/ |
503
|
3 |
|
public function setDeleted($deleted = true) |
504
|
|
|
{ |
505
|
3 |
|
$this->deleted = $deleted; |
506
|
3 |
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* @param bool $forceDbInsertOnSave |
510
|
|
|
*/ |
511
|
5 |
|
public function setForceDbInsertOnSave($forceDbInsertOnSave) |
512
|
|
|
{ |
513
|
5 |
|
$this->forceDbInsertOnSave = $forceDbInsertOnSave; |
514
|
5 |
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* @return bool |
518
|
|
|
*/ |
519
|
9 |
|
public function shouldForceDbInsertOnSave() |
520
|
|
|
{ |
521
|
9 |
|
return $this->forceDbInsertOnSave; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* @param string $propertyName |
526
|
|
|
* @return int|null |
527
|
|
|
*/ |
528
|
6 |
|
public static function getDbPropertyMaxLength($propertyName) |
529
|
|
|
{ |
530
|
6 |
|
return isset(static::$dbProperties[$propertyName]['maxLength']) |
531
|
6 |
|
? static::$dbProperties[$propertyName]['maxLength'] |
532
|
6 |
|
: null; |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
/** |
536
|
|
|
* @param string $propertyName |
537
|
|
|
* @return bool |
538
|
|
|
*/ |
539
|
2 |
|
public static function getDbPropertyRequired($propertyName) |
540
|
|
|
{ |
541
|
2 |
|
return isset(static::$dbProperties[$propertyName]['required']) |
542
|
2 |
|
? static::$dbProperties[$propertyName]['required'] |
543
|
2 |
|
: false; |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* @param string $propertyName |
548
|
|
|
* @return bool |
549
|
|
|
*/ |
550
|
3 |
|
public static function getDbPropertyNonEmpty($propertyName) |
551
|
|
|
{ |
552
|
3 |
|
return isset(static::$dbProperties[$propertyName]['nonEmpty']) |
553
|
2 |
|
? static::$dbProperties[$propertyName]['nonEmpty'] |
554
|
3 |
|
: false; |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Get validator for object's database data. |
559
|
|
|
* |
560
|
|
|
* @param ValidatorTranslatorInterface|SymfonyTranslatorInterface|null $translator |
561
|
|
|
* @return Validator |
562
|
|
|
*/ |
563
|
3 |
|
public function getDbDataValidator($translator = null) |
564
|
|
|
{ |
565
|
3 |
|
$validRuleProperties = Validator::getValidRuleProperties(); |
566
|
3 |
|
$fieldsRuleProperties = []; |
567
|
3 |
|
foreach (static::$dbProperties as $propertyName => $attributes) { |
568
|
|
|
// Always validate if validate is not explicitly set to false |
569
|
3 |
|
if (!isset($attributes['validate']) || $attributes['validate'] === true) { |
570
|
3 |
|
$fieldsRuleProperties[$propertyName] = []; |
571
|
3 |
|
foreach ($validRuleProperties as $ruleName) { |
572
|
3 |
|
if (isset($attributes[$ruleName])) { |
573
|
3 |
|
$fieldsRuleProperties[$propertyName][$ruleName] = $attributes[$ruleName]; |
574
|
|
|
} |
575
|
|
|
} |
576
|
|
|
} |
577
|
|
|
} |
578
|
|
|
|
579
|
3 |
|
$validator = new Validator($fieldsRuleProperties, $translator); |
580
|
|
|
|
581
|
3 |
|
return $validator; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* Validate and (if no error messages) set database data. |
586
|
|
|
* |
587
|
|
|
* @param array $data The data (e.g. from a form post) to be validated and set |
588
|
|
|
* @param ValidatorTranslatorInterface|SymfonyTranslatorInterface|null $translator |
589
|
|
|
* @return array An array with all (if any) of error messages |
590
|
|
|
*/ |
591
|
2 |
|
public function validateAndSetDbData(array $data, $translator = null) |
|
|
|
|
592
|
|
|
{ |
593
|
|
|
// Get arguments this method was called with, to allow passthrough to getDbDataValidator() |
594
|
2 |
|
$args = array_slice(func_get_args(), 1); |
595
|
|
|
|
596
|
2 |
|
$validator = call_user_func_array([$this, 'getDbDataValidator'], $args); |
597
|
2 |
|
$errorMessages = $validator->validate($data); |
598
|
|
|
|
599
|
2 |
|
if (empty($errorMessages)) { |
600
|
|
|
// Set db data from validated data because validator normalizes data as well (like trim) |
601
|
1 |
|
$this->setDbData($validator->getValidatedData()); |
602
|
|
|
} |
603
|
|
|
|
604
|
2 |
|
return $errorMessages; |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
/** |
608
|
|
|
* @return string|array |
609
|
|
|
*/ |
610
|
17 |
|
public static function getPrimaryDbPropertyKey() |
611
|
|
|
{ |
612
|
17 |
|
return static::$primaryDbPropertyKey; |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* @return string|array |
617
|
|
|
*/ |
618
|
9 |
|
public static function getPrimaryDbFieldKey() |
619
|
|
|
{ |
620
|
9 |
|
$primaryDbPropertyKey = static::getPrimaryDbPropertyKey(); |
621
|
|
|
|
622
|
9 |
|
if (is_array($primaryDbPropertyKey)) { |
623
|
2 |
|
$primaryDbFieldKey = []; |
624
|
2 |
|
foreach ($primaryDbPropertyKey as $propertyName) { |
625
|
2 |
|
$primaryDbFieldKey[] = static::getDbFieldName($propertyName); |
626
|
|
|
} |
627
|
|
|
|
628
|
2 |
|
return $primaryDbFieldKey; |
629
|
|
|
} else { |
630
|
7 |
|
return static::getDbFieldName($primaryDbPropertyKey); |
631
|
|
|
} |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
/** |
635
|
|
|
* Return array with db property names. |
636
|
|
|
* |
637
|
|
|
* @param array $exclude |
638
|
|
|
* @return array |
639
|
|
|
*/ |
640
|
1 |
|
public static function getDbPropertyNames(array $exclude = []) |
641
|
|
|
{ |
642
|
1 |
|
$dbPropertyNames = array_keys(static::$dbProperties); |
643
|
|
|
|
644
|
1 |
|
return $exclude ? array_diff($dbPropertyNames, $exclude) : $dbPropertyNames; |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* Return array with raw db field names. |
649
|
|
|
* |
650
|
|
|
* @param array $exclude |
651
|
|
|
* @return array |
652
|
|
|
*/ |
653
|
3 |
|
public static function getDbFieldNames(array $exclude = []) |
654
|
|
|
{ |
655
|
3 |
|
$fieldNames = []; |
656
|
3 |
|
foreach (array_keys(static::$dbProperties) as $propertyName) { |
657
|
3 |
|
$fieldNames[] = static::getDbFieldName($propertyName); |
658
|
|
|
} |
659
|
|
|
|
660
|
3 |
|
return $exclude ? array_diff($fieldNames, $exclude) : $fieldNames; |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
|
664
|
|
|
/** |
665
|
|
|
* Get raw database field names prefixed (id, name becomes t.id, t.name etc.). |
666
|
|
|
* |
667
|
|
|
* @param string $dbTableAlias |
668
|
|
|
* @param array $exclude |
669
|
|
|
* @return array |
670
|
|
|
*/ |
671
|
1 |
|
public static function getPrefixedDbFieldNames($dbTableAlias, array $exclude = []) |
672
|
|
|
{ |
673
|
1 |
|
return Arr::valuesWithPrefix(static::getDbFieldNames($exclude), $dbTableAlias . '.'); |
|
|
|
|
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
/** |
677
|
|
|
* Get database columns transformed from e.g. "productId, date" to "p.product_id AS p_product_id, p.date AS p_date". |
678
|
|
|
* |
679
|
|
|
* @param string $dbTableAlias |
680
|
|
|
* @param array $exclude |
681
|
|
|
* @return array |
682
|
|
|
*/ |
683
|
1 |
|
public static function getAliasedDbFieldNames($dbTableAlias, array $exclude = []) |
684
|
|
|
{ |
685
|
1 |
|
$newArray = []; |
686
|
1 |
|
foreach (static::getDbFieldNames($exclude) as $dbFieldName) { |
687
|
1 |
|
$fromCol = $dbTableAlias . '.' . $dbFieldName; |
688
|
1 |
|
$toCol = $dbTableAlias . '_' . $dbFieldName; |
689
|
1 |
|
$newArray[] = $fromCol . ' AS ' . $toCol; |
690
|
|
|
} |
691
|
|
|
|
692
|
1 |
|
return $newArray; |
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
/** |
696
|
|
|
* Filters a full db item array by it's table alias and the strips the table alias. |
697
|
|
|
* |
698
|
|
|
* @param array $rowData |
699
|
|
|
* @param string $dbTableAlias |
700
|
|
|
* @param bool $skipStrip For cases when you want to filter only (no stripping) |
701
|
|
|
* @return array |
702
|
|
|
*/ |
703
|
1 |
|
public static function filterStripDbRowData(array $rowData, $dbTableAlias, $skipStrip = false) |
704
|
|
|
{ |
705
|
1 |
|
$columnPrefix = $dbTableAlias . '_'; |
706
|
|
|
|
707
|
1 |
|
$filteredAndStrippedRowData = []; |
708
|
1 |
|
foreach ($rowData as $key => $val) { |
709
|
1 |
|
if (strpos($key, $columnPrefix) === 0) { |
710
|
1 |
|
$strippedKey = $skipStrip ? $key : Str::stripLeft($key, $columnPrefix); |
711
|
1 |
|
$filteredAndStrippedRowData[$strippedKey] = $val; |
712
|
|
|
} |
713
|
|
|
} |
714
|
|
|
|
715
|
1 |
|
return $filteredAndStrippedRowData; |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
/** |
719
|
|
|
* @return string |
720
|
|
|
*/ |
721
|
8 |
|
public static function getDbTableName() |
722
|
|
|
{ |
723
|
8 |
|
return static::$dbTableName; |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* Method to handle the serialization of this object. |
728
|
|
|
* |
729
|
|
|
* Implementation of Serializable interface. If descendant private properties |
730
|
|
|
* should be serialized, they need to be visible to this parent (i.e. not private). |
731
|
|
|
* |
732
|
|
|
* @return string |
733
|
|
|
*/ |
734
|
2 |
|
public function serialize() |
735
|
|
|
{ |
736
|
2 |
|
return serialize(get_object_vars($this)); |
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
/** |
740
|
|
|
* Method to handle the unserialization of this object. |
741
|
|
|
* |
742
|
|
|
* Implementation of Serializable interface. If descendant private properties |
743
|
|
|
* should be unserialized, they need to be visible to this parent (i.e. not private). |
744
|
|
|
* |
745
|
|
|
* @param string $serializedObject |
746
|
|
|
*/ |
747
|
1 |
|
public function unserialize($serializedObject) |
748
|
|
|
{ |
749
|
1 |
|
$objectVars = unserialize($serializedObject); |
750
|
|
|
|
751
|
1 |
|
foreach ($objectVars as $key => $value) { |
752
|
1 |
|
$this->{$key} = $value; |
753
|
|
|
} |
754
|
1 |
|
} |
755
|
|
|
|
756
|
|
|
/** |
757
|
|
|
* Merges other object's modified database data into this object. |
758
|
|
|
* |
759
|
|
|
* @param AbstractDbEntity $otherEntity |
760
|
|
|
*/ |
761
|
1 |
|
public function mergeWith(AbstractDbEntity $otherEntity) |
762
|
|
|
{ |
763
|
1 |
|
$dataToMerge = $otherEntity->getModifiedDbData(); |
764
|
1 |
|
$this->setDbData($dataToMerge); |
765
|
1 |
|
} |
766
|
|
|
} |
767
|
|
|
|