|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Imedia\Ammit\UI\Resolver\Validator; |
|
5
|
|
|
|
|
6
|
|
|
use Imedia\Ammit\UI\Resolver\Exception\CommandMappingException; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @internal Contains Domain Validation assertions (but class won't be removed in next version) |
|
11
|
|
|
* Domain Validation should be done in Domain |
|
12
|
|
|
* Should be used for prototyping project knowing you are accumulating technical debt |
|
13
|
|
|
* |
|
14
|
|
|
* @author Guillaume MOREL <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
View Code Duplication |
class PragmaticRequestQueryStringValueValidator extends RequestQueryStringValueValidator |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/** @var RawValueValidator */ |
|
19
|
|
|
protected $rawValueValidator; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(PragmaticRawValueValidator $rawValueValidator) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->rawValueValidator = $rawValueValidator; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Domain should be responsible for id format |
|
28
|
|
|
* Exceptions are caught in order to be processed later |
|
29
|
|
|
* |
|
30
|
|
|
* @throws CommandMappingException If any mapping validation failed |
|
31
|
|
|
*/ |
|
32
|
|
|
public function mustBeUuid(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): string |
|
33
|
|
|
{ |
|
34
|
|
|
$value = $this->extractValueFromRequestQueryString($request, $queryStringKey); |
|
35
|
|
|
|
|
36
|
|
|
return $this->rawValueValidator->mustBeUuid( |
|
|
|
|
|
|
37
|
|
|
$value, |
|
38
|
|
|
$queryStringKey, |
|
39
|
|
|
$this, |
|
40
|
|
|
$exceptionMessage |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Domain should be responsible for legit values |
|
46
|
|
|
* Exceptions are caught in order to be processed later |
|
47
|
|
|
* |
|
48
|
|
|
* @throws CommandMappingException If any mapping validation failed |
|
49
|
|
|
* @return mixed Untouched value |
|
50
|
|
|
*/ |
|
51
|
|
|
public function mustBeInArray(ServerRequestInterface $request, array $availableValues, string $queryStringKey, string $exceptionMessage = null) |
|
52
|
|
|
{ |
|
53
|
|
|
$value = $this->extractValueFromRequestQueryString($request, $queryStringKey); |
|
54
|
|
|
|
|
55
|
|
|
return $this->rawValueValidator->mustBeInArray( |
|
|
|
|
|
|
56
|
|
|
$value, |
|
57
|
|
|
$availableValues, |
|
58
|
|
|
$queryStringKey, |
|
59
|
|
|
$this, |
|
60
|
|
|
$exceptionMessage |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Domain should be responsible for string emptiness |
|
66
|
|
|
* Exceptions are caught in order to be processed later |
|
67
|
|
|
* |
|
68
|
|
|
* @throws CommandMappingException If any mapping validation failed |
|
69
|
|
|
* @return mixed Untouched value |
|
70
|
|
|
*/ |
|
71
|
|
|
public function mustBeStringNotEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null) |
|
72
|
|
|
{ |
|
73
|
|
|
$value = $this->extractValueFromRequestQueryString($request, $queryStringKey); |
|
74
|
|
|
|
|
75
|
|
|
return $this->rawValueValidator->mustBeStringNotEmpty( |
|
|
|
|
|
|
76
|
|
|
$value, |
|
77
|
|
|
$queryStringKey, |
|
78
|
|
|
$this, |
|
79
|
|
|
$exceptionMessage |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Domain should be responsible for id format |
|
85
|
|
|
* Exceptions are caught in order to be processed later |
|
86
|
|
|
* |
|
87
|
|
|
* @throws CommandMappingException If any mapping validation failed |
|
88
|
|
|
* @return mixed Untouched value |
|
89
|
|
|
*/ |
|
90
|
|
|
public function mustHaveLengthBetween(ServerRequestInterface $request, string $queryStringKey, int $min, int $max, string $exceptionMessage = null) |
|
91
|
|
|
{ |
|
92
|
|
|
$value = $this->extractValueFromRequestQueryString($request, $queryStringKey); |
|
93
|
|
|
|
|
94
|
|
|
return $this->rawValueValidator->mustHaveLengthBetween( |
|
|
|
|
|
|
95
|
|
|
$value, |
|
96
|
|
|
$queryStringKey, |
|
97
|
|
|
$min, |
|
98
|
|
|
$max, |
|
99
|
|
|
$this, |
|
100
|
|
|
$exceptionMessage |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Domain should be responsible for email format |
|
106
|
|
|
* Exceptions are caught in order to be processed later |
|
107
|
|
|
* |
|
108
|
|
|
* @throws CommandMappingException If any mapping validation failed |
|
109
|
|
|
* @return mixed Untouched value |
|
110
|
|
|
*/ |
|
111
|
|
|
public function mustBeEmailAddress(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null) |
|
112
|
|
|
{ |
|
113
|
|
|
$value = $this->extractValueFromRequestQueryString($request, $queryStringKey); |
|
114
|
|
|
|
|
115
|
|
|
return $this->rawValueValidator->mustBeEmailAddress( |
|
|
|
|
|
|
116
|
|
|
$value, |
|
117
|
|
|
$queryStringKey, |
|
118
|
|
|
$this, |
|
119
|
|
|
$exceptionMessage |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Domain should be responsible for regex validation |
|
125
|
|
|
* Exceptions are caught in order to be processed later |
|
126
|
|
|
* |
|
127
|
|
|
* @throws CommandMappingException If any mapping validation failed |
|
128
|
|
|
* @return mixed Untouched value |
|
129
|
|
|
*/ |
|
130
|
|
|
public function mustBeValidAgainstRegex(ServerRequestInterface $request, string $pattern, string $queryStringKey, string $exceptionMessage = null) |
|
131
|
|
|
{ |
|
132
|
|
|
$value = $this->extractValueFromRequestQueryString($request, $queryStringKey); |
|
133
|
|
|
|
|
134
|
|
|
return $this->rawValueValidator->mustBeValidAgainstRegex( |
|
|
|
|
|
|
135
|
|
|
$value, |
|
136
|
|
|
$pattern, |
|
137
|
|
|
$queryStringKey, |
|
138
|
|
|
$this, |
|
139
|
|
|
$exceptionMessage |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.