1 | <?php |
||
21 | abstract class MagicResource |
||
22 | { |
||
23 | protected $properties = []; |
||
24 | |||
25 | protected $validator; |
||
26 | |||
27 | private static $methodWhiteList = [ |
||
28 | 'box', |
||
29 | 'allowedValues' |
||
30 | ]; |
||
31 | |||
32 | public function __call($functionName, $arguments) |
||
33 | { |
||
34 | $propertyName = strtolower($functionName); |
||
35 | |||
36 | if ($this->hasProperty($propertyName)) { |
||
37 | return $this->get($propertyName); |
||
38 | } |
||
39 | |||
40 | if (isset($this->defaults()[$propertyName])) { |
||
41 | return $this->defaults()[$propertyName]; |
||
42 | } |
||
43 | |||
44 | throw new \Sensorario\Resources\Exceptions\UndefinedMethodException( |
||
45 | 'Method `' . get_class($this) |
||
46 | . '::' . $functionName |
||
47 | . '()` is not yet implemented' |
||
48 | ); |
||
49 | } |
||
50 | |||
51 | abstract public function applyConfiguration(Configurator $configurator); |
||
52 | |||
53 | 18 | public function __construct( |
|
54 | array $properties, |
||
55 | ResourcesValidator $validator, |
||
56 | Configurator $configuration = null |
||
57 | ) { |
||
58 | 18 | $this->properties = $properties; |
|
59 | 18 | $this->validator = $validator; |
|
60 | 18 | $this->configuration = $configuration; |
|
|
|||
61 | |||
62 | 18 | if ($configuration) { |
|
63 | $this->applyConfiguration( |
||
64 | $configuration |
||
65 | ); |
||
66 | } |
||
67 | |||
68 | 18 | $this->init(); |
|
69 | 12 | } |
|
70 | |||
71 | 18 | private function init() |
|
72 | { |
||
73 | 18 | $this->ensurePropertiesConsistency(); |
|
74 | 17 | $this->validate(); |
|
75 | 12 | } |
|
76 | |||
77 | 18 | public static function __callStatic($methodName, array $args) |
|
78 | { |
||
79 | 18 | $isMethodAllowed = in_array( |
|
80 | 18 | $methodName, |
|
81 | 18 | self::$methodWhiteList |
|
82 | ); |
||
83 | |||
84 | 18 | $configuration = null; |
|
85 | |||
86 | if ( |
||
87 | 18 | isset($args[1]) |
|
88 | 1 | && 'Sensorario\Resources\Configurator' == get_class($args[1]) |
|
89 | ) { |
||
90 | $configuration = new Configurator( |
||
91 | $args[1]->resourceName(), |
||
92 | $args[1]->container() |
||
93 | ); |
||
94 | } |
||
95 | |||
96 | 18 | if ($isMethodAllowed) { |
|
97 | 17 | return new static( |
|
98 | 17 | $args[0] ?? [], |
|
99 | 17 | new ResourcesValidator(), |
|
100 | 17 | $configuration |
|
101 | ); |
||
102 | } |
||
103 | |||
104 | 1 | throw new \Sensorario\Resources\Exceptions\FactoryMethodException( |
|
105 | 'Invalid factory method ' |
||
106 | 1 | . '`' . $methodName . '`' |
|
107 | ); |
||
108 | } |
||
109 | |||
110 | 15 | final public function hasProperty($propertyName) |
|
116 | |||
117 | final public function set($propertyName, $propertValue) |
||
118 | { |
||
119 | $this->properties[$propertyName] = $propertValue; |
||
120 | } |
||
121 | |||
122 | 13 | final public function get($propertyName) |
|
140 | |||
141 | 14 | final public function hasNotProperty($propertyName) |
|
145 | |||
146 | 2 | final public function hasProperties(array $properties) |
|
156 | |||
157 | 17 | final public function properties() |
|
181 | |||
182 | 17 | public function validate() |
|
186 | |||
187 | 18 | public function ensurePropertiesConsistency() |
|
199 | |||
200 | 13 | public function ensurePropertyNameIsNotEmpty($propertyName) |
|
201 | { |
||
202 | 13 | if ('' == $propertyName) { |
|
203 | throw new \Sensorario\Resources\Exceptions\PropertyNameEmptyException( |
||
208 | } |
||
209 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: