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