1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Middleware; |
4
|
|
|
|
5
|
|
|
use App\Support\Validation\ValidationException; |
6
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
11
|
|
|
|
12
|
|
|
final class ValidationMiddleware implements MiddlewareInterface |
13
|
|
|
{ |
14
|
|
|
private ResponseFactoryInterface $responseFactory; |
15
|
|
|
|
16
|
10 |
|
public function __construct(ResponseFactoryInterface $responseFactory) |
17
|
|
|
{ |
18
|
10 |
|
$this->responseFactory = $responseFactory; |
19
|
|
|
} |
20
|
|
|
|
21
|
10 |
|
public function process( |
22
|
|
|
ServerRequestInterface $request, |
23
|
|
|
RequestHandlerInterface $handler |
24
|
|
|
): ResponseInterface { |
25
|
|
|
try { |
26
|
10 |
|
return $handler->handle($request); |
27
|
3 |
|
} catch (ValidationException $exception) { |
28
|
2 |
|
$response = $this->responseFactory->createResponse(); |
29
|
2 |
|
$data = $this->transform($exception); |
30
|
2 |
|
$json = (string)json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR); |
31
|
2 |
|
$response->getBody()->write($json); |
32
|
|
|
|
33
|
2 |
|
return $response |
34
|
2 |
|
->withStatus(422) |
35
|
2 |
|
->withHeader('Content-Type', 'application/json'); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
private function transform(ValidationException $exception): array |
40
|
|
|
{ |
41
|
2 |
|
$error = [ |
42
|
2 |
|
'message' => $exception->getMessage(), |
43
|
2 |
|
]; |
44
|
|
|
|
45
|
2 |
|
$errors = $exception->getErrors(); |
46
|
2 |
|
if ($errors) { |
|
|
|
|
47
|
2 |
|
$error['details'] = $this->addErrors([], $errors); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
return ['error' => $error]; |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
private function addErrors(array $result, array $errors, string $path = ''): array |
54
|
|
|
{ |
55
|
2 |
|
foreach ($errors as $field => $error) { |
56
|
2 |
|
$oldPath = $path; |
57
|
2 |
|
$path .= ($path === '' ? '' : '.') . $field; |
58
|
2 |
|
$result = $this->addSubErrors($result, $error, $path); |
59
|
2 |
|
$path = $oldPath; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
return $result; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
private function addSubErrors(array $result, array $error, string $path = ''): array |
66
|
|
|
{ |
67
|
2 |
|
foreach ($error as $field2 => $errorMessage) { |
68
|
2 |
|
if (is_array($errorMessage)) { |
69
|
|
|
$result = $this->addErrors($result, [$field2 => $errorMessage], $path); |
70
|
|
|
} else { |
71
|
2 |
|
$result[] = [ |
72
|
2 |
|
'message' => $errorMessage, |
73
|
2 |
|
'field' => $path, |
74
|
2 |
|
]; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
return $result; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.