GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 8bb85a...ee9939 )
by Andreas
01:57
created

AbstractDbEntity::checkStaticProperties()   B

Complexity

Conditions 9
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 15
cts 15
cp 1
rs 7.756
c 0
b 0
f 0
cc 9
eloc 12
nc 3
nop 0
crap 9
1
<?php
2
/**
3
 * Starlit Db.
4
 *
5
 * @copyright Copyright (c) 2016 Starweb AB
6
 * @license   BSD 3-Clause
7
 */
8
9
namespace Starlit\Db;
10
11
use Starlit\Utils\Str;
12
use Starlit\Utils\Arr;
13
14
/**
15
 * Abstract class to model a single database row into an object.
16
 *
17
 * @author Andreas Nilsson <http://github.com/jandreasn>
18
 */
19
abstract class AbstractDbEntity implements \Serializable
20
{
21
    /**
22
     * The database table name (meant to be overridden).
23
     *
24
     * @var string
25
     */
26
    protected static $dbTableName;
27
28
    /**
29
     * Entity's database properties and their attributes (meant to be overridden).
30
     * Example format:
31
     *
32
     * $dbProperties = [
33
     *     'productId' => ['type' => 'int'],
34
     *     'otherId'   => ['type' => 'int', 'required' => true,],
35
     *     'name'      => ['type' => 'string', 'maxLength' => 10, 'required' => true, 'default' => 'Some name'],
36
     * ];
37
     *
38
     * 'type' => 'int'     Corresponding PHP type (required).
39
     * 'required' => true  The value have to be set (not '', null, false)
40
     * 'nonEmpty' => true  The value should not be empty ('', 0, null)
41
     *
42
     * Properties correspond to database table's columns but words are
43
     * camel cased instead of separated with underscore (_) as in the database.
44
     *
45
     * @var array
46
     */
47
    protected static $dbProperties = [];
48
49
    /**
50
     * Object database field name that is used for primary key (meant to be overridden).
51
     * Should be camel cased as it maps to the dbFields array.
52
     *
53
     * @var string|array
54
     */
55
    protected static $primaryDbPropertyKey;
56
57
    /**
58
     * @var array
59
     */
60
    private static $cachedDefaultDbData = [];
61
62
    /**
63
     * @var array
64
     */
65
    private static $cachedDbPropertyNames;
66
67
    /**
68
     * @var array
69
     */
70
    private static $cachedDbFieldNames;
71
72
    /**
73
     * @var array
74
     */
75
    private static $typeDefaults = [
76
        'string'   => '',
77
        'int'      => 0,
78
        'float'    => 0.0,
79
        'bool'     => false,
80
        'dateTime' => null,
81
    ];
82
83
    /**
84
     * Database row data with field names and their values.
85
     *
86
     * @var array
87
     */
88
    private $dbData = [];
89
90
    /**
91
     * Database fields that has had their value modified since init/load.
92
     *
93
     * @var array
94
     */
95
    private $modifiedDbProperties = [];
96
97
    /**
98
     * @var bool
99
     */
100
    private $deleteFromDbOnSave = false;
101
102
    /**
103
     * @var bool
104
     */
105
    private $deleted = false;
106
107
    /**
108
     * @var bool
109
     */
110
    private $forceDbInsertOnSave = false;
111
112
    /**
113
     * Constructor.
114
     *
115
     * @param mixed $primaryDbValueOrRowData
116
     */
117 68
    public function __construct($primaryDbValueOrRowData = null)
118
    {
119 68
        self::checkStaticProperties();
120
121
        // Set default values
122 67
        $this->dbData = $this->getDefaultDbData();
123
124
        // Override default values with provided values
125 67
        if ($primaryDbValueOrRowData !== null) {
126 13
            $this->setPrimaryDbValueOrRowData($primaryDbValueOrRowData);
127 13
        }
128 67
    }
129
130
    /**
131
     * Make sure that class has all necessary static properties set.
132
     */
133 68
    private static function checkStaticProperties()
134
    {
135 68
        static $checkedClasses = [];
136 68
        if (!in_array(static::class, $checkedClasses)) {
137 8
            if (empty(static::$dbTableName)
138 7
                || empty(static::$dbProperties)
139 7
                || empty(static::$primaryDbPropertyKey)
140 7
                || (is_scalar(static::$primaryDbPropertyKey)
141 7
                    && !isset(static::$dbProperties[static::$primaryDbPropertyKey]['type']))
142 7
                || (is_array(static::$primaryDbPropertyKey)
143 7
                    && !Arr::allIn(static::$primaryDbPropertyKey, array_keys(static::$dbProperties)))
144 8
            ) {
145 1
                throw new \LogicException("All db entity's static properties not set");
146
            }
147 7
            $checkedClasses[] = static::class;
148 7
        }
149 67
    }
150
151
    /**
152
     * @param mixed $primaryDbValueOrRowData
153
     */
154 13
    public function setPrimaryDbValueOrRowData($primaryDbValueOrRowData = null)
155
    {
156
        // Row data would ba an associative array (not sequential, that would indicate a multi column primary key)
157 13
        if (is_array($primaryDbValueOrRowData) && !isset($primaryDbValueOrRowData[0])) {
158 1
            $this->setDbDataFromRow($primaryDbValueOrRowData);
159 1
        } else {
160 12
            $this->setPrimaryDbValue($primaryDbValueOrRowData);
161
        }
162 13
    }
163
164
    /**
165
     * Get all default database values.
166
     *
167
     * @return array
168
     */
169 67
    public function getDefaultDbData()
170
    {
171 67
        $class = get_called_class();
172 67
        if (!isset(self::$cachedDefaultDbData[$class])) {
173 7
            self::$cachedDefaultDbData[$class] = [];
174 7
            foreach (array_keys(static::$dbProperties) as $propertyName) {
175 7
                self::$cachedDefaultDbData[$class][$propertyName] = $this->getDefaultDbPropertyValue($propertyName);
176 7
            }
177 7
        }
178
179 67
        return self::$cachedDefaultDbData[$class];
180
    }
181
182
    /**
183
     * Get default db value (can be overridden if non static default values need to be used).
184
     *
185
     * @param string $propertyName
186
     * @return mixed
187
     */
188 7
    public function getDefaultDbPropertyValue($propertyName)
189
    {
190
        // A default value is set
191 7
        if (array_key_exists('default', static::$dbProperties[$propertyName])) {
192 5
            $defaultValue = static::$dbProperties[$propertyName]['default'];
193
        // No default value set, use default for type
194 5
        } else {
195 7
            $defaultValue = self::$typeDefaults[static::$dbProperties[$propertyName]['type']];
196
        }
197
198 7
        return $defaultValue;
199
    }
200
201
    /**
202
     * @return mixed
203
     */
204 22
    public function getPrimaryDbValue()
205
    {
206 22
        if (is_array(static::$primaryDbPropertyKey)) {
207 3
            $primaryValues = [];
208 3
            foreach (static::$primaryDbPropertyKey as $keyPart) {
209 3
                $primaryValues[] = $this->dbData[$keyPart];
210 3
            }
211
212 3
            return $primaryValues;
213
        }
214
215 19
        return $this->dbData[static::$primaryDbPropertyKey];
216
    }
217
218
    /**
219
     * @param mixed $primaryDbValue
220
     */
221 17
    public function setPrimaryDbValue($primaryDbValue)
222
    {
223 17
        if (is_array(static::$primaryDbPropertyKey)) {
224 3
            if (!is_array($primaryDbValue)) {
225 1
                throw new \InvalidArgumentException("Primary db value should be an array");
226
            }
227
228 2
            reset($primaryDbValue);
229 2
            foreach (static::$primaryDbPropertyKey as $keyPart) {
230 2
                $this->dbData[$keyPart] = current($primaryDbValue);
231 2
                next($primaryDbValue);
232 2
            }
233 2
        } else {
234 14
            $this->dbData[static::$primaryDbPropertyKey] = $primaryDbValue;
235
        }
236 16
    }
237
238
    /**
239
     * @return bool
240
     */
241 10
    public function isNewDbEntity()
242
    {
243 10
        if (is_array(static::$primaryDbPropertyKey)) {
244
            // Multiple column keys have to use explicit force insert because we have no way
245
            // to detect if it's a new entity (can't leave more than one primary field empty on insert because
246
            // db can't have two auto increment columns)
247 1
            throw new \LogicException("Can't detect if multi column primary key is a new entity");
248
        }
249
250 9
        return !$this->getPrimaryDbValue();
251
    }
252
253
    /**
254
     * @return bool
255
     */
256 9
    public function shouldInsertOnDbSave()
257
    {
258 9
        return (!is_array(static::$primaryDbPropertyKey) && $this->isNewDbEntity())
259 9
            || $this->shouldForceDbInsertOnSave();
260
    }
261
262
    /**
263
     * Set a row field value.
264
     *
265
     * @param string $property
266
     * @param mixed  $value
267
     * @param bool   $setAsModified
268
     * @param bool   $force
269
     */
270 27
    protected function setDbValue($property, $value, $setAsModified = true, $force = false)
271
    {
272 27
        if (!isset(static::$dbProperties[$property])) {
273 1
            throw new \InvalidArgumentException("No database entity property[{$property}] exists");
274
        }
275
276
        // Don't set type if value is null and allowed (allowed currently indicated by default => null)
277 26
        $nullIsAllowed = (array_key_exists('default', static::$dbProperties[$property])
278 26
            && static::$dbProperties[$property]['default'] === null);
279 26
        if (!($value === null && $nullIsAllowed)) {
280 25
            $type = static::$dbProperties[$property]['type'];
281
            // Set null when empty and default is null
282 25
            if ($value === '' && $nullIsAllowed) {
283 1
                 $value = null;
284 25
            } elseif ($type === 'dateTime') {
285 1
                if (!($value instanceof \DateTimeInterface)) {
286 1
                    $value = $this->createDateTimeDbValue($value);
287 1
                }
288 1
            } else {
289 24
                settype($value, $type);
290
            }
291 25
        }
292
293 26
        if ($this->dbData[$property] !== $value || $force) {
294 23
            $this->dbData[$property] = $value;
295
296 23
            if ($setAsModified && !$this->isDbPropertyModified($property)) {
297 16
                $this->modifiedDbProperties[] = $property;
298 16
            }
299 23
        }
300 26
    }
301
302
    /**
303
     * @param string $value
304
     * @return \DateTime|\Carbon\Carbon|null
305
     */
306 1
    protected function createDateTimeDbValue($value)
307
    {
308 1
        static $carbonExists = null;
309
        if ($carbonExists === true
310 1
            || ($carbonExists === null && ($carbonExists = class_exists(\Carbon\Carbon::class)))
311 1
        ) {
312 1
            return new \Carbon\Carbon($value);
313
        }
314
315
        return new \DateTime($value);
316
    }
317
318
    /**
319
     * Get a database field value.
320
     *
321
     * @param string $property
322
     * @return mixed
323
     */
324 7
    protected function getDbValue($property)
325
    {
326 7
        return $this->dbData[$property];
327
    }
328
329
    /**
330
     * Get raw (with underscore as word separator as it is formatted in database)
331
     * field name from a object field property name (camelcased).
332
     *
333
     * @param string $propertyName
334
     * @return string
335
     */
336 19
    public static function getDbFieldName($propertyName)
337
    {
338 19
        if (!isset(self::$cachedDbFieldNames[$propertyName])) {
339 6
            self::$cachedDbFieldNames[$propertyName] = Str::camelToSeparator($propertyName);
340 6
        }
341
342 19
        return self::$cachedDbFieldNames[$propertyName];
343
    }
344
345
    /**
346
     * Get object field property name (camelCased) from database field name (underscore separated).
347
     *
348
     * @param string $dbFieldName
349
     * @return string
350
     */
351 7
    public static function getDbPropertyName($dbFieldName)
352
    {
353 7
        if (!isset(self::$cachedDbPropertyNames[$dbFieldName])) {
354 2
            self::$cachedDbPropertyNames[$dbFieldName] = Str::separatorToCamel($dbFieldName);
355 2
        }
356
357 7
        return self::$cachedDbPropertyNames[$dbFieldName];
358
    }
359
360
    /**
361
     * @return bool
362
     */
363 5
    public function hasModifiedDbProperties()
364
    {
365 5
        return !empty($this->modifiedDbProperties);
366
    }
367
368
    /**
369
     * @param string $property
370
     * @return bool
371
     */
372 17
    public function isDbPropertyModified($property)
373
    {
374 17
        return in_array($property, $this->modifiedDbProperties);
375
    }
376
377
    /**
378
     * @return array
379
     */
380 6
    public function getModifiedDbData()
381
    {
382 6
        return array_intersect_key($this->dbData, array_flip($this->modifiedDbProperties));
383
    }
384
385
    /**
386
     * @param string $property
387
     */
388
    public function clearModifiedDbProperty($property)
389
    {
390
        if (($key = array_search($property, $this->modifiedDbProperties))) {
391
            unset($this->modifiedDbProperties[$key]);
392
        }
393
    }
394
395 4
    public function clearModifiedDbProperties()
396
    {
397 4
        $this->modifiedDbProperties = [];
398 4
    }
399
400 1
    public function setAllDbPropertiesAsModified()
401
    {
402 1
        $this->modifiedDbProperties = array_keys(static::$dbProperties);
403 1
    }
404
405
    /**
406
     * Magic method used to automate getters & setters for row data.
407
     *
408
     * @param string $name
409
     * @param array  $arguments
410
     * @return mixed
411
     */
412 19
    public function __call($name, array $arguments = [])
413
    {
414 19
        $propertyName = lcfirst(substr($name, 3));
415
416 19
        if (strpos($name, 'get') === 0 && isset(static::$dbProperties[$propertyName])) {
417 5
            return $this->getDbValue($propertyName);
418 17
        } elseif (strpos($name, 'set') === 0 && isset(static::$dbProperties[$propertyName])) {
419 16
            $argumentCount = count($arguments);
420 16
            if ($argumentCount >= 1 && $argumentCount <= 3) {
421 15
                return $this->setDbValue($propertyName, ...$arguments);
422
            } else {
423 1
                throw new \BadMethodCallException("Invalid argument count[{$argumentCount}] for {$name}()");
424
            }
425
        } else {
426 1
            throw new \BadMethodCallException("No method named {$name}()");
427
        }
428
    }
429
430
    /**
431
     * Set database fields' data.
432
     *
433
     * @param array $data
434
     */
435 3
    public function setDbData(array $data)
436
    {
437 3 View Code Duplication
        foreach (array_keys(static::$dbProperties) as $propertyName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
438 3
            if (array_key_exists($propertyName, $data)) {
439 3
                $this->setDbValue($propertyName, $data[$propertyName], true);
440 3
            }
441 3
        }
442 3
    }
443
444
    /**
445
     * Set db data from raw database row data with field names in database format.
446
     *
447
     * @param array $rowData
448
     */
449 7
    public function setDbDataFromRow(array $rowData)
450
    {
451
        // If there are less row data than properties, use rows as starting point (optimization)
452 7
        if (count($rowData) < count(static::$dbProperties)) {
453 6
            foreach ($rowData as $dbFieldName => $value) {
454 6
                $propertyName = static::getDbPropertyName($dbFieldName);
455 6
                if (isset(static::$dbProperties[$propertyName])) {
456 6
                    $this->setDbValue($propertyName, $value, false);
457 6
                }
458 6
            }
459
        // If there are more row data than properties, use properties as starting point
460 6
        } else {
461 2 View Code Duplication
            foreach (array_keys(static::$dbProperties) as $propertyName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
462 2
                $fieldName = static::getDbFieldName($propertyName);
463 2
                if (array_key_exists($fieldName, $rowData)) {
464 2
                    $this->setDbValue($propertyName, $rowData[$fieldName], false);
465 2
                }
466 2
            }
467
        }
468 7
    }
469
470
    /**
471
     * @return array
472
     */
473 11
    public function getDbData()
474
    {
475 11
        return $this->dbData;
476
    }
477
478
    /**
479
     * @return array
480
     */
481 1
    public function getDbRowData()
482
    {
483 1
        $rowData = [];
484 1
        foreach ($this->getDbData() as $propertyName => $value) {
485 1
            $dbFieldName = static::getDbFieldName($propertyName);
486 1
            $rowData[$dbFieldName] = $value;
487 1
        }
488
489 1
        return $rowData;
490
    }
491
492
    /**
493
     * @return array
494
     */
495 2
    public function getDbDataWithoutPrimary()
496
    {
497 2
        $dbDataWithoutPrimary = $this->dbData;
498
499 2
        if (is_array(static::$primaryDbPropertyKey)) {
500 1
            foreach (static::$primaryDbPropertyKey as $keyPart) {
501 1
                unset($dbDataWithoutPrimary[$keyPart]);
502 1
            }
503 1
        } else {
504 1
            unset($dbDataWithoutPrimary[static::$primaryDbPropertyKey]);
505
        }
506
507 2
        return $dbDataWithoutPrimary;
508
    }
509
510
    /**
511
     * @param bool $deleteFromDbOnSave
512
     */
513 3
    public function setDeleteFromDbOnSave($deleteFromDbOnSave = true)
514
    {
515 3
        $this->deleteFromDbOnSave = $deleteFromDbOnSave;
516 3
    }
517
518
    /**
519
     * @return bool
520
     */
521 11
    public function shouldBeDeletedFromDbOnSave()
522
    {
523 11
        return $this->deleteFromDbOnSave;
524
    }
525
526
    /**
527
     * @return bool
528
     */
529 1
    public function isDeleted()
530
    {
531 1
        return $this->deleted;
532
    }
533
534
    /**
535
     * @param bool $deleted
536
     */
537 3
    public function setDeleted($deleted = true)
538
    {
539 3
        $this->deleted = $deleted;
540 3
    }
541
542
    /**
543
     * @param bool $forceDbInsertOnSave
544
     */
545 5
    public function setForceDbInsertOnSave($forceDbInsertOnSave)
546
    {
547 5
        $this->forceDbInsertOnSave = $forceDbInsertOnSave;
548 5
    }
549
550
    /**
551
     * @return bool
552
     */
553 10
    public function shouldForceDbInsertOnSave()
554
    {
555 10
        return $this->forceDbInsertOnSave;
556
    }
557
558
    /**
559
     * @return array
560
     */
561 1
    public static function getDbProperties()
562
    {
563 1
        return static::$dbProperties;
564
    }
565
566
    /**
567
     * @param string $propertyName
568
     * @return int|null
569
     */
570 6
    public static function getDbPropertyMaxLength($propertyName)
571
    {
572 6
        return isset(static::$dbProperties[$propertyName]['maxLength'])
573 6
            ? static::$dbProperties[$propertyName]['maxLength']
574 6
            : null;
575
    }
576
577
    /**
578
     * @param string $propertyName
579
     * @return bool
580
     */
581 3
    public static function getDbPropertyRequired($propertyName)
582
    {
583 3
        return isset(static::$dbProperties[$propertyName]['required'])
584 3
            ? static::$dbProperties[$propertyName]['required']
585 3
            : false;
586
    }
587
588
    /**
589
     * @param string $propertyName
590
     * @return bool
591
     */
592 4
    public static function getDbPropertyNonEmpty($propertyName)
593
    {
594 4
        return isset(static::$dbProperties[$propertyName]['nonEmpty'])
595 4
            ? static::$dbProperties[$propertyName]['nonEmpty']
596 4
            : false;
597
    }
598
599
    /**
600
     * @return string|array
601
     */
602 18
    public static function getPrimaryDbPropertyKey()
603
    {
604 18
        return static::$primaryDbPropertyKey;
605
    }
606
607
    /**
608
     * @return string|array
609
     */
610 10
    public static function getPrimaryDbFieldKey()
611
    {
612 10
        $primaryDbPropertyKey = static::getPrimaryDbPropertyKey();
613
614 10
        if (is_array($primaryDbPropertyKey)) {
615 3
            $primaryDbFieldKey = [];
616 3
            foreach ($primaryDbPropertyKey as $propertyName) {
617 3
                $primaryDbFieldKey[] = static::getDbFieldName($propertyName);
618 3
            }
619
620 3
            return $primaryDbFieldKey;
621
        } else {
622 7
            return static::getDbFieldName($primaryDbPropertyKey);
623
        }
624
    }
625
626
    /**
627
     * Return array with db property names.
628
     *
629
     * @param array $exclude
630
     * @return array
631
     */
632 1
    public static function getDbPropertyNames(array $exclude = [])
633
    {
634 1
        $dbPropertyNames = array_keys(static::$dbProperties);
635
636 1
        return $exclude ? array_diff($dbPropertyNames, $exclude) : $dbPropertyNames;
637
    }
638
639
    /**
640
     * Return array with raw db field names.
641
     *
642
     * @param array $exclude
643
     * @return array
644
     */
645 3
    public static function getDbFieldNames(array $exclude = [])
646
    {
647 3
        $fieldNames = [];
648 3
        foreach (array_keys(static::$dbProperties) as $propertyName) {
649 3
            $fieldNames[] = static::getDbFieldName($propertyName);
650 3
        }
651
652 3
        return $exclude ? array_diff($fieldNames, $exclude) : $fieldNames;
653
    }
654
655
656
    /**
657
     * Get raw database field names prefixed (id, name becomes t.id, t.name etc.).
658
     *
659
     * @param string $dbTableAlias
660
     * @param array  $exclude
661
     * @return array
662
     */
663 1
    public static function getPrefixedDbFieldNames($dbTableAlias, array $exclude = [])
664
    {
665 1
        return Arr::valuesWithPrefix(static::getDbFieldNames($exclude), $dbTableAlias . '.');
666
    }
667
668
    /**
669
     * Get database columns transformed from e.g. "productId, date" to "p.product_id AS p_product_id, p.date AS p_date".
670
     *
671
     * @param string $dbTableAlias
672
     * @param array  $exclude
673
     * @return array
674
     */
675 1
    public static function getAliasedDbFieldNames($dbTableAlias, array $exclude = [])
676
    {
677 1
        $newArray = [];
678 1
        foreach (static::getDbFieldNames($exclude) as $dbFieldName) {
679 1
            $fromCol = $dbTableAlias . '.' . $dbFieldName;
680 1
            $toCol = $dbTableAlias . '_' . $dbFieldName;
681 1
            $newArray[] = $fromCol . ' AS ' . $toCol;
682 1
        }
683
684 1
        return $newArray;
685
    }
686
687
    /**
688
     * Filters a full db item array by it's table alias and the strips the table alias.
689
     *
690
     * @param array  $rowData
691
     * @param string $dbTableAlias
692
     * @param bool   $skipStrip For cases when you want to filter only (no stripping)
693
     * @return array
694
     */
695 1
    public static function filterStripDbRowData(array $rowData, $dbTableAlias, $skipStrip = false)
696
    {
697 1
        $columnPrefix = $dbTableAlias . '_';
698
699 1
        $filteredAndStrippedRowData = [];
700 1
        foreach ($rowData as $key => $val) {
701 1
            if (strpos($key, $columnPrefix) === 0) {
702 1
                $strippedKey = $skipStrip ? $key : Str::stripLeft($key, $columnPrefix);
703 1
                $filteredAndStrippedRowData[$strippedKey] = $val;
704 1
            }
705 1
        }
706
707 1
        return $filteredAndStrippedRowData;
708
    }
709
710
    /**
711
     * @return string
712
     */
713 8
    public static function getDbTableName()
714
    {
715 8
        return static::$dbTableName;
716
    }
717
718
    /**
719
     * Method to handle the serialization of this object.
720
     *
721
     * Implementation of Serializable interface. If descendant private properties
722
     * should be serialized, they need to be visible to this parent (i.e. not private).
723
     *
724
     * @return string
725
     */
726 2
    public function serialize()
727
    {
728 2
        return serialize(get_object_vars($this));
729
    }
730
731
    /**
732
     * Method to handle the unserialization of this object.
733
     *
734
     * Implementation of Serializable interface. If descendant private properties
735
     * should be unserialized, they need to be visible to this parent (i.e. not private).
736
     *
737
     * @param string $serializedObject
738
     */
739 1
    public function unserialize($serializedObject)
740
    {
741 1
        $objectVars = unserialize($serializedObject);
742
743 1
        foreach ($objectVars as $key => $value) {
744 1
            $this->{$key} = $value;
745 1
        }
746 1
    }
747
748
    /**
749
     * Merges other object's modified database data into this object.
750
     *
751
     * @param AbstractDbEntity $otherEntity
752
     */
753 1
    public function mergeWith(AbstractDbEntity $otherEntity)
754
    {
755 1
        $dataToMerge = $otherEntity->getModifiedDbData();
756 1
        $this->setDbData($dataToMerge);
757 1
    }
758
}
759