Completed
Push — master ( b28387...74032c )
by Pol
01:37
created

DynamicObjectsTrait::createDynamicClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 0
cp 0
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace drupol\DynamicObjects;
4
5
use drupol\Memoize\MemoizeTrait;
6
7
/**
8
 * Trait DynamicObjectsTrait.
9
 *
10
 * @package drupol\DynamicObjects
11
 */
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
     * Create an anonymous class.
28
     *
29
     * @return DynamicObject
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use anonymous//src/DynamicObjectsTrait.php$0.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
30
     */
31
    public static function createDynamicClass()
32
    {
33
        return new class extends DynamicObject {
34
        };
35
    }
36
37
    /**
38
     * Add a dynamic property.
39
     *
40
     * @param string $name
41
     *   The property name.
42
     * @param mixed $value
43
     *   The property value.
44
     * @param bool $memoize
45
     *   Memoize parameter.
46
     */
47 5
    public static function addDynamicProperty($name, $value, $memoize = false)
48
    {
49 5
        static::$dynamicProperties[get_called_class()][$name] = [
50 5
          'name' => $name,
51 5
          'factory' => $value,
52 5
          'memoize' => $memoize,
53
        ];
54 5
    }
55
56
    /**
57
     * Add a dynamic method.
58
     *
59
     * @param $name
60
     *   The method name.
61
     * @param \Closure $func
62
     *   The method.
63
     * @param bool $memoize
64
     *   Memoize parameter.
65
     */
66 4
    public static function addDynamicMethod($name, \Closure $func, $memoize = false)
67
    {
68 4
        static::$dynamicMethods[get_called_class()][$name] = [
69 4
          'name' => $name,
70 4
          'factory' => $func,
71 4
          'memoize' => $memoize,
72
        ];
73 4
    }
74
75
    /**
76
     * Check if a dynamic property exists.
77
     *
78
     * @param string $name
79
     *   The property name.
80
     * @return bool
81
     *   True if the property exists, false otherwise.
82
     */
83 8
    public static function hasDynamicProperty($name)
84
    {
85 8
        return isset(static::$dynamicProperties[get_called_class()][$name]);
86
    }
87
88
    /**
89
     * Check if a dynamic method exists.
90
     *
91
     * @param string $name
92
     *   The property name.
93
     * @return bool
94
     *   True if the property exists, false otherwise.
95
     */
96 6
    public static function hasDynamicMethod($name)
97
    {
98 6
        return isset(static::$dynamicMethods[get_called_class()][$name]);
99
    }
100
101
    /**
102
     * Get a dynamic property.
103
     *
104
     * @param $name
105
     *   The property name.
106
     * @return mixed|null
107
     *   The property value if it exists, null otherwise.
108
     */
109 4
    public static function getDynamicProperty($name)
110
    {
111 4
        if (static::hasDynamicProperty($name)) {
112 3
            return static::$dynamicProperties[get_called_class()][$name];
113
        }
114
115 1
        return null;
116
    }
117
118
    /**
119
     * Get a dynamic method.
120
     *
121
     * @param $name
122
     *   The method name.
123
     * @return mixed|null
124
     *   The method if it exists, null otherwise.
125
     */
126 2
    public static function getDynamicMethod($name)
127
    {
128 2
        return ( static::hasDynamicMethod($name) ) ?
129 2
          static::$dynamicMethods[get_called_class()][$name] : null;
130
    }
131
132
    /**
133
     * Clear dynamic properties.
134
     */
135 1
    public static function clearDynamicProperties()
136
    {
137 1
        static::$dynamicProperties[get_called_class()] = array();
138 1
    }
139
140
    /**
141
     * Clear dynamic methods.
142
     */
143 1
    public static function clearDynamicMethods()
144
    {
145 1
        static::$dynamicMethods[get_called_class()] = array();
146 1
    }
147
148
    /**
149
     * Remove a dynamic property.
150
     *
151
     * @param string $name
152
     *   The property name.
153
     */
154 1
    public static function removeDynamicProperty($name)
155
    {
156 1
        unset(static::$dynamicProperties[get_called_class()][$name]);
157 1
    }
158
159
    /**
160
     * Remove a dynamic method.
161
     *
162
     * @param string $name
163
     *   The method name.
164
     */
165 1
    public static function removeDynamicMethod($name)
166
    {
167 1
        unset(static::$dynamicMethods[get_called_class()][$name]);
168 1
    }
169
170
    /**
171
     * Execute a closure.
172
     *
173
     * @param \Closure $func
174
     *   The closure.
175
     * @param array $parameters
176
     *   The closure's parameters.
177
     * @param bool $memoize
178
     *   The memoize parameter.
179
     *
180
     * @return mixed|null
181
     *   The return of the closure.
182
     *
183
     * @throws \Psr\SimpleCache\InvalidArgumentException
184
     */
185 4
    public function doDynamicRequest(\Closure $func, array $parameters = [], $memoize = false)
186
    {
187 4
        if (!class_exists('\drupol\Memoize\Memoize') || false === $memoize) {
188 4
            $memoize = false;
189
        }
190
191 4
        if (true === $memoize) {
192 2
            return $this->memoize($func, $parameters);
193
        }
194
195 4
        return call_user_func_array($func, $parameters);
196
    }
197
198
    /**
199
     * @param $method
200
     * @param array $parameters
201
     *
202
     * @return mixed
203
     * @throws \Psr\SimpleCache\InvalidArgumentException
204
     */
205 3
    public function __call($method, array $parameters = array())
206
    {
207 3
        if (static::hasDynamicMethod($method)) {
208 2
            $data = static::getDynamicMethod($method);
209 2
            return $this->doDynamicRequest($data['factory'], $parameters, $data['memoize']);
210
        }
211
212 1
        throw new \BadMethodCallException(sprintf('Undefined method: %s().', $method));
213
    }
214
215
    /**
216
     * {inheritdoc}
217
     */
218 4
    public function __get($property)
219
    {
220 4
        if (static::hasDynamicProperty($property)) {
221 3
            $data = static::getDynamicProperty($property);
222
223 3
            $return = $data['factory'];
224 3
            if (is_callable($data['factory'])) {
225 2
                $return = $this->doDynamicRequest($data['factory'], [], $data['memoize']);
226
            }
227
228 3
            return $return;
229
        }
230
231 1
        throw new \DomainException(sprintf('Undefined property: %s().', $property));
232
    }
233
234
    /**
235
     * {inheritdoc}
236
     */
237 3
    public function __set($property, $value)
238
    {
239 3
        if (static::hasDynamicProperty($property)) {
240 1
            static::addDynamicProperty($property, $value);
241
        }
242 3
    }
243
}
244