1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* antibot |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Antibot |
8
|
|
|
* @subpackage Jkphl\Antibot\Domain\Model |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2018 Joschi Kuphal <[email protected]> |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Antibot\Domain\Model; |
38
|
|
|
|
39
|
|
|
use Jkphl\Antibot\Domain\Exceptions\ErrorException; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Validation Result |
43
|
|
|
* |
44
|
|
|
* @package Jkphl\Antibot |
45
|
|
|
* @subpackage Jkphl\Antibot\Domain\Model |
46
|
|
|
*/ |
47
|
|
|
class ValidationResult |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* Request was valid |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $valid = true; |
55
|
|
|
/** |
56
|
|
|
* Whitelisted |
57
|
|
|
* |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected $whitelisted = false; |
61
|
|
|
/** |
62
|
|
|
* Named whitelists |
63
|
|
|
* |
64
|
|
|
* @var string[] |
65
|
|
|
*/ |
66
|
|
|
protected $whitelists = []; |
67
|
|
|
/** |
68
|
|
|
* Blacklisted |
69
|
|
|
* |
70
|
|
|
* @var bool |
71
|
|
|
*/ |
72
|
|
|
protected $blacklisted = false; |
73
|
|
|
/** |
74
|
|
|
* Named blacklists |
75
|
|
|
* |
76
|
|
|
* @var string[] |
77
|
|
|
*/ |
78
|
|
|
protected $blacklists = []; |
79
|
|
|
/** |
80
|
|
|
* Error messages |
81
|
|
|
* |
82
|
|
|
* @var ErrorException[] |
83
|
|
|
*/ |
84
|
|
|
protected $errors = []; |
85
|
|
|
/** |
86
|
|
|
* Skipping validators |
87
|
|
|
* |
88
|
|
|
* @var string[] |
89
|
|
|
*/ |
90
|
|
|
protected $skips = []; |
91
|
|
|
/** |
92
|
|
|
* Skipped |
93
|
|
|
* |
94
|
|
|
* @var bool |
95
|
|
|
*/ |
96
|
|
|
protected $skipped = false; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return whether the request was valid |
100
|
|
|
* |
101
|
|
|
* @return bool Valid |
102
|
|
|
*/ |
103
|
4 |
|
public function isValid(): bool |
104
|
|
|
{ |
105
|
4 |
|
return $this->valid && !$this->skipped; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return whether the request was invalid |
110
|
|
|
* |
111
|
|
|
* @return bool Valid |
112
|
|
|
*/ |
113
|
2 |
|
public function isFailed(): bool |
114
|
|
|
{ |
115
|
2 |
|
return !$this->valid && !$this->skipped; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set whether the request was valid in general |
120
|
|
|
* |
121
|
|
|
* @param bool $valid Valid |
122
|
|
|
*/ |
123
|
2 |
|
public function setValid(bool $valid): void |
124
|
|
|
{ |
125
|
2 |
|
$this->valid = $valid; |
126
|
2 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Return whether the request was whitelisted |
130
|
|
|
* |
131
|
|
|
* @return bool Whitelisted |
132
|
|
|
*/ |
133
|
1 |
|
public function isWhitelisted(): bool |
134
|
|
|
{ |
135
|
1 |
|
return $this->whitelisted; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Add a named whitelist |
140
|
|
|
* |
141
|
|
|
* @param string $whitelist Whitelist |
142
|
|
|
*/ |
143
|
1 |
|
public function addWhitelist(string $whitelist): void |
144
|
|
|
{ |
145
|
1 |
|
$this->whitelists[] = $whitelist; |
146
|
1 |
|
$this->whitelisted = true; |
147
|
1 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Return all whitelists |
151
|
|
|
* |
152
|
|
|
* @return string[] Whitelist names |
153
|
|
|
*/ |
154
|
|
|
public function getWhitelists(): array |
155
|
|
|
{ |
156
|
|
|
return $this->whitelists; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return whether the request was blacklisted |
161
|
|
|
* |
162
|
|
|
* @return bool Blacklisted |
163
|
|
|
*/ |
164
|
|
|
public function isBlacklisted(): bool |
165
|
|
|
{ |
166
|
|
|
return $this->blacklisted; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Add a named blacklist |
171
|
|
|
* |
172
|
|
|
* @param string $blacklist Blacklist |
173
|
|
|
*/ |
174
|
|
|
public function addBlacklist(string $blacklist): void |
175
|
|
|
{ |
176
|
|
|
$this->blacklists[] = $blacklist; |
177
|
|
|
$this->blacklisted = true; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Return all blacklists |
182
|
|
|
* |
183
|
|
|
* @return string[] Blacklist names |
184
|
|
|
*/ |
185
|
|
|
public function getBlacklists(): array |
186
|
|
|
{ |
187
|
|
|
return $this->blacklists; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Add an error |
192
|
|
|
* |
193
|
|
|
* @param ErrorException $error |
194
|
|
|
*/ |
195
|
2 |
|
public function addError(ErrorException $error): void |
196
|
|
|
{ |
197
|
2 |
|
$this->errors[] = $error; |
198
|
2 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Return all errors |
202
|
|
|
* |
203
|
|
|
* @return ErrorException[] Errors |
204
|
|
|
*/ |
205
|
2 |
|
public function getErrors(): array |
206
|
|
|
{ |
207
|
2 |
|
return $this->errors; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Return whether this result has errors |
212
|
|
|
* |
213
|
|
|
* @return bool Has errors |
214
|
|
|
*/ |
215
|
2 |
|
public function hasErrors(): bool |
216
|
|
|
{ |
217
|
2 |
|
return count($this->errors) > 0; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Add a skipping validator |
222
|
|
|
* |
223
|
|
|
* @param string $skip Skipping validator |
224
|
|
|
*/ |
225
|
2 |
|
public function addSkip(string $skip): void |
226
|
|
|
{ |
227
|
2 |
|
$this->skips[] = $skip; |
228
|
2 |
|
$this->skipped = true; |
229
|
2 |
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Return whether this result has skipping validators |
233
|
|
|
* |
234
|
|
|
* @return bool Has skipping validators |
235
|
|
|
*/ |
236
|
|
|
public function hasSkips(): bool |
237
|
|
|
{ |
238
|
|
|
return count($this->skips) > 0; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Return whether a validator skipped this validation |
243
|
|
|
* |
244
|
|
|
* @return bool Validation skipped |
245
|
|
|
*/ |
246
|
2 |
|
public function isSkipped(): bool |
247
|
|
|
{ |
248
|
2 |
|
return $this->skipped; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|