1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devtoolboxuk\cerberus\Wrappers; |
4
|
|
|
|
5
|
|
|
use devtoolboxuk\soteria\SoteriaService; |
6
|
|
|
|
7
|
|
|
abstract class Base |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
protected $soteria; |
11
|
|
|
protected $score = 0; |
12
|
|
|
private $options = []; |
13
|
|
|
private $results = null; |
14
|
|
|
private $realScore = 0; |
15
|
|
|
private $params = []; |
16
|
|
|
|
17
|
|
|
private $name; |
18
|
|
|
private $active; |
19
|
|
|
private $reference; |
20
|
|
|
|
21
|
|
|
private $output = null; |
22
|
|
|
|
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->soteria = new SoteriaService(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getOutput() |
29
|
|
|
{ |
30
|
|
|
return $this->output; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function setOutput($value) |
34
|
|
|
{ |
35
|
|
|
$this->output = $value; |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setOptions($reference, $options = []) |
40
|
|
|
{ |
41
|
|
|
$this->reference = $reference; |
42
|
|
|
$this->options = $options; |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function inParams($name) |
47
|
|
|
{ |
48
|
|
|
if (in_array($name, $this->getParams())) { |
49
|
|
|
return true; |
50
|
|
|
} |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function getParams() |
55
|
|
|
{ |
56
|
|
|
return $this->params; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function setParams($params) |
60
|
|
|
{ |
61
|
|
|
$this->params = explode("|", $params); |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getRuleOption($name, $score) |
66
|
|
|
{ |
67
|
|
|
if (!$this->hasRuleOption($name)) { |
68
|
|
|
return $score; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->options[$this->name][$name]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function hasRuleOption($name) |
75
|
|
|
{ |
76
|
|
|
return isset($this->options[$this->name][$name]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getResult() |
80
|
|
|
{ |
81
|
|
|
return $this->results; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function overRideScore($data) |
85
|
|
|
{ |
86
|
|
|
$sanitise = $this->soteria->sanitise(); |
87
|
|
|
$this->score = $this->getRealScore(); |
88
|
|
|
|
89
|
|
|
if (isset($data[1])) { |
90
|
|
|
$sanitise->disinfect($data[1], 'int'); |
91
|
|
|
$this->score = (int)$sanitise->result()->getOutput(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getRealScore() |
96
|
|
|
{ |
97
|
|
|
if (!$this->hasRealScore()) { |
98
|
|
|
return 0; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->realScore; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function hasRealScore() |
105
|
|
|
{ |
106
|
|
|
return isset($this->realScore); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function pushResult($result) |
110
|
|
|
{ |
111
|
|
|
array_unshift($this->results, $result); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function sanitizeReference() |
115
|
|
|
{ |
116
|
|
|
return str_replace(" ", "", strip_tags(trim($this->getReference()))); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function getReference() |
120
|
|
|
{ |
121
|
|
|
$this->setOutput($this->reference); |
122
|
|
|
return $this->reference; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function setReference($reference) |
126
|
|
|
{ |
127
|
|
|
$this->reference = $reference; |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function initWrapper($name) |
132
|
|
|
{ |
133
|
|
|
$this->setName($name); |
134
|
|
|
$this->setRules(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function setName($name) |
138
|
|
|
{ |
139
|
|
|
$this->name = $name; |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function setRules() |
144
|
|
|
{ |
145
|
|
|
$this->options = $this->getOption($this->getName()); |
146
|
|
|
|
147
|
|
|
$this->setRealScore($this->getOption('score')); |
148
|
|
|
$this->setActive($this->getOption('active')); |
149
|
|
|
$this->setParams($this->getOption('params')); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getOption($name) |
153
|
|
|
{ |
154
|
|
|
if (!$this->hasOption($name)) { |
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->options[$name]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function hasOption($name) |
162
|
|
|
{ |
163
|
|
|
return isset($this->options[$name]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function getName() |
167
|
|
|
{ |
168
|
|
|
return $this->name; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
private function setRealScore($score) |
172
|
|
|
{ |
173
|
|
|
$this->realScore = $score; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
private function setActive($active) |
177
|
|
|
{ |
178
|
|
|
$this->active = $active; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
protected function setResult() |
182
|
|
|
{ |
183
|
|
|
if ($this->getActive()) { |
184
|
|
|
$this->results[$this->getName()] = $this->getScore(); |
185
|
|
|
$this->score = $this->getScore(); |
186
|
|
|
} else { |
187
|
|
|
$this->score = 0; |
188
|
|
|
} |
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
private function getActive() |
193
|
|
|
{ |
194
|
|
|
return $this->active; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function getScore() |
198
|
|
|
{ |
199
|
|
|
if (!$this->hasScore()) { |
200
|
|
|
return 0; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $this->score; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
protected function setScore($score) |
207
|
|
|
{ |
208
|
|
|
$this->score = $score; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
private function hasScore() |
212
|
|
|
{ |
213
|
|
|
return isset($this->score); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
} |