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