|
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\OopError; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class HandlersSuit. |
|
15
|
|
|
* |
|
16
|
|
|
* Processes Axessors handlers. |
|
17
|
|
|
* |
|
18
|
|
|
* @package NoOne4rever\Axessors |
|
19
|
|
|
*/ |
|
20
|
|
|
class HandlersSuit 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
|
|
|
* @throws OopError if the property does not have one of the handlers defined in the Axessors comment |
|
28
|
|
|
*/ |
|
29
|
|
|
public function executeHandlers($value) |
|
30
|
|
|
{ |
|
31
|
|
|
$handlers = $this->mode == RunningSuit::OUTPUT_MODE ? $this->propertyData->getOutputHandlers() : $this->propertyData->getInputHandlers(); |
|
32
|
|
|
foreach ($handlers as $handler) { |
|
33
|
|
|
if (strpos($handler, '`') !== false) { |
|
34
|
|
|
$value = $this->runInjectedHandler($handler, $value); |
|
35
|
|
|
} else { |
|
36
|
|
|
$value = $this->runStandardHandler($handler, $value); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
return $value; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function runInjectedHandler(string $handler, $value) |
|
43
|
|
|
{ |
|
44
|
|
|
$handler = str_replace('\\`', '`', substr($handler, 1, strlen($handler) - 2)); |
|
45
|
|
View Code Duplication |
if (is_null($this->object)) { |
|
|
|
|
|
|
46
|
|
|
return call_user_func("{$this->class}::__axessorsExecute", $handler, $value, false); |
|
47
|
|
|
} else { |
|
48
|
|
|
return $this->object->__axessorsExecute($handler, $value, false); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function runStandardHandler(string $handler, $value) |
|
53
|
|
|
{ |
|
54
|
|
|
foreach ($this->propertyData->getTypeTree() as $type => $subType) { |
|
55
|
|
|
$reflection = new \ReflectionClass('\Axessors\Types\\' . is_int($type) ? $subType : $type); |
|
56
|
|
|
foreach ($reflection->getMethods() as $method) { |
|
57
|
|
|
$isAccessible = $method->isPublic() && $method->isStatic() && !$method->isAbstract(); |
|
58
|
|
|
$isThat = call_user_func([$reflection->name, 'is'], $value); |
|
59
|
|
|
if ($isAccessible && $isThat && "h_$handler" == $method->name) { |
|
60
|
|
|
return call_user_func([$reflection->name, $method->name], $value, false); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
throw new OopError("property {$this->class}::\${$this->propertyData->getName()} does not have handler \"$handler\""); |
|
65
|
|
|
} |
|
66
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.