/
EntityTrait.php
897 lines (823 loc) · 26.6 KB
/
EntityTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Datasource;
use Cake\Collection\Collection;
use Cake\Utility\Inflector;
use InvalidArgumentException;
use Traversable;
/**
* An entity represents a single result row from a repository. It exposes the
* methods for retrieving and storing properties associated in this row.
*/
trait EntityTrait
{
/**
* Holds all properties and their values for this entity
*
* @var array
*/
protected $_properties = [];
/**
* Holds all properties that have been changed and their original values for this entity
*
* @var array
*/
protected $_original = [];
/**
* List of property names that should **not** be included in JSON or Array
* representations of this Entity.
*
* @var array
*/
protected $_hidden = [];
/**
* List of computed or virtual fields that **should** be included in JSON or array
* representations of this Entity. If a field is present in both _hidden and _virtual
* the field will **not** be in the array/json versions of the entity.
*
* @var array
*/
protected $_virtual = [];
/**
* Holds the name of the class for the instance object
*
* @var string
*/
protected $_className;
/**
* Holds a list of the properties that were modified or added after this object
* was originally created.
*
* @var array
*/
protected $_dirty = [];
/**
* Holds a cached list of methods that exist in the instanced class
*
* @var array
*/
protected static $_accessors = [];
/**
* Indicates whether or not this entity is yet to be persisted.
* Entities default to assuming they are new. You can use Table::persisted()
* to set the new flag on an entity based on records in the database.
*
* @var bool
*/
protected $_new = true;
/**
* List of errors per field as stored in this object
*
* @var array
*/
protected $_errors = [];
/**
* Map of properties in this entity that can be safely assigned, each
* property name points to a boolean indicating its status. An empty array
* means no properties are accessible
*
* The special property '\*' can also be mapped, meaning that any other property
* not defined in the map will take its value. For example, `'\*' => true`
* means that any property not defined in the map will be accessible by default
*
* @var array
*/
protected $_accessible = ['*' => true];
/**
* The alias of the repository this entity came from
*
* @var string
*/
protected $_registryAlias;
/**
* Holds a list of properties that were mutated using the get accessor
*
* @var array
*/
protected $_mutated = [];
/**
* Magic getter to access properties that have been set in this entity
*
* @param string $property Name of the property to access
* @return mixed
*/
public function &__get($property)
{
return $this->get($property);
}
/**
* Magic setter to add or edit a property in this entity
*
* @param string $property The name of the property to set
* @param mixed $value The value to set to the property
* @return void
*/
public function __set($property, $value)
{
$this->set($property, $value);
}
/**
* Returns whether this entity contains a property named $property
* regardless of if it is empty.
*
* @param string $property The property to check.
* @return bool
* @see \Cake\ORM\Entity::has()
*/
public function __isset($property)
{
return $this->has($property);
}
/**
* Removes a property from this entity
*
* @param string $property The property to unset
* @return void
*/
public function __unset($property)
{
$this->unsetProperty($property);
}
/**
* Sets a single property inside this entity.
*
* ### Example:
*
* ``$entity->set('name', 'Andrew');``
*
* It is also possible to mass-assign multiple properties to this entity
* with one call by passing a hashed array as properties in the form of
* property => value pairs
*
* ### Example:
*
* ```
* $entity->set(['name' => 'andrew', 'id' => 1]);
* echo $entity->name // prints andrew
* echo $entity->id // prints 1
* ```
*
* Some times it is handy to bypass setter functions in this entity when assigning
* properties. You can achieve this by disabling the `setter` option using the
* `$options` parameter:
*
* ```
* $entity->set('name', 'Andrew', ['setter' => false]);
* $entity->set(['name' => 'Andrew', 'id' => 1], ['setter' => false]);
* ```
*
* Mass assignment should be treated carefully when accepting user input, by default
* entities will guard all fields when properties are assigned in bulk. You can disable
* the guarding for a single set call with the `guard` option:
*
* ```
* $entity->set(['name' => 'Andrew', 'id' => 1], ['guard' => true]);
* ```
*
* You do not need to use the guard option when assigning properties individually:
*
* ```
* // No need to use the guard option.
* $entity->set('name', 'Andrew');
* ```
*
* @param string|array $property the name of property to set or a list of
* properties with their respective values
* @param mixed $value The value to set to the property or an array if the
* first argument is also an array, in which case will be treated as $options
* @param array $options options to be used for setting the property. Allowed option
* keys are `setter` and `guard`
* @return $this
* @throws \InvalidArgumentException
*/
public function set($property, $value = null, array $options = [])
{
$isString = is_string($property);
if ($isString && $property !== '') {
$guard = false;
$property = [$property => $value];
} else {
$guard = true;
$options = (array)$value;
}
if (!is_array($property)) {
throw new InvalidArgumentException('Cannot set an empty property');
}
$options += ['setter' => true, 'guard' => $guard];
foreach ($property as $p => $value) {
if ($options['guard'] === true && !$this->accessible($p)) {
continue;
}
unset($this->_mutated[$p]);
$this->dirty($p, true);
if (!isset($this->_original[$p]) &&
isset($this->_properties[$p]) &&
$this->_properties[$p] !== $value
) {
$this->_original[$p] = $this->_properties[$p];
}
if (!$options['setter']) {
$this->_properties[$p] = $value;
continue;
}
$setter = '_set' . Inflector::camelize($p);
if ($this->_methodExists($setter)) {
$value = $this->{$setter}($value);
}
$this->_properties[$p] = $value;
}
return $this;
}
/**
* Returns the value of a property by name
*
* @param string $property the name of the property to retrieve
* @return mixed
* @throws \InvalidArgumentException if an empty property name is passed
*/
public function &get($property)
{
if (!strlen((string)$property)) {
throw new InvalidArgumentException('Cannot get an empty property');
}
if (array_key_exists($property, $this->_mutated)) {
return $this->_mutated[$property];
}
$value = null;
$method = '_get' . Inflector::camelize($property);
if (isset($this->_properties[$property])) {
$value =& $this->_properties[$property];
}
if ($this->_methodExists($method)) {
$result = $this->{$method}($value);
$this->_mutated[$property] = $result;
return $result;
}
return $value;
}
/**
* Returns the value of an original property by name
*
* @param string $property the name of the property for which original value is retrieved.
* @return mixed
* @throws \InvalidArgumentException if an empty property name is passed.
*/
public function getOriginal($property)
{
if (!strlen((string)$property)) {
throw new InvalidArgumentException('Cannot get an empty property');
}
if (isset($this->_original[$property])) {
return $this->_original[$property];
}
return $this->get($property);
}
/**
* Returns whether this entity contains a property named $property
* regardless of if it is empty.
*
* ### Example:
*
* ```
* $entity = new Entity(['id' => 1, 'name' => null]);
* $entity->has('id'); // true
* $entity->has('name'); // false
* $entity->has('last_name'); // false
* ```
*
* When checking multiple properties. All properties must not be null
* in order for true to be returned.
*
* @param string|array $property The property or properties to check.
* @return bool
*/
public function has($property)
{
foreach ((array)$property as $prop) {
if ($this->get($prop) === null) {
return false;
}
}
return true;
}
/**
* Removes a property or list of properties from this entity
*
* ### Examples:
*
* ```
* $entity->unsetProperty('name');
* $entity->unsetProperty(['name', 'last_name']);
* ```
*
* @param string|array $property The property to unset.
* @return $this
*/
public function unsetProperty($property)
{
$property = (array)$property;
foreach ($property as $p) {
unset($this->_properties[$p]);
unset($this->_dirty[$p]);
}
return $this;
}
/**
* Get/Set the hidden properties on this entity.
*
* If the properties argument is null, the currently hidden properties
* will be returned. Otherwise the hidden properties will be set.
*
* @param null|array $properties Either an array of properties to hide or null to get properties
* @return array|$this
*/
public function hiddenProperties($properties = null)
{
if ($properties === null) {
return $this->_hidden;
}
$this->_hidden = $properties;
return $this;
}
/**
* Get/Set the virtual properties on this entity.
*
* If the properties argument is null, the currently virtual properties
* will be returned. Otherwise the virtual properties will be set.
*
* @param null|array $properties Either an array of properties to treat as virtual or null to get properties
* @return array|$this
*/
public function virtualProperties($properties = null)
{
if ($properties === null) {
return $this->_virtual;
}
$this->_virtual = $properties;
return $this;
}
/**
* Get the list of visible properties.
*
* The list of visible properties is all standard properties
* plus virtual properties minus hidden properties.
*
* @return array A list of properties that are 'visible' in all
* representations.
*/
public function visibleProperties()
{
$properties = array_keys($this->_properties);
$properties = array_merge($properties, $this->_virtual);
return array_diff($properties, $this->_hidden);
}
/**
* Returns an array with all the properties that have been set
* to this entity
*
* This method will recursively transform entities assigned to properties
* into arrays as well.
*
* @return array
*/
public function toArray()
{
$result = [];
foreach ($this->visibleProperties() as $property) {
$value = $this->get($property);
if (is_array($value) && isset($value[0]) && $value[0] instanceof EntityInterface) {
$result[$property] = [];
foreach ($value as $k => $entity) {
$result[$property][$k] = $entity->toArray();
}
} elseif ($value instanceof EntityInterface) {
$result[$property] = $value->toArray();
} else {
$result[$property] = $value;
}
}
return $result;
}
/**
* Returns the properties that will be serialized as JSON
*
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}
/**
* Implements isset($entity);
*
* @param mixed $offset The offset to check.
* @return bool Success
*/
public function offsetExists($offset)
{
return $this->has($offset);
}
/**
* Implements $entity[$offset];
*
* @param mixed $offset The offset to get.
* @return mixed
*/
public function &offsetGet($offset)
{
return $this->get($offset);
}
/**
* Implements $entity[$offset] = $value;
*
* @param mixed $offset The offset to set.
* @param mixed $value The value to set.
* @return void
*/
public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}
/**
* Implements unset($result[$offset);
*
* @param mixed $offset The offset to remove.
* @return void
*/
public function offsetUnset($offset)
{
$this->unsetProperty($offset);
}
/**
* Determines whether a method exists in this class
*
* @param string $method the method to check for existence
* @return bool true if method exists
*/
protected function _methodExists($method)
{
if (empty(static::$_accessors[$this->_className])) {
static::$_accessors[$this->_className] = array_flip(get_class_methods($this));
}
return isset(static::$_accessors[$this->_className][$method]);
}
/**
* Returns an array with the requested properties
* stored in this entity, indexed by property name
*
* @param array $properties list of properties to be returned
* @param bool $onlyDirty Return the requested property only if it is dirty
* @return array
*/
public function extract(array $properties, $onlyDirty = false)
{
$result = [];
foreach ($properties as $property) {
if (!$onlyDirty || $this->dirty($property)) {
$result[$property] = $this->get($property);
}
}
return $result;
}
/**
* Returns an array with the requested original properties
* stored in this entity, indexed by property name.
*
* Properties that are unchanged from their original value will be included in the
* return of this method.
*
* @param array $properties List of properties to be returned
* @return array
*/
public function extractOriginal(array $properties)
{
$result = [];
foreach ($properties as $property) {
$original = $this->getOriginal($property);
if ($original !== null) {
$result[$property] = $original;
}
}
return $result;
}
/**
* Returns an array with only the original properties
* stored in this entity, indexed by property name.
*
* This method will only return properties that have been modified since
* the entity was built. Unchanged properties will be omitted.
*
* @param array $properties List of properties to be returned
* @return array
*/
public function extractOriginalChanged(array $properties)
{
$result = [];
foreach ($properties as $property) {
$original = $this->getOriginal($property);
if ($original !== null && $original !== $this->get($property)) {
$result[$property] = $original;
}
}
return $result;
}
/**
* Sets the dirty status of a single property. If called with no second
* argument, it will return whether the property was modified or not
* after the object creation.
*
* When called with no arguments it will return whether or not there are any
* dirty property in the entity
*
* @param string $property the field to set or check status for
* @param null|bool $isDirty true means the property was changed, false means
* it was not changed and null will make the function return current state
* for that property
* @return bool whether the property was changed or not
*/
public function dirty($property = null, $isDirty = null)
{
if ($property === null) {
return !empty($this->_dirty);
}
if ($isDirty === null) {
return isset($this->_dirty[$property]);
}
if ($isDirty === false) {
unset($this->_dirty[$property]);
return false;
}
$this->_dirty[$property] = true;
unset($this->_errors[$property]);
return true;
}
/**
* Sets the entire entity as clean, which means that it will appear as
* no properties being modified or added at all. This is an useful call
* for an initial object hydration
*
* @return void
*/
public function clean()
{
$this->_dirty = [];
$this->_errors = [];
}
/**
* Returns whether or not this entity has already been persisted.
* This method can return null in the case there is no prior information on
* the status of this entity.
*
* If called with a boolean it will set the known status of this instance,
* true means that the instance is not yet persisted in the database, false
* that it already is.
*
* @param bool|null $new true if it is known this instance was persisted
* @return bool Whether or not the entity has been persisted.
*/
public function isNew($new = null)
{
if ($new === null) {
return $this->_new;
}
$new = (bool)$new;
if ($new) {
foreach ($this->_properties as $k => $p) {
$this->_dirty[$k] = true;
}
}
return $this->_new = $new;
}
/**
* Sets the error messages for a field or a list of fields. When called
* without the second argument it returns the validation
* errors for the specified fields. If called with no arguments it returns
* all the validation error messages stored in this entity and any other nested
* entity.
*
* ### Example
*
* ```
* // Sets the error messages for a single field
* $entity->errors('salary', ['must be numeric', 'must be a positive number']);
*
* // Returns the error messages for a single field
* $entity->errors('salary');
*
* // Returns all error messages indexed by field name
* $entity->errors();
*
* // Sets the error messages for multiple fields at once
* $entity->errors(['salary' => ['message'], 'name' => ['another message']);
* ```
*
* When used as a setter, this method will return this entity instance for method
* chaining.
*
* @param string|array|null $field The field to get errors for, or the array of errors to set.
* @param string|array|null $errors The errors to be set for $field
* @param bool $overwrite Whether or not to overwrite pre-existing errors for $field
* @return array|$this
*/
public function errors($field = null, $errors = null, $overwrite = false)
{
if ($field === null) {
$diff = array_diff_key($this->_properties, $this->_errors);
return $this->_errors + (new Collection($diff))
->filter(function ($value) {
return is_array($value) || $value instanceof EntityInterface;
})
->map(function ($value) {
return $this->_readError($value);
})
->filter()
->toArray();
}
if (is_string($field) && $errors === null) {
$errors = isset($this->_errors[$field]) ? $this->_errors[$field] : [];
if ($errors) {
return $errors;
}
return $this->_nestedErrors($field);
}
if (!is_array($field)) {
$field = [$field => $errors];
}
foreach ($field as $f => $error) {
$this->_errors += [$f => []];
$this->_errors[$f] = $overwrite ?
(array)$error :
array_merge($this->_errors[$f], (array)$error);
}
return $this;
}
/**
* Auxiliary method for getting errors in nested entities
*
* @param string $field the field in this entity to check for errors
* @return array errors in nested entity if any
*/
protected function _nestedErrors($field)
{
$path = explode('.', $field);
// Only one path element, check for nested entity with error.
if (count($path) === 1) {
return $this->_readError($this->get($path[0]));
}
$entity = $this;
$len = count($path);
while ($len) {
$part = array_shift($path);
$len = count($path);
if ($entity instanceof EntityInterface) {
$val = $entity->get($part);
} elseif (is_array($entity)) {
$val = isset($entity[$part]) ? $entity[$part] : false;
}
if (is_array($val) ||
$val instanceof Traversable ||
$val instanceof EntityInterface
) {
$entity = $val;
} else {
$path[] = $part;
break;
}
}
if (count($path) <= 1) {
return $this->_readError($entity, array_pop($path));
}
return [];
}
/**
* Read the error(s) from one or many objects.
*
* @param array|\Cake\Datasource\EntityTrait $object The object to read errors from.
* @param string $path The field name for errors.
* @return array
*/
protected function _readError($object, $path = null)
{
if ($object instanceof EntityInterface) {
return $object->errors($path);
}
if (is_array($object)) {
$array = array_map(function ($val) {
if ($val instanceof EntityInterface) {
return $val->errors();
}
}, $object);
return array_filter($array);
}
return [];
}
/**
* Stores whether or not a property value can be changed or set in this entity.
* The special property `*` can also be marked as accessible or protected, meaning
* that any other property specified before will take its value. For example
* `$entity->accessible('*', true)` means that any property not specified already
* will be accessible by default.
*
* You can also call this method with an array of properties, in which case they
* will each take the accessibility value specified in the second argument.
*
* ### Example:
*
* ```
* $entity->accessible('id', true); // Mark id as not protected
* $entity->accessible('author_id', false); // Mark author_id as protected
* $entity->accessible(['id', 'user_id'], true); // Mark both properties as accessible
* $entity->accessible('*', false); // Mark all properties as protected
* ```
*
* When called without the second param it will return whether or not the property
* can be set.
*
* ### Example:
*
* ```
* $entity->accessible('id'); // Returns whether it can be set or not
* ```
*
* @param string|array $property single or list of properties to change its accessibility
* @param bool $set true marks the property as accessible, false will
* mark it as protected.
* @return $this|bool
*/
public function accessible($property, $set = null)
{
if ($set === null) {
$value = isset($this->_accessible[$property]) ?
$this->_accessible[$property] :
null;
return ($value === null && !empty($this->_accessible['*'])) || $value;
}
if ($property === '*') {
$this->_accessible = array_map(function ($p) use ($set) {
return (bool)$set;
}, $this->_accessible);
$this->_accessible['*'] = (bool)$set;
return $this;
}
foreach ((array)$property as $prop) {
$this->_accessible[$prop] = (bool)$set;
}
return $this;
}
/**
* Returns the alias of the repository from which this entity came from.
*
* If called with no arguments, it returns the alias of the repository
* this entity came from if it is known.
*
* @param string $alias the alias of the repository
* @return string
*/
public function source($alias = null)
{
if ($alias === null) {
return $this->_registryAlias;
}
$this->_registryAlias = $alias;
}
/**
* Returns a string representation of this object in a human readable format.
*
* @return string
*/
public function __toString()
{
return json_encode($this, JSON_PRETTY_PRINT);
}
/**
* Returns an array that can be used to describe the internal state of this
* object.
*
* @return array
*/
public function __debugInfo()
{
return [
'new' => $this->isNew(),
'accessible' => array_filter($this->_accessible),
'properties' => $this->_properties,
'dirty' => $this->_dirty,
'original' => $this->_original,
'virtual' => $this->_virtual,
'errors' => $this->_errors,
'repository' => $this->_registryAlias
];
}
}