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\TypeError; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class HandlersSuit. |
15
|
|
|
* |
16
|
|
|
* Processes Axessors handlers. |
17
|
|
|
* |
18
|
|
|
* @package NoOne4rever\Axessors |
19
|
|
|
*/ |
20
|
|
|
class HandlersRunner extends RunningSuit |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Executes handlers defined in the Axessors comment. |
24
|
|
|
* |
25
|
|
|
* @param $value mixed value of the property |
26
|
|
|
* @return mixed new value of the property |
27
|
|
|
*/ |
28
|
62 |
|
public function executeHandlers($value) |
29
|
|
|
{ |
30
|
62 |
|
$handlers = $this->mode == RunningSuit::OUTPUT_MODE ? $this->propertyData->getOutputHandlers() : $this->propertyData->getInputHandlers(); |
31
|
62 |
|
foreach ($handlers as $handler) { |
32
|
14 |
|
if (strpos($handler, '`') !== false) { |
33
|
9 |
|
$value = $this->executeInjectedString($handler, $value, false); |
34
|
|
|
} else { |
35
|
12 |
|
$value = $this->runStandardHandler($handler, $value); |
36
|
|
|
} |
37
|
|
|
} |
38
|
58 |
|
return $value; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Runs Axessors handler. |
43
|
|
|
* |
44
|
|
|
* @param string $handler handler name |
45
|
|
|
* @param mixed $value the value to process |
46
|
|
|
* @return mixed the result of handler execution |
47
|
|
|
* @throws TypeError if the called handler not found |
48
|
|
|
*/ |
49
|
6 |
|
private function runStandardHandler(string $handler, $value) |
50
|
|
|
{ |
51
|
6 |
|
foreach ($this->propertyData->getTypeTree() as $type => $subType) { |
52
|
6 |
|
$reflection = new \ReflectionClass('\Axessors\Types\\' . is_int($type) ? $subType : $type); |
53
|
6 |
|
foreach ($reflection->getMethods() as $method) { |
54
|
6 |
|
if ($this->isCalledHandler($method, $handler, $reflection->name, $value)) { |
55
|
6 |
|
return call_user_func([$reflection->name, $method->name], $value, false); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
2 |
|
throw new TypeError("property {$this->class}::\${$this->propertyData->getName()} does not have handler \"$handler\""); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Checks if the method can be called. |
64
|
|
|
* |
65
|
|
|
* @param \ReflectionMethod $method method reflection |
66
|
|
|
* @return bool the result of the checkout |
67
|
|
|
*/ |
68
|
6 |
|
private function isMethodAccessible(\ReflectionMethod $method): bool |
69
|
|
|
{ |
70
|
6 |
|
return $method->isPublic() && $method->isStatic() && !$method->isAbstract(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Checks if the value match handler's type. |
75
|
|
|
* |
76
|
|
|
* @param string $type type |
77
|
|
|
* @param mixed $value value |
78
|
|
|
* @return bool the result of the checkout |
79
|
|
|
*/ |
80
|
6 |
|
private function valueMatchType(string $type, $value): bool |
81
|
|
|
{ |
82
|
6 |
|
return call_user_func([$type, 'is'], $value); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Checks if the handler can be called. |
87
|
|
|
* |
88
|
|
|
* @param \ReflectionMethod $method method reflection |
89
|
|
|
* @param string $handler handler name |
90
|
|
|
* @param string $type type |
91
|
|
|
* @param mixed $value value |
92
|
|
|
* @return bool the result of the checkout |
93
|
|
|
*/ |
94
|
6 |
|
private function isCalledHandler(\ReflectionMethod $method, string $handler, string $type, $value): bool |
95
|
|
|
{ |
96
|
6 |
|
return $this->isMethodAccessible($method) && $this->valueMatchType($type, $value) && "h_$handler" === $method->name; |
97
|
|
|
} |
98
|
|
|
} |