1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace AmmitPhp\Ammit\UI\Resolver\Validator; |
5
|
|
|
|
6
|
|
|
use AmmitPhp\Ammit\UI\Resolver\Exception\CommandMappingException; |
7
|
|
|
use AmmitPhp\Ammit\UI\Resolver\Exception\UIValidationException; |
8
|
|
|
use AmmitPhp\Ammit\UI\Resolver\ValueExtractor; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
|
11
|
|
|
class RequestAttributeValueValidator implements UIValidatorInterface |
12
|
|
|
{ |
13
|
|
|
/** @var RawValueValidator */ |
14
|
|
|
protected $rawValueValidator; |
15
|
|
|
|
16
|
|
|
public function __construct(RawValueValidator $rawValueValidator) |
17
|
|
|
{ |
18
|
1 |
|
$this->rawValueValidator = $rawValueValidator; |
19
|
1 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Validate if request attribute $_POST field can be mapped to a Command |
23
|
|
|
* Throw CommandMappingException directly if any mapping issue |
24
|
|
|
* @param ServerRequestInterface $request |
25
|
|
|
* @param string $attributeKey |
26
|
|
|
* |
27
|
|
|
* @return mixed |
|
|
|
|
28
|
|
|
* @throws CommandMappingException If any mapping validation failed |
29
|
|
|
*/ |
30
|
|
|
public function extractValueFromRequestAttribute(ServerRequestInterface $request, string $attributeKey) |
31
|
|
|
{ |
32
|
1 |
|
$valueExtractor = new ValueExtractor(); |
33
|
|
|
|
34
|
|
|
try { |
35
|
1 |
|
$value = $valueExtractor->fromArray( |
36
|
1 |
|
$request->getParsedBody(), |
37
|
|
|
$attributeKey |
38
|
|
|
); |
39
|
|
|
|
40
|
1 |
|
if (is_string($value)) { |
41
|
1 |
|
$value = rawurldecode($value); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
return $value; |
45
|
1 |
|
} catch (InvalidArgumentException $exception) { |
46
|
1 |
|
throw CommandMappingException::fromAttribute( |
47
|
1 |
|
$exception->getMessage(), |
48
|
1 |
|
$exception->getPropertyPath() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Exceptions are caught in order to be processed later |
55
|
|
|
* |
56
|
|
|
* @throws CommandMappingException If any mapping validation failed |
57
|
|
|
*/ |
58
|
|
|
public function mustBeString(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): string |
59
|
|
|
{ |
60
|
1 |
|
$value = $this->extractValueFromRequestAttribute($request, $attributeKey); |
61
|
|
|
|
62
|
1 |
|
return $this->rawValueValidator->mustBeString( |
63
|
|
|
$value, |
64
|
|
|
$attributeKey, |
65
|
|
|
$this, |
66
|
|
|
$exceptionMessage |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Exceptions are caught in order to be processed later |
72
|
|
|
* |
73
|
|
|
* @throws CommandMappingException If any mapping validation failed |
74
|
|
|
* @return string|null |
75
|
|
|
*/ |
76
|
|
|
public function mustBeStringOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
77
|
|
|
{ |
78
|
1 |
|
$value = $this->extractValueFromRequestAttribute($request, $attributeKey); |
79
|
|
|
|
80
|
1 |
|
return $this->rawValueValidator->mustBeStringOrEmpty( |
81
|
|
|
$value, |
82
|
|
|
$attributeKey, |
83
|
|
|
$this, |
84
|
|
|
$exceptionMessage |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Exceptions are caught in order to be processed later |
90
|
|
|
* |
91
|
|
|
* @throws CommandMappingException If any mapping validation failed |
92
|
|
|
*/ |
93
|
|
|
public function mustBeBoolean(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): bool |
94
|
|
|
{ |
95
|
1 |
|
$value = $this->extractValueFromRequestAttribute($request, $attributeKey); |
96
|
|
|
|
97
|
1 |
|
return $this->rawValueValidator->mustBeBoolean( |
98
|
|
|
$value, |
99
|
|
|
$attributeKey, |
100
|
|
|
$this, |
101
|
|
|
$exceptionMessage |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Exceptions are caught in order to be processed later |
107
|
|
|
* |
108
|
|
|
* @throws CommandMappingException If any mapping validation failed |
109
|
|
|
* @return bool|null |
110
|
|
|
*/ |
111
|
|
|
public function mustBeBooleanOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
112
|
|
|
{ |
113
|
1 |
|
$value = $this->extractValueFromRequestAttribute($request, $attributeKey); |
114
|
|
|
|
115
|
1 |
|
return $this->rawValueValidator->mustBeBooleanOrEmpty( |
116
|
|
|
$value, |
117
|
|
|
$attributeKey, |
118
|
|
|
$this, |
119
|
|
|
$exceptionMessage |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Exceptions are caught in order to be processed later |
125
|
|
|
* |
126
|
|
|
* @throws CommandMappingException If any mapping validation failed |
127
|
|
|
*/ |
128
|
|
|
public function mustBeArray(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): array |
129
|
|
|
{ |
130
|
1 |
|
$value = $this->extractValueFromRequestAttribute($request, $attributeKey); |
131
|
|
|
|
132
|
1 |
|
return $this->rawValueValidator->mustBeArray( |
133
|
|
|
$value, |
134
|
|
|
$attributeKey, |
135
|
|
|
$this, |
136
|
|
|
$exceptionMessage |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Exceptions are caught in order to be processed later |
142
|
|
|
* |
143
|
|
|
* @throws CommandMappingException If any mapping validation failed |
144
|
|
|
*/ |
145
|
|
|
public function mustBeFloat(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): float |
146
|
|
|
{ |
147
|
1 |
|
$value = $this->extractValueFromRequestAttribute($request, $attributeKey); |
148
|
|
|
|
149
|
1 |
|
return $this->rawValueValidator->mustBeFloat( |
150
|
|
|
$value, |
151
|
|
|
$attributeKey, |
152
|
|
|
$this, |
153
|
|
|
$exceptionMessage |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Exceptions are caught in order to be processed later |
159
|
|
|
* |
160
|
|
|
* @throws CommandMappingException If any mapping validation failed |
161
|
|
|
*/ |
162
|
|
|
public function mustBeInteger(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): int |
163
|
|
|
{ |
164
|
1 |
|
$value = $this->extractValueFromRequestAttribute($request, $attributeKey); |
165
|
|
|
|
166
|
1 |
|
return $this->rawValueValidator->mustBeInteger( |
167
|
|
|
$value, |
168
|
|
|
$attributeKey, |
169
|
|
|
$this, |
170
|
|
|
$exceptionMessage |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Exceptions are caught in order to be processed later |
176
|
|
|
* |
177
|
|
|
* @throws CommandMappingException If any mapping validation failed |
178
|
|
|
* @return int|null |
179
|
|
|
*/ |
180
|
|
View Code Duplication |
public function mustBeIntegerOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
|
|
|
|
181
|
|
|
{ |
182
|
1 |
|
$value = $this->extractValueFromRequestAttribute($request, $attributeKey); |
183
|
|
|
|
184
|
1 |
|
return $this->rawValueValidator->mustBeIntegerOrEmpty( |
185
|
|
|
$value, |
186
|
|
|
$attributeKey, |
187
|
|
|
$this, |
188
|
|
|
$exceptionMessage |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Exceptions are caught in order to be processed later |
194
|
|
|
* |
195
|
|
|
* @throws CommandMappingException If any mapping validation failed |
196
|
|
|
*/ |
197
|
|
|
public function mustBeDate(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): \DateTime |
198
|
|
|
{ |
199
|
1 |
|
$value = $this->extractValueFromRequestAttribute($request, $attributeKey); |
200
|
|
|
|
201
|
1 |
|
return $this->rawValueValidator->mustBeDate( |
202
|
|
|
$value, |
203
|
|
|
$attributeKey, |
204
|
|
|
$this, |
205
|
|
|
$exceptionMessage |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Exceptions are caught in order to be processed later |
211
|
|
|
* |
212
|
|
|
* @throws CommandMappingException If any mapping validation failed |
213
|
|
|
*/ |
214
|
|
View Code Duplication |
public function mustBeDateTime(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): \DateTime |
|
|
|
|
215
|
|
|
{ |
216
|
1 |
|
$value = $this->extractValueFromRequestAttribute($request, $attributeKey); |
217
|
|
|
|
218
|
1 |
|
return $this->rawValueValidator->mustBeDateTime( |
219
|
|
|
$value, |
220
|
|
|
$attributeKey, |
221
|
|
|
$this, |
222
|
|
|
$exceptionMessage |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Exceptions are caught in order to be processed later |
228
|
|
|
* |
229
|
|
|
* @return \DateTime|null |
|
|
|
|
230
|
|
|
* @throws CommandMappingException If any mapping validation failed |
231
|
|
|
*/ |
232
|
|
|
public function mustBeDateTimeOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
233
|
|
|
{ |
234
|
1 |
|
$value = $this->extractValueFromRequestAttribute($request, $attributeKey); |
235
|
|
|
|
236
|
1 |
|
return $this->rawValueValidator->mustBeDateTimeOrEmpty( |
237
|
|
|
$value, |
238
|
|
|
$attributeKey, |
239
|
|
|
$this, |
240
|
|
|
$exceptionMessage |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @inheritdoc |
246
|
|
|
*/ |
247
|
|
|
public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException |
248
|
|
|
{ |
249
|
|
|
return UIValidationException::fromAttribute($message, $propertyPath); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.