1 | <?php |
||
10 | trait DynamicObjectsTrait |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | protected static $dynamicMethods = array(); |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | protected static $dynamicProperties = array(); |
||
21 | |||
22 | /** |
||
23 | * Add a dynamic property. |
||
24 | * |
||
25 | * @param string $name |
||
26 | * The property name. |
||
27 | * @param mixed $value |
||
28 | * The property value. |
||
29 | * |
||
30 | */ |
||
31 | 4 | public static function addDynamicProperty($name, $value) |
|
32 | { |
||
33 | 4 | if (is_callable($value)) { |
|
34 | 1 | $instance = new static(); |
|
35 | 1 | static::$dynamicProperties[get_called_class()][$name] = \Closure::bind($value, $instance, $instance); |
|
36 | } else { |
||
37 | 3 | static::$dynamicProperties[get_called_class()][$name] = $value; |
|
38 | } |
||
39 | 4 | } |
|
40 | |||
41 | /** |
||
42 | * Add a dynamic method. |
||
43 | * |
||
44 | * @param $name |
||
45 | * The method name. |
||
46 | * @param \Closure $func |
||
47 | * The method. |
||
48 | */ |
||
49 | 3 | public static function addDynamicMethod($name, \Closure $func) |
|
54 | |||
55 | /** |
||
56 | * Check if a dynamic property exists. |
||
57 | * |
||
58 | * @param string $name |
||
59 | * The property name. |
||
60 | * @return bool |
||
61 | * True if the property exists, false otherwise. |
||
62 | */ |
||
63 | 6 | public static function hasDynamicProperty($name) |
|
67 | |||
68 | /** |
||
69 | * Check if a dynamic method exists. |
||
70 | * |
||
71 | * @param string $name |
||
72 | * The property name. |
||
73 | * @return bool |
||
74 | * True if the property exists, false otherwise. |
||
75 | */ |
||
76 | 4 | public static function hasDynamicMethod($name) |
|
80 | |||
81 | /** |
||
82 | * Get a dynamic property. |
||
83 | * |
||
84 | * @param $name |
||
85 | * The property name. |
||
86 | * @return mixed|null |
||
87 | * The property value if it exists, null otherwise. |
||
88 | */ |
||
89 | 2 | public static function getDynamicProperty($name) |
|
97 | |||
98 | /** |
||
99 | * Get a dynamic method. |
||
100 | * |
||
101 | * @param $name |
||
102 | * The method name. |
||
103 | * @return mixed|null |
||
104 | * The method if it exists, null otherwise. |
||
105 | */ |
||
106 | 1 | public static function getDynamicMethod($name) |
|
111 | |||
112 | /** |
||
113 | * Clear dynamic properties. |
||
114 | */ |
||
115 | 1 | public static function clearDynamicProperties() |
|
119 | |||
120 | /** |
||
121 | * Clear dynamic methods. |
||
122 | */ |
||
123 | 1 | public static function clearDynamicMethods() |
|
127 | |||
128 | /** |
||
129 | * Remove a dynamic property. |
||
130 | * |
||
131 | * @param string $name |
||
132 | * The property name. |
||
133 | */ |
||
134 | 1 | public static function removeDynamicProperty($name) |
|
138 | |||
139 | /** |
||
140 | * Remove a dynamic method. |
||
141 | * |
||
142 | * @param string $name |
||
143 | * The method name. |
||
144 | */ |
||
145 | 1 | public static function removeDynamicMethod($name) |
|
149 | |||
150 | /** |
||
151 | * {inheritdoc} |
||
152 | */ |
||
153 | 1 | public function __call($method, $parameters) |
|
162 | |||
163 | /** |
||
164 | * {inheritdoc} |
||
165 | */ |
||
166 | 3 | public function __set($property, $value) |
|
167 | { |
||
168 | 3 | if (static::hasDynamicProperty($property)) { |
|
169 | 1 | static::addDynamicProperty($property, $value); |
|
170 | } |
||
171 | 3 | } |
|
172 | |||
173 | /** |
||
174 | * {inheritdoc} |
||
175 | */ |
||
176 | 2 | public function __get($property) |
|
182 | } |
||
183 |