1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is a part of "Axessors" library. |
4
|
|
|
* |
5
|
|
|
* @author <[email protected]> |
6
|
|
|
* @license GPL |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace NoOne4rever\Axessors; |
10
|
|
|
|
11
|
|
|
use NoOne4rever\Axessors\Exceptions\{ |
12
|
|
|
AxessorsError, |
13
|
|
|
TypeError |
14
|
|
|
}; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class MethodRunner. |
18
|
|
|
* |
19
|
|
|
* Runs a method generated by Axessors. |
20
|
|
|
* |
21
|
|
|
* @package NoOne4rever\Axessors |
22
|
|
|
*/ |
23
|
|
|
class MethodRunner extends RunningSuit |
24
|
|
|
{ |
25
|
|
|
/** @var string method name */ |
26
|
|
|
private $method; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* MethodRunner constructor. |
30
|
|
|
* |
31
|
|
|
* @param int $mode running mode |
32
|
|
|
* @param PropertyData $data property data |
33
|
|
|
* @param string $class class |
34
|
|
|
* @param string $method method name |
35
|
|
|
* @param object|null $object object |
36
|
|
|
*/ |
37
|
91 |
|
public function __construct(int $mode, PropertyData $data, string $class, string $method, $object = null) |
38
|
|
|
{ |
39
|
91 |
|
parent::__construct($mode, $data, $class, $object); |
40
|
91 |
|
$this->method = $method; |
41
|
91 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Emulates execution of the method. |
45
|
|
|
* |
46
|
|
|
* @param array $args the arguments of the method called |
47
|
|
|
* @param string $file filename |
48
|
|
|
* @param int $line line number |
49
|
|
|
* |
50
|
|
|
* @return mixed return value of the method called |
51
|
|
|
* @throws AxessorsError if conditions for executing an accessor did not pass |
52
|
|
|
* @throws AxessorsError if Axessors method not found |
53
|
|
|
*/ |
54
|
91 |
|
public function run(array $args, string $file, int $line) |
55
|
|
|
{ |
56
|
91 |
|
$prefix = substr($this->method, 0, 3); |
57
|
91 |
|
if ($prefix == 'get') { |
58
|
18 |
|
$this->mode = RunningSuit::OUTPUT_MODE; |
59
|
18 |
|
$this->propertyData->reflection->setAccessible(true); |
60
|
18 |
|
$value = is_null($this->object) ? $this->propertyData->reflection->getValue() : $this->propertyData->reflection->getValue($this->object); |
61
|
18 |
|
$this->propertyData->reflection->setAccessible(false); |
62
|
18 |
|
return $this->executeAccessor(RunningSuit::OUTPUT_MODE, $value); |
63
|
73 |
|
} elseif ($prefix == 'set') { |
64
|
71 |
|
$this->mode = RunningSuit::INPUT_MODE; |
65
|
71 |
|
if (!isset($args[0])) { |
66
|
1 |
|
throw new AxessorsError("setter could not be called without arguments at $file:$line"); |
67
|
|
|
} |
68
|
70 |
|
$value = $this->executeAccessor(RunningSuit::INPUT_MODE, $args[0]); |
69
|
45 |
|
$this->propertyData->reflection->setAccessible(true); |
70
|
45 |
|
is_null($this->object) ? $this->propertyData->reflection->setValue($value) : $this->propertyData->reflection->setValue($this->object, |
71
|
45 |
|
$value); |
72
|
45 |
|
$this->propertyData->reflection->setAccessible(false); |
73
|
45 |
|
return; |
74
|
|
|
} else { |
75
|
2 |
|
if ($this->propertyData->getAlias() === '') { |
76
|
|
|
$this->method .= 'PROPERTY'; |
77
|
|
|
} else { |
78
|
2 |
|
$this->method = str_replace(ucfirst($this->propertyData->getAlias()), 'PROPERTY', $this->method); |
79
|
|
|
} |
80
|
2 |
|
return $this->runAxessorsMethod($args); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Searches and runs Axessors method. |
86
|
|
|
* |
87
|
|
|
* @param array $args method parameters |
88
|
|
|
* @return mixed result of function |
89
|
|
|
* @throws AxessorsError if requested method not found |
90
|
|
|
*/ |
91
|
2 |
|
private function runAxessorsMethod(array $args) |
92
|
|
|
{ |
93
|
2 |
|
$this->propertyData->reflection->setAccessible(true); |
94
|
2 |
|
$value = $this->propertyData->reflection->getValue($this->object); |
95
|
2 |
|
$this->checkType($this->propertyData->getTypeTree(), $value); |
96
|
2 |
|
foreach ($this->propertyData->getTypeTree() as $type => $subType) { |
97
|
2 |
|
$type = is_int($type) ? $subType : $type; |
98
|
2 |
|
$reflection = new \ReflectionClass($type); |
99
|
2 |
|
foreach ($reflection->getMethods() as $method) { |
100
|
2 |
|
if ($this->isCalledAxessorsMethod($method)) { |
101
|
2 |
|
return $this->executeAxessorsMethod($type, $method->name, $value, $args); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
throw new AxessorsError("method {$this->class}::{$this->method}() not found"); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Checks if the method given is called method. |
110
|
|
|
* |
111
|
|
|
* @param \ReflectionMethod $method method |
112
|
|
|
* @return bool the result of the checkout |
113
|
|
|
*/ |
114
|
2 |
|
private function isCalledAxessorsMethod(\ReflectionMethod $method): bool |
115
|
|
|
{ |
116
|
2 |
|
$isAccessible = $method->isStatic() && $method->isPublic() && !$method->isAbstract(); |
117
|
2 |
|
$isCalled = $method->name === "m_in_$this->method" || $method->name === "m_out_$this->method"; |
118
|
2 |
|
return $isAccessible && $isCalled; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Executes Axessors method. |
123
|
|
|
* |
124
|
|
|
* @param string $type type name |
125
|
|
|
* @param string $name method name |
126
|
|
|
* @param mixed $value value to read/write |
127
|
|
|
* @param array $args arguments |
128
|
|
|
* @return mixed function result |
129
|
|
|
*/ |
130
|
2 |
|
private function executeAxessorsMethod(string $type, string $name, $value, array $args) |
131
|
|
|
{ |
132
|
2 |
|
if ($name == "m_in_$this->method") { |
133
|
|
|
// add support for static properties |
134
|
2 |
|
$this->propertyData->reflection->setValue($this->object, |
135
|
2 |
|
call_user_func([$type, "m_in_$this->method"], $value, $args)); |
136
|
2 |
|
$this->propertyData->reflection->setAccessible(false); |
137
|
2 |
|
return; |
138
|
|
|
} elseif ($name == "m_out_$this->method") { |
139
|
|
|
$result = call_user_func([$type, "m_out_$this->method"], $value, $args); |
140
|
|
|
$this->propertyData->reflection->setAccessible(false); |
141
|
|
|
return $result; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Executes complex accessor. |
147
|
|
|
* |
148
|
|
|
* @param int $mode running mode |
149
|
|
|
* @param mixed $value field or argument value |
150
|
|
|
* @return mixed new value |
151
|
|
|
* @throws AxessorsError if conditions for accessor did not pass |
152
|
|
|
*/ |
153
|
88 |
|
private function executeAccessor(int $mode, $value) |
154
|
|
|
{ |
155
|
88 |
|
$this->checkType($this->propertyData->getTypeTree(), $value); |
156
|
82 |
|
$conditionsSuit = new ConditionsRunner($mode, $this->propertyData, $this->class, $this->method, $this->object); |
157
|
82 |
|
if ($conditionsSuit->processConditions($value)) { |
158
|
62 |
|
$handlersSuit = new HandlersRunner($mode, $this->propertyData, $this->class, $this->object); |
159
|
62 |
|
$value = $handlersSuit->executeHandlers($value); |
160
|
58 |
|
return $value; |
161
|
|
|
} else { |
162
|
18 |
|
throw new AxessorsError("conditions for {$this->class}::{$this->method}() did not pass"); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Checks if the variable belongs to defined type. |
168
|
|
|
* |
169
|
|
|
* @param mixed $var variable |
170
|
|
|
* @param string $type type |
171
|
|
|
* @return bool the result of the checkout |
172
|
|
|
*/ |
173
|
89 |
|
private function is($var, string $type): bool |
174
|
|
|
{ |
175
|
89 |
|
$isExactClass = $var instanceof $type; |
176
|
89 |
|
$isAxessorsType = ((new \ReflectionClass($type))->hasMethod('is') && call_user_func([$type, 'is'], $var)); |
177
|
89 |
|
return $isExactClass || $isAxessorsType; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Checks if the type of new property's value is correct. |
182
|
|
|
* |
183
|
|
|
* @param array $typeTree all possible types |
184
|
|
|
* @param $var mixed new value of the property |
185
|
|
|
* @throws TypeError if the type of new property's value does not match the type defined in Axessors comment |
186
|
|
|
*/ |
187
|
90 |
|
private function checkType(array $typeTree, $var): void |
188
|
|
|
{ |
189
|
90 |
|
if ($this->mode == RunningSuit::OUTPUT_MODE && is_null($var)) { |
190
|
1 |
|
return; |
191
|
|
|
} |
192
|
89 |
|
foreach ($typeTree as $type => $subType) { |
193
|
89 |
|
if (is_int($type) && $this->is($var, $subType)) { |
194
|
83 |
|
return; |
195
|
10 |
|
} elseif (is_iterable($var) && $this->is($var, $type)) { |
196
|
4 |
|
foreach ($var as $subVar) { |
197
|
4 |
|
$this->checkType($subType, $subVar); |
198
|
|
|
} |
199
|
10 |
|
return; |
200
|
|
|
} |
201
|
|
|
} |
202
|
6 |
|
throw new TypeError("not a valid type of {$this->class}::\${$this->propertyData->getName()}"); |
203
|
|
|
} |
204
|
|
|
} |