-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRequest.php
executable file
·382 lines (334 loc) · 9.88 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/**
* Copyright 2021 Jeremy Presutti <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare(strict_types=1);
namespace Feast;
use Feast\Enums\RequestMethod;
use Feast\Exception\InvalidArgumentException;
use Feast\Exception\InvalidDateException;
use Feast\Exception\ServerFailureException;
use Feast\Interfaces\RequestInterface;
use Feast\ServiceContainer\ContainerException;
use Feast\ServiceContainer\NotFoundException;
use Feast\ServiceContainer\ServiceContainerItemInterface;
use Feast\Traits\DependencyInjected;
use stdClass;
/**
* Manages the request argument including all arguments.
*/
class Request implements ServiceContainerItemInterface, RequestInterface
{
use DependencyInjected;
private stdClass $arguments;
private bool $isJson = false;
/**
* @throws ContainerException|NotFoundException
*/
public function __construct()
{
$this->checkInjected();
$this->arguments = new stdClass();
}
/**
* Clear all request arguments.
*/
public function clearArguments(): void
{
$this->arguments = new stdClass();
}
/**
* Set argument {name} to {value}.
*
* @param string $name
* @param string|array|float|bool|int|null $value
*/
public function setArgument(string $name, string|null|array|float|bool|int $value): void
{
if ($value === null) {
unset($this->arguments->{$name});
} else {
$this->arguments->{$name} = $value;
}
}
/**
* Get argument value as string.
*
* @param string $name
* @param string|null $default
* @return string|null
*/
public function getArgumentString(string $name, ?string $default = null): string|null
{
$argument = $this->arguments->{$name} ?? null;
if ($argument !== null) {
return (string)$argument;
}
return $default;
}
/**
* Get argument value as Date.
*
* @param string $name
* @param bool $throwOnFailure
* @return Date|null
* @throws InvalidDateException
*/
public function getArgumentDate(string $name, bool $throwOnFailure = false): Date|null
{
$argument = $this->getArgumentString($name);
if ($argument !== null) {
return $this->convertArgumentToDate($argument, $throwOnFailure);
}
return null;
}
/**
* Get argument value as bool.
*
* @param string $name
* @param bool|null $default
* @return bool|null
*/
public function getArgumentBool(string $name, ?bool $default = null): bool|null
{
if (isset($this->arguments->{$name})) {
/** @var string|int|bool|float $argument */
$argument = $this->arguments->{$name};
if (is_bool($argument)) {
return $argument;
}
return $this->convertArgumentToBool((string)$argument);
}
return $default;
}
/**
* Get argument value as int.
*
* @param string $name
* @param int|null $default
* @return int|null
* @throws InvalidArgumentException
*/
public function getArgumentInt(string $name, ?int $default = null): int|null
{
if (isset($this->arguments->{$name})) {
/** @var string|int|bool|float $argument */
$argument = $this->arguments->{$name};
if (is_int($argument)) {
return $argument;
}
return $this->convertArgumentToInt((string)$argument);
}
return $default;
}
/**
* Get argument value as float.
*
* @param string $name
* @param float|null $default
* @return float|null
* @throws InvalidArgumentException
*/
public function getArgumentFloat(string $name, ?float $default = null): float|null
{
if (isset($this->arguments->{$name})) {
/** @var string|int|bool|float $argument */
$argument = $this->arguments->{$name};
if (is_float($argument)) {
return $argument;
}
return $this->convertArgumentToFloat((string)$argument);
}
return $default;
}
/**
* Get argument value as array with all values inside converted to the specified type.
*
* @param string $name
* @param array|null $default
* @param string $type
* @return array|null
* @throws InvalidDateException
* @throws ServerFailureException
*/
public function getArgumentArray(string $name, ?array $default = null, string $type = 'string'): array|null
{
if (isset($this->arguments->{$name})) {
/** @var array<string>|string $variable */
$variable = $this->arguments->{$name};
if (!is_array($variable)) {
$variable = [$variable];
}
return match ($type) {
'int' => $this->convertArgumentArrayToInt($variable),
'bool' => $this->convertArgumentArrayToBool($variable),
'float' => $this->convertArgumentArrayToFloat($variable),
Date::class => $this->convertArgumentArrayToDate($variable),
default => $variable
};
}
return $default;
}
/**
* Get all arguments.
*/
public function getAllArguments(): stdClass
{
return $this->arguments;
}
/**
* Check whether request is a POST request.
*
* @return bool
*/
public function isPost(): bool
{
return !empty($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === RequestMethod::POST->value;
}
/**
* Check whether request is a GET request.
*
* @return bool
*/
public function isGet(): bool
{
return !empty($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === RequestMethod::GET->value;
}
/**
* Check whether request is a DELETE request.
*
* @return bool
*/
public function isDelete(): bool
{
return !empty($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === RequestMethod::DELETE->value;
}
/**
* Check whether request is a PUT request.
*
* @return bool
*/
public function isPut(): bool
{
return !empty($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === RequestMethod::PUT->value;
}
/**
* Check whether request is a PATCH request.
*
* @return bool
*/
public function isPatch(): bool
{
return !empty($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === RequestMethod::PATCH->value;
}
/**
* @param array<string> $values
* @return array<float>
* @throws ServerFailureException
*/
protected function convertArgumentArrayToFloat(array $values): array
{
$return = [];
foreach ($values as $value) {
$return[] = $this->convertArgumentToFloat($value);
}
return $return;
}
/**
* @param array<string> $values
* @return array<Date|null>
* @throws InvalidDateException
*/
protected function convertArgumentArrayToDate(array $values): array
{
$return = [];
foreach ($values as $value) {
$return[] = $this->convertArgumentToDate($value);
}
return $return;
}
/**
* @throws InvalidDateException
*/
protected function convertArgumentToDate(string $argument, bool $throwOnFailure = false): ?Date
{
try {
if (ctype_digit($argument)) {
return Date::createFromTimestamp((int)$argument);
}
return Date::createFromString($argument);
} catch (InvalidDateException $exception) {
if ($throwOnFailure) {
throw $exception;
}
return null;
}
}
/**
* @throws InvalidArgumentException
*/
protected function convertArgumentToFloat(string $argument): float
{
if (!is_numeric($argument)) {
throw new InvalidArgumentException('Expected a float value. Got ' . $argument);
}
return (float)$argument;
}
/**
* @param array<string> $values
* @return array<bool>
*/
protected function convertArgumentArrayToBool(array $values): array
{
$return = [];
foreach ($values as $value) {
$return[] = $this->convertArgumentToBool($value);
}
return $return;
}
protected function convertArgumentToBool(string $argument): bool
{
$trueValues = [
'on',
'1',
'true',
'yes'
];
return in_array(strtolower($argument), $trueValues);
}
/**
* @param array<string> $values
* @return array<int>
* @throws ServerFailureException
*/
protected function convertArgumentArrayToInt(array $values): array
{
$return = [];
foreach ($values as $value) {
$return[] = $this->convertArgumentToInt($value);
}
return $return;
}
/**
* @throws InvalidArgumentException
*/
protected function convertArgumentToInt(string $argument): int
{
if (!is_numeric($argument) || str_contains($argument, '.')) {
throw new InvalidArgumentException('Expected an integer value. Got ' . $argument);
}
return (int)$argument;
}
}