|
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
|
|
|
|
If you implement
__calland 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
__callis implemented by a parent class and only the child class knows which methods exist: