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 © 2020 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 © 2020 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\Ports; |
38
|
|
|
|
39
|
|
|
use Jkphl\Antibot\Domain\Contract\ValidationResultInterface; |
40
|
|
|
use Jkphl\Antibot\Domain\Exceptions\ErrorException; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Validation Result |
44
|
|
|
* |
45
|
|
|
* @package Jkphl\Antibot |
46
|
|
|
* @subpackage Jkphl\Antibot\Domain\Model |
47
|
|
|
*/ |
48
|
|
|
class ValidationResult implements ValidationResultInterface |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* Request was valid |
52
|
|
|
* |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
protected $valid = true; |
56
|
|
|
/** |
57
|
|
|
* Whitelisted |
58
|
|
|
* |
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
protected $whitelisted = false; |
62
|
|
|
/** |
63
|
|
|
* Named whitelists |
64
|
|
|
* |
65
|
|
|
* @var string[] |
66
|
|
|
*/ |
67
|
|
|
protected $whitelists = []; |
68
|
|
|
/** |
69
|
|
|
* Blacklisted |
70
|
|
|
* |
71
|
|
|
* @var bool |
72
|
|
|
*/ |
73
|
|
|
protected $blacklisted = false; |
74
|
|
|
/** |
75
|
|
|
* Named blacklists |
76
|
|
|
* |
77
|
|
|
* @var string[] |
78
|
|
|
*/ |
79
|
|
|
protected $blacklists = []; |
80
|
|
|
/** |
81
|
|
|
* Error messages |
82
|
|
|
* |
83
|
|
|
* @var ErrorException[] |
84
|
|
|
*/ |
85
|
|
|
protected $errors = []; |
86
|
|
|
/** |
87
|
|
|
* Skipping validators |
88
|
|
|
* |
89
|
|
|
* @var string[] |
90
|
|
|
*/ |
91
|
|
|
protected $skips = []; |
92
|
|
|
/** |
93
|
|
|
* Skipped |
94
|
|
|
* |
95
|
|
|
* @var bool |
96
|
|
|
*/ |
97
|
|
|
protected $skipped = false; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return whether the request was valid |
101
|
|
|
* |
102
|
|
|
* @return bool Valid |
103
|
|
|
*/ |
104
|
12 |
|
public function isValid(): bool |
105
|
|
|
{ |
106
|
12 |
|
return $this->valid && !$this->skipped; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return whether the request was invalid |
111
|
|
|
* |
112
|
|
|
* @return bool Valid |
113
|
|
|
*/ |
114
|
10 |
|
public function isFailed(): bool |
115
|
|
|
{ |
116
|
10 |
|
return !$this->valid && !$this->skipped; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set whether the request was valid in general |
121
|
|
|
* |
122
|
|
|
* @param bool $valid Valid |
123
|
|
|
*/ |
124
|
7 |
|
public function setValid(bool $valid): void |
125
|
|
|
{ |
126
|
7 |
|
$this->valid = $valid; |
127
|
7 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Return whether the request was whitelisted |
131
|
|
|
* |
132
|
|
|
* @return bool Whitelisted |
133
|
|
|
*/ |
134
|
3 |
|
public function isWhitelisted(): bool |
135
|
|
|
{ |
136
|
3 |
|
return $this->whitelisted; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Add a named whitelist |
141
|
|
|
* |
142
|
|
|
* @param string $whitelist Whitelist |
143
|
|
|
*/ |
144
|
3 |
|
public function addWhitelist(string $whitelist): void |
145
|
|
|
{ |
146
|
3 |
|
$this->whitelists[] = $whitelist; |
147
|
3 |
|
$this->whitelisted = true; |
148
|
3 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Return all whitelists |
152
|
|
|
* |
153
|
|
|
* @return string[] Whitelist names |
154
|
|
|
*/ |
155
|
2 |
|
public function getWhitelists(): array |
156
|
|
|
{ |
157
|
2 |
|
return $this->whitelists; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Return whether the request was blacklisted |
162
|
|
|
* |
163
|
|
|
* @return bool Blacklisted |
164
|
|
|
*/ |
165
|
2 |
|
public function isBlacklisted(): bool |
166
|
|
|
{ |
167
|
2 |
|
return $this->blacklisted; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Add a named blacklist |
172
|
|
|
* |
173
|
|
|
* @param string $blacklist Blacklist |
174
|
|
|
*/ |
175
|
2 |
|
public function addBlacklist(string $blacklist): void |
176
|
|
|
{ |
177
|
2 |
|
$this->blacklists[] = $blacklist; |
178
|
2 |
|
$this->blacklisted = true; |
179
|
2 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Return all blacklists |
183
|
|
|
* |
184
|
|
|
* @return string[] Blacklist names |
185
|
|
|
*/ |
186
|
2 |
|
public function getBlacklists(): array |
187
|
|
|
{ |
188
|
2 |
|
return $this->blacklists; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Add an error |
193
|
|
|
* |
194
|
|
|
* @param ErrorException $error |
195
|
|
|
*/ |
196
|
5 |
|
public function addError(ErrorException $error): void |
197
|
|
|
{ |
198
|
5 |
|
$this->errors[] = $error; |
199
|
5 |
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Return all errors |
203
|
|
|
* |
204
|
|
|
* @return ErrorException[] Errors |
205
|
|
|
*/ |
206
|
4 |
|
public function getErrors(): array |
207
|
|
|
{ |
208
|
4 |
|
return $this->errors; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Return whether this result has errors |
213
|
|
|
* |
214
|
|
|
* @return bool Has errors |
215
|
|
|
*/ |
216
|
6 |
|
public function hasErrors(): bool |
217
|
|
|
{ |
218
|
6 |
|
return count($this->errors) > 0; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Add a skipping validator |
223
|
|
|
* |
224
|
|
|
* @param string $skip Skipping validator |
225
|
|
|
*/ |
226
|
4 |
|
public function addSkip(string $skip): void |
227
|
|
|
{ |
228
|
4 |
|
$this->skips[] = $skip; |
229
|
4 |
|
$this->skipped = true; |
230
|
4 |
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Return whether this result has skipping validators |
234
|
|
|
* |
235
|
|
|
* @return bool Has skipping validators |
236
|
|
|
*/ |
237
|
2 |
|
public function hasSkips(): bool |
238
|
|
|
{ |
239
|
2 |
|
return count($this->skips) > 0; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Return whether a validator skipped this validation |
244
|
|
|
* |
245
|
|
|
* @return bool Validation skipped |
246
|
|
|
*/ |
247
|
4 |
|
public function isSkipped(): bool |
248
|
|
|
{ |
249
|
4 |
|
return $this->skipped; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|