1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of the Aura project for PHP. |
5
|
|
|
* |
6
|
|
|
* @package Aura.Marshal |
7
|
|
|
* |
8
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
namespace Aura\Marshal\Entity; |
12
|
|
|
|
13
|
|
|
use Aura\Marshal\Lazy\LazyInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* Use this trait for magic __get(), __set(), __isset(), and __unset() on any |
18
|
|
|
* entity class that uses protected properties for its fields. |
19
|
|
|
* |
20
|
|
|
* @package Aura.Marshal |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
trait MagicPropertyTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* Gets a protected property as a field value; converts Lazy objects for |
28
|
|
|
* related entities and collections in place. |
29
|
|
|
* |
30
|
|
|
* Note that this will *not* be invoked by in-class uses of the protected |
31
|
|
|
* property; this means proxies will not be converted in those cases. |
32
|
|
|
* |
33
|
|
|
* @param string $field The field name to get. |
34
|
|
|
* |
35
|
|
|
* @return mixed The field value. |
36
|
|
|
* |
37
|
|
|
*/ |
38
|
|
|
public function __get($field) |
39
|
|
|
{ |
40
|
|
|
// get the property value |
41
|
|
|
$value = $this->$field; |
42
|
|
|
|
43
|
|
|
// is it a Lazy placeholder? |
44
|
|
|
if ($value instanceof LazyInterface) { |
45
|
|
|
// replace the Lazy placeholder with the real object |
46
|
|
|
$value = $value->get($this); |
47
|
|
|
$this->$field = $value; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// done! |
51
|
|
|
return $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* Sets a protected property. |
57
|
|
|
* |
58
|
|
|
* @param string $field The field name to get. |
59
|
|
|
* |
60
|
|
|
* @param mixed $value Set the field to this value. |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
* |
64
|
|
|
*/ |
65
|
|
|
public function __set($field, $value) |
66
|
|
|
{ |
67
|
|
|
$this->$field = $value; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
* Checks to see if a protected property is set. |
73
|
|
|
* |
74
|
|
|
* @param string $field The field name to check. |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
* |
78
|
|
|
*/ |
79
|
|
|
public function __isset($field) |
80
|
|
|
{ |
81
|
|
|
return isset($this->$field); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
* Unsets a protected property. |
87
|
|
|
* |
88
|
|
|
* @param string $field The field name to check. |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
* |
92
|
|
|
*/ |
93
|
|
|
public function __unset($field) |
94
|
|
|
{ |
95
|
|
|
unset($this->$field); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|