1 | <?php |
||
22 | abstract class MagicResource |
||
23 | { |
||
24 | protected $properties = []; |
||
25 | |||
26 | private static $methodWhiteList = [ |
||
27 | 'box', |
||
28 | 'allowedValues' |
||
29 | ]; |
||
30 | |||
31 | 3 | public function __call($functionName, $arguments) |
|
49 | |||
50 | abstract public function applyConfiguration(Configurator $configurator); |
||
51 | |||
52 | 39 | public function __construct( |
|
53 | array $properties, |
||
54 | ResourcesValidator $validator, |
||
55 | Configurator $configuration = null |
||
56 | ) { |
||
57 | 39 | $this->properties = $properties; |
|
58 | |||
59 | 39 | if ($configuration) { |
|
60 | 21 | $this->applyConfiguration( |
|
61 | 21 | $configuration |
|
62 | ); |
||
63 | } |
||
64 | |||
65 | 39 | foreach ($properties as $k => $v) { |
|
66 | 31 | if ('object' === gettype($v) && !isset($this->rules()[$k])) { |
|
67 | 1 | throw new \Sensorario\Resources\Exceptions\PropertyWithoutRuleException( |
|
68 | 1 | 'When property `' . $k . '` is an object class, must be defined in Resources::rules()'. |
|
69 | 1 | ' but rules here are equals to ' . var_export($this->rules(), true) |
|
70 | 1 | . ' And properties are ' . var_export($this->properties, true) |
|
71 | ); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | 38 | $validator->validate($this); |
|
76 | 24 | } |
|
77 | |||
78 | 39 | public static function __callStatic($methodName, array $args) |
|
79 | { |
||
80 | 39 | $isMethodAllowed = in_array( |
|
81 | 39 | $methodName, |
|
82 | 39 | self::$methodWhiteList |
|
83 | ); |
||
84 | |||
85 | 39 | $configuration = null; |
|
86 | |||
87 | if ( |
||
88 | 39 | isset($args[1]) |
|
89 | 22 | && 'Sensorario\Resources\Configurator' == get_class($args[1]) |
|
90 | ) { |
||
91 | 21 | $configuration = new Configurator( |
|
92 | 21 | $args[1]->resourceName(), |
|
93 | 21 | $args[1]->container() |
|
94 | ); |
||
95 | } |
||
96 | |||
97 | 39 | if ($isMethodAllowed) { |
|
98 | 38 | return new static( |
|
99 | 38 | $args[0] ?? [], |
|
100 | 38 | new ResourcesValidator(), |
|
101 | 38 | $configuration |
|
102 | ); |
||
103 | } |
||
104 | |||
105 | 1 | throw new \Sensorario\Resources\Exceptions\FactoryMethodException( |
|
106 | 'Invalid factory method ' |
||
107 | 1 | . '`' . $methodName . '`' |
|
108 | ); |
||
109 | } |
||
110 | |||
111 | 31 | final public function hasProperty($propertyName) |
|
117 | |||
118 | 1 | final public function set($propertyName, $propertValue) |
|
122 | |||
123 | 24 | final public function get($propertyName) |
|
145 | |||
146 | 29 | final public function hasNotProperty($propertyName) |
|
150 | |||
151 | 2 | final public function hasProperties(array $properties) |
|
161 | |||
162 | 38 | final public function properties() |
|
163 | { |
||
186 | } |
||
187 |