| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Normalizer; |
| 13 |
|
| 14 |
use Symfony\Component\ErrorHandler\Exception\FlattenException; |
| 15 |
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
| 16 |
use Symfony\Component\Messenger\Exception\ValidationFailedException as MessageValidationFailedException; |
| 17 |
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
| 18 |
use Symfony\Component\Serializer\Exception\PartialDenormalizationException; |
| 19 |
use Symfony\Component\Serializer\SerializerAwareInterface; |
| 20 |
use Symfony\Component\Serializer\SerializerAwareTrait; |
| 21 |
use Symfony\Component\Validator\Exception\ValidationFailedException; |
| 22 |
use Symfony\Contracts\Translation\TranslatorInterface; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
> |
| 30 |
|
| 31 |
|
| 32 |
class ProblemNormalizer implements NormalizerInterface, SerializerAwareInterface |
| 33 |
{ |
| 34 |
use SerializerAwareTrait; |
| 35 |
|
| 36 |
public const TITLE = 'title'; |
| 37 |
public const TYPE = 'type'; |
| 38 |
public const STATUS = 'status'; |
| 39 |
|
| 40 |
public function __construct( |
| 41 |
private bool $debug = false, |
| 42 |
private array $defaultContext = [], |
| 43 |
private ?TranslatorInterface $translator = null, |
| 44 |
) { |
| 45 |
} |
| 46 |
|
| 47 |
public function getSupportedTypes(?string $format): array |
| 48 |
{ |
| 49 |
return [ |
| 50 |
FlattenException::class => __CLASS__ === self::class, |
| 51 |
]; |
| 52 |
} |
| 53 |
|
| 54 |
public function normalize(mixed $object, ?string $format = null, array $context = []): array |
| 55 |
{ |
| 56 |
if (!$object instanceof FlattenException) { |
| 57 |
throw new InvalidArgumentException(\sprintf('The object must implement "%s".', FlattenException::class)); |
| 58 |
} |
| 59 |
|
| 60 |
$error = []; |
| 61 |
$context += $this->defaultContext; |
| 62 |
$debug = $this->debug && ($context['debug'] ?? true); |
| 63 |
$exception = $context['exception'] ?? null; |
| 64 |
if ($exception instanceof HttpExceptionInterface) { |
| 65 |
$exception = $exception->getPrevious(); |
| 66 |
|
| 67 |
if ($exception instanceof PartialDenormalizationException) { |
| 68 |
$trans = $this->translator ? $this->translator->trans(...) : static fn ($m, $p) => strtr($m, $p); |
| 69 |
$template = 'This value should be of type {{ type }}.'; |
| 70 |
$error = [ |
| 71 |
self::TYPE => 'https://symfony.com/errors/validation', |
| 72 |
self::TITLE => 'Validation Failed', |
| 73 |
'violations' => array_map( |
| 74 |
static fn ($e) => [ |
| 75 |
'propertyPath' => $e->getPath(), |
| 76 |
'title' => $trans($template, [ |
| 77 |
'{{ type }}' => implode('|', $e->getExpectedTypes() ?? ['?']), |
| 78 |
], 'validators'), |
| 79 |
'template' => $template, |
| 80 |
'parameters' => [ |
| 81 |
'{{ type }}' => implode('|', $e->getExpectedTypes() ?? ['?']), |
| 82 |
], |
| 83 |
] + ($debug || $e->canUseMessageForUser() ? ['hint' => $e->getMessage()] : []), |
| 84 |
$exception->getErrors() |
| 85 |
), |
| 86 |
]; |
| 87 |
$error['detail'] = implode("\n", array_map(static fn ($e) => $e['propertyPath'].': '.$e['title'], $error['violations'])); |
| 88 |
} elseif (($exception instanceof ValidationFailedException || $exception instanceof MessageValidationFailedException) |
| 89 |
&& $this->serializer instanceof NormalizerInterface |
| 90 |
&& $this->serializer->supportsNormalization($exception->getViolations(), $format, $context) |
| 91 |
) { |
| 92 |
$error = $this->serializer->normalize($exception->getViolations(), $format, $context); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
$error = [ |
| 97 |
self::TYPE => $error[self::TYPE] ?? $context[self::TYPE] ?? 'https://tools.ietf.org/html/rfc2616#section-10', |
| 98 |
self::TITLE => $error[self::TITLE] ?? $context[self::TITLE] ?? 'An error occurred', |
| 99 |
self::STATUS => $context[self::STATUS] ?? $object->getStatusCode(), |
| 100 |
'detail' => $error['detail'] ?? ($debug ? $object->getMessage() : $object->getStatusText()), |
| 101 |
] + $error; |
| 102 |
if ($debug) { |
| 103 |
$error['class'] = $object->getClass(); |
| 104 |
$error['trace'] = $object->getTrace(); |
| 105 |
} |
| 106 |
|
| 107 |
return $error; |
| 108 |
} |
| 109 |
|
| 110 |
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool |
| 111 |
{ |
| 112 |
return $data instanceof FlattenException; |
| 113 |
} |
| 114 |
} |