1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devtoolboxuk\cerberus\Wrappers; |
4
|
|
|
|
5
|
|
|
use devtoolboxuk\soteria\SoteriaService; |
6
|
|
|
|
7
|
|
|
abstract class Wrapper |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
protected $soteria; |
11
|
|
|
private $options = []; |
12
|
|
|
private $results = null; |
13
|
|
|
private $score = 0; |
14
|
|
|
private $realScore = 0; |
15
|
|
|
private $params = []; |
16
|
|
|
private $name; |
17
|
|
|
private $active; |
18
|
|
|
private $reference; |
19
|
|
|
|
20
|
4 |
|
public function __construct() |
21
|
|
|
{ |
22
|
4 |
|
$this->soteria = new SoteriaService(); |
23
|
4 |
|
} |
24
|
|
|
|
25
|
2 |
|
public function getRealScore() |
26
|
|
|
{ |
27
|
2 |
|
if (!$this->hasRealScore()) { |
28
|
|
|
return 0; |
29
|
|
|
} |
30
|
|
|
|
31
|
2 |
|
return $this->realScore; |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
private function hasRealScore() |
35
|
|
|
{ |
36
|
2 |
|
return isset($this->realScore); |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
function setOptions($reference, $options = []) |
|
|
|
|
40
|
|
|
{ |
41
|
4 |
|
$this->reference = $reference; |
42
|
4 |
|
$this->options = $options; |
43
|
4 |
|
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
|
4 |
|
protected function setParams($params) |
60
|
|
|
{ |
61
|
4 |
|
$this->params = explode("|", $params); |
62
|
4 |
|
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
|
4 |
|
public function getResult() |
80
|
|
|
{ |
81
|
4 |
|
return $this->results; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function pushResult($result) |
85
|
|
|
{ |
86
|
|
|
array_unshift($this->results, $result); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function sanitizeReference() |
90
|
|
|
{ |
91
|
|
|
return str_replace(" ", "", strip_tags(trim($this->getReference()))); |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
protected function getReference() |
95
|
|
|
{ |
96
|
4 |
|
return $this->reference; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function setReference($reference) |
100
|
|
|
{ |
101
|
|
|
$this->reference = $reference; |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
protected function initWrapper($name) |
106
|
|
|
{ |
107
|
4 |
|
$this->setName($name); |
108
|
4 |
|
$this->setRules(); |
109
|
4 |
|
} |
110
|
|
|
|
111
|
4 |
|
protected function setName($name) |
112
|
|
|
{ |
113
|
4 |
|
$this->name = $name; |
114
|
4 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
4 |
|
private function setRules() |
118
|
|
|
{ |
119
|
4 |
|
$this->options = $this->getOption($this->getName()); |
120
|
|
|
|
121
|
4 |
|
$this->setRealScore($this->getOption('score')); |
122
|
4 |
|
$this->setActive($this->getOption('active')); |
123
|
4 |
|
$this->setParams($this->getOption('params')); |
124
|
4 |
|
} |
125
|
|
|
|
126
|
4 |
|
public function getOption($name) |
127
|
|
|
{ |
128
|
4 |
|
if (!$this->hasOption($name)) { |
129
|
2 |
|
return null; |
130
|
|
|
} |
131
|
|
|
|
132
|
4 |
|
return $this->options[$name]; |
133
|
|
|
} |
134
|
|
|
|
135
|
4 |
|
public function hasOption($name) |
136
|
|
|
{ |
137
|
4 |
|
return isset($this->options[$name]); |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
private function getName() |
141
|
|
|
{ |
142
|
4 |
|
return $this->name; |
143
|
|
|
} |
144
|
|
|
|
145
|
4 |
|
private function setRealScore($score) |
146
|
|
|
{ |
147
|
4 |
|
$this->realScore = $score; |
148
|
4 |
|
} |
149
|
|
|
|
150
|
4 |
|
private function setActive($active) |
151
|
|
|
{ |
152
|
4 |
|
$this->active = $active; |
153
|
4 |
|
} |
154
|
|
|
|
155
|
2 |
|
protected function setResult() |
156
|
|
|
{ |
157
|
2 |
|
if ($this->getActive()) { |
158
|
2 |
|
$this->results[$this->getName()] = $this->getScore(); |
159
|
2 |
|
$this->score = $this->getScore(); |
160
|
|
|
} else { |
|
|
|
|
161
|
|
|
$this->score = 0; |
162
|
|
|
} |
163
|
2 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
private function getActive() |
167
|
|
|
{ |
168
|
2 |
|
return $this->active; |
169
|
|
|
} |
170
|
|
|
|
171
|
4 |
|
public function getScore() |
172
|
|
|
{ |
173
|
4 |
|
if (!$this->hasScore()) { |
174
|
|
|
return 0; |
175
|
|
|
} |
176
|
|
|
|
177
|
4 |
|
return $this->score; |
178
|
|
|
} |
179
|
|
|
|
180
|
2 |
|
protected function setScore($score) |
181
|
|
|
{ |
182
|
2 |
|
$this->score = $score; |
183
|
2 |
|
} |
184
|
|
|
|
185
|
4 |
|
private function hasScore() |
186
|
|
|
{ |
187
|
4 |
|
return isset($this->score); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.