|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Valkyrja Framework package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Valkyrja\Http\Struct\Request\Trait; |
|
15
|
|
|
|
|
16
|
|
|
use Valkyrja\Http\Message\Request\Contract\ServerRequest; |
|
17
|
|
|
use Valkyrja\Type\BuiltIn\Enum\Trait\Arrayable; |
|
18
|
|
|
use Valkyrja\Validation\Validator\Contract\Validator as ValidatorContract; |
|
19
|
|
|
use Valkyrja\Validation\Validator\Validator; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Trait RequestStruct. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Melech Mizrachi |
|
25
|
|
|
*/ |
|
26
|
|
|
trait RequestStruct |
|
27
|
|
|
{ |
|
28
|
|
|
use Arrayable; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @inheritDoc |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function getDataFromRequest(ServerRequest $request): array |
|
34
|
|
|
{ |
|
35
|
|
|
return static::getOnlyParamsFromRequest($request, ...static::values()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritDoc |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function determineIfRequestContainsExtraData(ServerRequest $request): bool |
|
42
|
|
|
{ |
|
43
|
|
|
return ! empty(static::getExceptParamsFromRequest($request, ...static::values())); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritDoc |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function getValidationRules(ServerRequest $request): array|null |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritDoc |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function validate(ServerRequest $request): ValidatorContract |
|
58
|
|
|
{ |
|
59
|
|
|
return new Validator(static::getValidationRules($request) ?? []); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get only the specified request params. |
|
64
|
|
|
* |
|
65
|
|
|
* @param ServerRequest $request The request |
|
66
|
|
|
* @param string|int ...$values The values |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
abstract protected static function getOnlyParamsFromRequest(ServerRequest $request, string|int ...$values): array; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get all request params except the ones specified. |
|
74
|
|
|
* |
|
75
|
|
|
* @param ServerRequest $request The request |
|
76
|
|
|
* @param string|int ...$values The values |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
abstract protected static function getExceptParamsFromRequest(ServerRequest $request, string|int ...$values): array; |
|
81
|
|
|
} |
|
82
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.