|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPAlgorithms\GraphTools\Traits; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class AllowedPropertiesTrait |
|
7
|
|
|
* @package PHPAlgorithms\GraphTools\Traits |
|
8
|
|
|
*/ |
|
9
|
|
|
trait AllowedPropertiesTrait |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string[] $allowedGet |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $allowedGet = array(); |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string[] $allowedSet |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $allowedSet = array(); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $name |
|
23
|
|
|
* @param array $arguments |
|
24
|
|
|
* @return mixed |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __call(string $name, array $arguments) |
|
27
|
|
|
{ |
|
28
|
|
|
$variable = lcfirst(substr($name, 3)); |
|
29
|
|
|
|
|
30
|
|
|
switch (substr($name, 0, 3)) { |
|
31
|
|
|
case 'get': |
|
32
|
|
|
return $this->__get($variable); |
|
33
|
|
|
case 'set': |
|
34
|
|
|
call_user_func_array([$this, '__set'], array_merge([$variable], $arguments)); |
|
35
|
|
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
return trigger_error('Call to undefined method '. static::class . "::{$name}()", E_USER_ERROR); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $name |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __get(string $name) |
|
47
|
|
|
{ |
|
48
|
|
|
if ($this->canGet($name)) { |
|
49
|
|
|
return $this->{$name}; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return trigger_error("Property {$name} doesn't exists and cannot be get.", E_USER_ERROR); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $name |
|
57
|
|
|
* @return boolean |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __isset(string $name) |
|
60
|
|
|
{ |
|
61
|
|
|
return property_exists(static::class, $name); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $name |
|
66
|
|
|
* @param mixed $value |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __set(string $name, $value) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->canSet($name)) { |
|
72
|
|
|
return $this->{$name} = $value; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return trigger_error("Property {$name} doesn't exists and cannot be set.", E_USER_ERROR); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $name |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function __unset(string $name) |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->canSet($name)) { |
|
85
|
|
|
unset($this->{$name}); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return trigger_error("Property {$name} doesn't exists and cannot be unset.", E_USER_ERROR); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* @return boolean |
|
94
|
|
|
*/ |
|
95
|
|
|
public function canGet(string $name) : bool |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->__isset($name) && in_array($name, $this->allowedGet, true); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $name |
|
102
|
|
|
* @return boolean |
|
103
|
|
|
*/ |
|
104
|
|
|
public function canSet(string $name) |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->__isset($name) && in_array($name, $this->allowedSet, true); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|