RequestQueryStringValueValidator::mustBeBoolean()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 7
nc 1
nop 3
crap 1
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 RequestQueryStringValueValidator 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 query string $_GET field can be mapped to a Command
23
     * Throw CommandMappingException directly if any mapping issue
24
     * @param ServerRequestInterface $request
25
     * @param string $queryStringKey
26
     *
27
     * @return mixed
28
     * @throws CommandMappingException If any mapping validation failed
29
     */
30
    public function extractValueFromRequestQueryString(ServerRequestInterface $request, string $queryStringKey)
31
    {
32 1
        $valueExtractor = new ValueExtractor();
33
34
        try {
35 1
            return $valueExtractor->fromArray(
36 1
                $request->getQueryParams(),
37
                $queryStringKey
38
            );
39 1
        } catch (InvalidArgumentException $exception) {
40 1
            throw CommandMappingException::fromParameter(
41 1
                $exception->getMessage(),
42 1
                $exception->getPropertyPath()
43
            );
44
        }
45
    }
46
47
    /**
48
     * Exceptions are caught in order to be processed later
49
     *
50
     * @throws CommandMappingException If any mapping validation failed
51
     */
52
    public function mustBeString(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): string
53
    {
54 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
55
56 1
        return $this->rawValueValidator->mustBeString(
57
            $value,
58
            $queryStringKey,
59
            $this,
60
            $exceptionMessage
61
        );
62
    }
63
64
    /**
65
     * Exceptions are caught in order to be processed later
66
     *
67
     * @throws CommandMappingException If any mapping validation failed
68
     * @return string|null
69
     */
70
    public function mustBeStringOrEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
71
    {
72 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
73
74 1
        return $this->rawValueValidator->mustBeStringOrEmpty(
75
            $value,
76
            $queryStringKey,
77
            $this,
78
            $exceptionMessage
79
        );
80
    }
81
82
    /**
83
     * Exceptions are caught in order to be processed later
84
     *
85
     * @throws CommandMappingException If any mapping validation failed
86
     */
87
    public function mustBeBoolean(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): bool
88
    {
89 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
90
91 1
        return $this->rawValueValidator->mustBeBoolean(
92
            $value,
93
            $queryStringKey,
94
            $this,
95
            $exceptionMessage
96
        );
97
    }
98
99
    /**
100
     * Exceptions are caught in order to be processed later
101
     *
102
     * @throws CommandMappingException If any mapping validation failed
103
     * @return bool|null
104
     */
105
    public function mustBeBooleanOrEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
106
    {
107 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
108
109 1
        return $this->rawValueValidator->mustBeBooleanOrEmpty(
110
            $value,
111
            $queryStringKey,
112
            $this,
113
            $exceptionMessage
114
        );
115
    }
116
117
    /**
118
     * Exceptions are caught in order to be processed later
119
     *
120
     * @throws CommandMappingException If any mapping validation failed
121
     */
122
    public function mustBeArray(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): array
123
    {
124 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
125
126 1
        return $this->rawValueValidator->mustBeArray(
127
            $value,
128
            $queryStringKey,
129
            $this,
130
            $exceptionMessage
131
        );
132
    }
133
134
    /**
135
     * Exceptions are caught in order to be processed later
136
     *
137
     * @throws CommandMappingException If any mapping validation failed
138
     */
139
    public function mustBeFloat(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): float
140
    {
141 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
142
143 1
        return $this->rawValueValidator->mustBeFloat(
144
            $value,
145
            $queryStringKey,
146
            $this,
147
            $exceptionMessage
148
        );
149
    }
150
151
    /**
152
     * Exceptions are caught in order to be processed later
153
     *
154
     * @throws CommandMappingException If any mapping validation failed
155
     */
156
    public function mustBeInteger(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): int
157
    {
158 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
159
160 1
        return $this->rawValueValidator->mustBeInteger(
161
            $value,
162
            $queryStringKey,
163
            $this,
164
            $exceptionMessage
165
        );
166
    }
167
168
    /**
169
     * Exceptions are caught in order to be processed later
170
     *
171
     * @throws CommandMappingException If any mapping validation failed
172
     * @return int|null
173
     */
174 View Code Duplication
    public function mustBeIntegerOrEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
175
    {
176 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
177
178 1
        return $this->rawValueValidator->mustBeIntegerOrEmpty(
179
            $value,
180
            $queryStringKey,
181
            $this,
182
            $exceptionMessage
183
        );
184
    }
185
186
    /**
187
     * Exceptions are caught in order to be processed later
188
     *
189
     * @throws CommandMappingException If any mapping validation failed
190
     */
191 View Code Duplication
    public function mustBeDate(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): \DateTime
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
192
    {
193 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
194
195 1
        return $this->rawValueValidator->mustBeDate(
196
            $value,
197
            $queryStringKey,
198
            $this,
199
            $exceptionMessage
200
        );
201
    }
202
203
    /**
204
     * Exceptions are caught in order to be processed later
205
     *
206
     * @throws CommandMappingException If any mapping validation failed
207
     */
208 View Code Duplication
    public function mustBeDateTime(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): \DateTime
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
209
    {
210 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
211
212 1
        return $this->rawValueValidator->mustBeDateTime(
213
            $value,
214
            $queryStringKey,
215
            $this,
216
            $exceptionMessage
217
        );
218
    }
219
220
    /**
221
     * Exceptions are caught in order to be processed later
222
     *
223
     * @return \DateTime|null
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\DateTime|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
224
     * @throws CommandMappingException If any mapping validation failed
225
     */
226
    public function mustBeDateTimeOrEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
227
    {
228 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
229
230 1
        return $this->rawValueValidator->mustBeDateTimeOrEmpty(
231
            $value,
232
            $queryStringKey,
233
            $this,
234
            $exceptionMessage
235
        );
236
    }
237
238
    /**
239
     * @inheritdoc
240
     */
241
    public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException
242
    {
243
        return UIValidationException::fromParameter($message, $propertyPath);
244
    }
245
}
246