1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of sensorario/resources repository |
5
|
|
|
* |
6
|
|
|
* (c) Simone Gentili <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sensorario\Resources; |
13
|
|
|
|
14
|
|
|
use RuntimeException; |
15
|
|
|
use Sensorario\Resources\Validators\ResourcesValidator; |
16
|
|
|
|
17
|
|
|
abstract class MagicResource |
18
|
|
|
{ |
19
|
|
|
protected $properties = []; |
20
|
|
|
|
21
|
|
|
public function __call($functionName, $arguments) |
22
|
|
|
{ |
23
|
|
|
$propertyName = strtolower($functionName); |
24
|
|
|
|
25
|
|
|
if ($this->hasProperty($propertyName)) { |
26
|
|
|
return $this->get($propertyName); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (isset($this->defaults()[$propertyName])) { |
30
|
|
|
return $this->defaults()[$propertyName]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
throw new RuntimeException( |
34
|
|
|
'Method `' . get_class($this) |
35
|
|
|
. '::' . $functionName |
36
|
|
|
. '()` is not yet implemented' |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function __construct( |
41
|
|
|
array $properties, |
42
|
|
|
ResourcesValidator $validator |
43
|
|
|
) { |
44
|
|
|
$this->properties = $properties; |
45
|
|
|
|
46
|
|
|
foreach ($properties as $k => $v) { |
47
|
|
|
if ('object' === gettype($v) && !isset($this->rules()[$k])) { |
|
|
|
|
48
|
|
|
throw new RuntimeException( |
49
|
|
|
'When property `' . $k . '` is an object class, must be defined in Resources::rules()' |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
foreach ($properties as $name => $value) { |
55
|
|
|
if ('object' !== gettype($value)) { |
56
|
|
|
$properties[$name] = utf8_encode( |
57
|
|
|
$value |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$validator->validate($this); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function __callStatic($methodName, array $args) |
66
|
|
|
{ |
67
|
|
|
$methodWhiteList = [ |
68
|
|
|
'box', |
69
|
|
|
'allowedValues' |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$isMethodAllowed = in_array( |
73
|
|
|
$methodName, |
74
|
|
|
$methodWhiteList |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
if ($isMethodAllowed) { |
78
|
|
|
return new static( |
79
|
|
|
isset($args[0]) |
80
|
|
|
? $args[0] |
81
|
|
|
: [], |
82
|
|
|
new ResourcesValidator() |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
throw new RuntimeException( |
87
|
|
|
'Invalid factory method `' . $methodName . '`' |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
final public function hasProperty($propertyName) |
92
|
|
|
{ |
93
|
|
|
return isset( |
94
|
|
|
$this->properties[$propertyName] |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
final public function get($propertyName) |
99
|
|
|
{ |
100
|
|
|
if ('' == $propertyName) { |
101
|
|
|
throw new RuntimeException( |
102
|
|
|
'Oops! Property name requested is empty string!!' |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($this->hasNotProperty($propertyName)) { |
107
|
|
|
if (isset($this->defaults()[$propertyName])) { |
108
|
|
|
return $this->defaults()[$propertyName]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
throw new RuntimeException( |
112
|
|
|
'No value nor method `' |
113
|
|
|
. $propertyName |
114
|
|
|
. '` found in this resource' |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->properties[$propertyName]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public static function defaults() |
122
|
|
|
{ |
123
|
|
|
return []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
final public function hasNotProperty($propertyName) |
127
|
|
|
{ |
128
|
|
|
return !$this->hasProperty($propertyName); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
final public function hasProperties(array $properties) |
132
|
|
|
{ |
133
|
|
|
foreach ($properties as $property) { |
134
|
|
|
if ($this->hasNotProperty($property)) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return true; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
final public function properties() |
143
|
|
|
{ |
144
|
|
|
$properties = $this->properties; |
145
|
|
|
|
146
|
|
|
foreach ($properties as $k => $v) { |
147
|
|
|
if ('object' === gettype($v)) { |
148
|
|
|
if ($this->rules()[$k]['object'] === '\\Sensorario\\Resources\\Resource') { |
|
|
|
|
149
|
|
|
$properties[$k] = $v->properties(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $properties; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: