kodedphp /
http
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Koded package. |
||
| 5 | * |
||
| 6 | * (c) Mihail Binev <[email protected]> |
||
| 7 | * |
||
| 8 | * Please view the LICENSE distributed with this source code |
||
| 9 | * for the full copyright and license information. |
||
| 10 | * |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace Koded\Http; |
||
| 14 | |||
| 15 | use Koded\Http\Interfaces\{HttpInputValidator, Response}; |
||
| 16 | use Koded\Stdlib\Immutable; |
||
| 17 | use function Koded\Stdlib\json_serialize; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @method Response|null getParsedBody |
||
| 21 | */ |
||
| 22 | trait ValidatableTrait |
||
| 23 | { |
||
| 24 | 4 | public function validate(HttpInputValidator $validator): ?Response |
|
| 25 | { |
||
| 26 | 4 | $body = new Immutable($this->getParsedBody() ?? []); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 27 | |||
| 28 | 4 | if (0 === $body->count()) { |
|
| 29 | 1 | return null; |
|
| 30 | } |
||
| 31 | |||
| 32 | 3 | if (empty($errors = $validator->validate($body))) { |
|
| 33 | 1 | return null; |
|
| 34 | } |
||
| 35 | |||
| 36 | 2 | $errors['code'] = (int)($errors['code'] ?? StatusCode::BAD_REQUEST); |
|
| 37 | 2 | return new ServerResponse(json_serialize($errors), $errors['code']); |
|
| 38 | } |
||
| 39 | } |
||
| 40 |