1 | <?php |
||
12 | trait DynamicObjectsTrait |
||
13 | { |
||
14 | use MemoizeTrait; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected static $dynamicMethods = array(); |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected static $dynamicProperties = array(); |
||
25 | |||
26 | /** |
||
27 | * Add a dynamic property. |
||
28 | * |
||
29 | * @param string $name |
||
30 | * The property name. |
||
31 | * @param mixed $value |
||
32 | * The property value. |
||
33 | * @param bool $memoize |
||
34 | * Memoize parameter. |
||
35 | */ |
||
36 | 5 | public static function addDynamicProperty($name, $value, $memoize = false) |
|
44 | |||
45 | /** |
||
46 | * Add a dynamic method. |
||
47 | * |
||
48 | * @param $name |
||
49 | * The method name. |
||
50 | * @param \Closure $func |
||
51 | * The method. |
||
52 | * @param bool $memoize |
||
53 | * Memoize parameter. |
||
54 | */ |
||
55 | 4 | public static function addDynamicMethod($name, \Closure $func, $memoize = false) |
|
63 | |||
64 | /** |
||
65 | * Check if a dynamic property exists. |
||
66 | * |
||
67 | * @param string $name |
||
68 | * The property name. |
||
69 | * @return bool |
||
70 | * True if the property exists, false otherwise. |
||
71 | */ |
||
72 | 8 | public static function hasDynamicProperty($name) |
|
76 | |||
77 | /** |
||
78 | * Check if a dynamic method exists. |
||
79 | * |
||
80 | * @param string $name |
||
81 | * The property name. |
||
82 | * @return bool |
||
83 | * True if the property exists, false otherwise. |
||
84 | */ |
||
85 | 6 | public static function hasDynamicMethod($name) |
|
89 | |||
90 | /** |
||
91 | * Get a dynamic property. |
||
92 | * |
||
93 | * @param $name |
||
94 | * The property name. |
||
95 | * @return mixed|null |
||
96 | * The property value if it exists, null otherwise. |
||
97 | */ |
||
98 | 4 | public static function getDynamicProperty($name) |
|
106 | |||
107 | /** |
||
108 | * Get a dynamic method. |
||
109 | * |
||
110 | * @param $name |
||
111 | * The method name. |
||
112 | * @return mixed|null |
||
113 | * The method if it exists, null otherwise. |
||
114 | */ |
||
115 | 2 | public static function getDynamicMethod($name) |
|
120 | |||
121 | /** |
||
122 | * Clear dynamic properties. |
||
123 | */ |
||
124 | 1 | public static function clearDynamicProperties() |
|
128 | |||
129 | /** |
||
130 | * Clear dynamic methods. |
||
131 | */ |
||
132 | 1 | public static function clearDynamicMethods() |
|
136 | |||
137 | /** |
||
138 | * Remove a dynamic property. |
||
139 | * |
||
140 | * @param string $name |
||
141 | * The property name. |
||
142 | */ |
||
143 | 1 | public static function removeDynamicProperty($name) |
|
147 | |||
148 | /** |
||
149 | * Remove a dynamic method. |
||
150 | * |
||
151 | * @param string $name |
||
152 | * The method name. |
||
153 | */ |
||
154 | 1 | public static function removeDynamicMethod($name) |
|
158 | |||
159 | /** |
||
160 | * Execute a closure. |
||
161 | * |
||
162 | * @param \Closure $func |
||
163 | * The closure. |
||
164 | * @param array $parameters |
||
165 | * The closure's parameters. |
||
166 | * @param bool $memoize |
||
167 | * The memoize parameter. |
||
168 | * |
||
169 | * @return mixed|null |
||
170 | * The return of the closure. |
||
171 | * |
||
172 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
173 | */ |
||
174 | 4 | public function doDynamicRequest(\Closure $func, array $parameters = [], $memoize = false) |
|
175 | { |
||
176 | 4 | if (!class_exists('\drupol\Memoize\Memoize') || false === $memoize) { |
|
177 | 4 | $memoize = false; |
|
178 | } |
||
179 | |||
180 | 4 | if (true === $memoize) { |
|
181 | 2 | return $this->memoize($func, $parameters); |
|
182 | } |
||
183 | |||
184 | 4 | return call_user_func_array($func, $parameters); |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * @param $method |
||
189 | * @param array $parameters |
||
190 | * |
||
191 | * @return mixed |
||
192 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
193 | */ |
||
194 | 3 | public function __call($method, array $parameters = array()) |
|
203 | |||
204 | /** |
||
205 | * {inheritdoc} |
||
206 | */ |
||
207 | 4 | public function __get($property) |
|
222 | |||
223 | /** |
||
224 | * {inheritdoc} |
||
225 | */ |
||
226 | 3 | public function __set($property, $value) |
|
232 | } |
||
233 |