1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nelexa\RequestDtoBundle\Normalizer; |
6
|
|
|
|
7
|
|
|
use Nelexa\RequestDtoBundle\Exception\RequestDtoValidationException; |
8
|
|
|
use Symfony\Component\Serializer\Exception\ExceptionInterface; |
9
|
|
|
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer; |
10
|
|
|
use Symfony\Component\Serializer\Normalizer\ProblemNormalizer; |
11
|
|
|
|
12
|
|
|
class RequestDtoExceptionNormalizer extends ProblemNormalizer |
13
|
|
|
{ |
14
|
|
|
private ConstraintViolationListNormalizer $normalizer; |
15
|
|
|
|
16
|
|
|
private bool $debug; |
17
|
|
|
|
18
|
19 |
|
public function __construct( |
19
|
|
|
ConstraintViolationListNormalizer $normalizer, |
20
|
|
|
bool $debug = false, |
21
|
|
|
array $defaultContext = [] |
22
|
|
|
) { |
23
|
19 |
|
parent::__construct($debug, $defaultContext); |
24
|
19 |
|
$this->normalizer = $normalizer; |
25
|
19 |
|
$this->debug = $debug; |
26
|
19 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param RequestDtoValidationException $exception |
30
|
|
|
* |
31
|
|
|
* @throws ExceptionInterface |
32
|
|
|
* |
33
|
|
|
* @return array|\ArrayObject|bool|float|int|string|null |
34
|
|
|
*/ |
35
|
3 |
|
public function normalize($exception, ?string $format = null, array $context = []) |
36
|
|
|
{ |
37
|
|
|
$context += [ |
38
|
3 |
|
'type' => 'https://tools.ietf.org/html/rfc7807', |
39
|
|
|
]; |
40
|
3 |
|
$data = $this->normalizer->normalize($exception->getErrors(), $format, $context); |
41
|
3 |
|
$data['status'] = $exception->getStatusCode(); |
42
|
|
|
|
43
|
3 |
|
if ($this->debug) { |
44
|
3 |
|
$data['class'] = \get_class($exception); |
45
|
3 |
|
$data['trace'] = $exception->getTrace(); |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
return $data; |
49
|
|
|
} |
50
|
|
|
|
51
|
18 |
|
public function supportsNormalization($data, ?string $format = null): bool |
52
|
|
|
{ |
53
|
18 |
|
return $data instanceof RequestDtoValidationException; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|