1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devtoolboxuk\cerberus\Handlers; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
abstract class AbstractBase |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
private $wrappers = []; |
13
|
|
|
private $input; |
14
|
|
|
private $internalInput; |
15
|
|
|
private $output; |
16
|
|
|
|
17
|
|
|
private $handlerName; |
18
|
|
|
private $score; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
private $handlerReference; |
22
|
|
|
|
23
|
|
|
public function __construct($value) |
24
|
|
|
{ |
25
|
|
|
$this->setInput($value); |
26
|
|
|
$this->setInternalInput($value); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getInput() |
30
|
|
|
{ |
31
|
|
|
return $this->input; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function getInternalInput() |
38
|
|
|
{ |
39
|
|
|
return $this->internalInput; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getHandlerReference() |
43
|
|
|
{ |
44
|
|
|
return $this->handlerReference; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setHandlerReference($reference = null) |
48
|
|
|
{ |
49
|
|
|
$this->handlerReference = $this->getHandlerName(); |
50
|
|
|
if ($reference) { |
51
|
|
|
$this->handlerReference = $reference; |
52
|
|
|
} |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function setInput($value) |
57
|
|
|
{ |
58
|
|
|
$this->input = $value; |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setInternalInput($value) |
63
|
|
|
{ |
64
|
|
|
$this->internalInput = $value; |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getHandlerName() |
69
|
|
|
{ |
70
|
|
|
return $this->handlerName; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function setHandlerName($value) |
74
|
|
|
{ |
75
|
|
|
$this->handlerName = $value; |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getOutput() |
80
|
|
|
{ |
81
|
|
|
return $this->output; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function setOutput($value) |
85
|
|
|
{ |
86
|
|
|
$this->output = $value; |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getWrappers() |
91
|
|
|
{ |
92
|
|
|
return $this->wrappers; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getScore() |
96
|
|
|
{ |
97
|
|
|
return $this->score; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setScore($score) |
101
|
|
|
{ |
102
|
|
|
$this->score = $score; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function pushWrapper($wrapper) |
107
|
|
|
{ |
108
|
|
|
array_unshift($this->wrappers, $wrapper); |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
public function build($method, $arguments = []) |
114
|
|
|
{ |
115
|
|
|
$className = __NAMESPACE__ . '\\' . ucfirst($method) . 'Handler'; |
116
|
|
|
|
117
|
|
|
if (class_exists($className)) { |
118
|
|
|
|
119
|
|
|
$reflection = new ReflectionClass($className); |
120
|
|
|
|
121
|
|
|
if (!$reflection->isInstantiable()) { |
122
|
|
|
throw new Exception(sprintf('"%s" must be instantiable', $className)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $reflection->newInstanceArgs($arguments); |
126
|
|
|
} |
127
|
|
|
throw new Exception(sprintf('"%s" is not a valid handler', $method)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |