class ProblemNormalizer
Normalizes errors according to the API Problem spec (RFC 7807).
@author Kévin Dunglas <dunglas@gmail.com> @author Yonel Ceruto <yonelceruto@gmail.com>
Hierarchy
- class \Symfony\Component\Serializer\Normalizer\ProblemNormalizer implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface, \Symfony\Component\Serializer\SerializerAwareInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
Expanded class hierarchy of ProblemNormalizer
See also
https://tools.ietf.org/html/rfc7807
1 file declares its use of ProblemNormalizer
- ProblemNormalizerContextBuilder.php in vendor/
symfony/ serializer/ Context/ Normalizer/ ProblemNormalizerContextBuilder.php
File
-
vendor/
symfony/ serializer/ Normalizer/ ProblemNormalizer.php, line 32
Namespace
Symfony\Component\Serializer\NormalizerView source
class ProblemNormalizer implements NormalizerInterface, SerializerAwareInterface {
use SerializerAwareTrait;
public const TITLE = 'title';
public const TYPE = 'type';
public const STATUS = 'status';
public function __construct(bool $debug = false, array $defaultContext = [], ?TranslatorInterface $translator = null) {
}
public function getSupportedTypes(?string $format) : array {
return [
FlattenException::class => __CLASS__ === self::class,
];
}
public function normalize(mixed $object, ?string $format = null, array $context = []) : array {
if (!$object instanceof FlattenException) {
throw new InvalidArgumentException(\sprintf('The object must implement "%s".', FlattenException::class));
}
$data = [];
$context += $this->defaultContext;
$debug = $this->debug && ($context['debug'] ?? true);
$exception = $context['exception'] ?? null;
if ($exception instanceof HttpExceptionInterface) {
$exception = $exception->getPrevious();
if ($exception instanceof PartialDenormalizationException) {
$trans = $this->translator ? $this->translator
->trans(...) : fn($m, $p) => strtr($m, $p);
$template = 'This value should be of type {{ type }}.';
$data = [
self::TYPE => 'https://symfony.com/errors/validation',
self::TITLE => 'Validation Failed',
'violations' => array_map(fn($e) => [
'propertyPath' => $e->getPath(),
'title' => $trans($template, [
'{{ type }}' => implode('|', $e->getExpectedTypes() ?? [
'?',
]),
], 'validators'),
'template' => $template,
'parameters' => [
'{{ type }}' => implode('|', $e->getExpectedTypes() ?? [
'?',
]),
],
] + ($debug || $e->canUseMessageForUser() ? [
'hint' => $e->getMessage(),
] : []), $exception->getErrors()),
];
$data['detail'] = implode("\n", array_map(fn($e) => $e['propertyPath'] . ': ' . $e['title'], $data['violations']));
}
elseif (($exception instanceof ValidationFailedException || $exception instanceof MessageValidationFailedException) && $this->serializer instanceof NormalizerInterface && $this->serializer
->supportsNormalization($exception->getViolations(), $format, $context)) {
$data = $this->serializer
->normalize($exception->getViolations(), $format, $context);
}
}
$data = [
self::TYPE => $data[self::TYPE] ?? $context[self::TYPE] ?? 'https://tools.ietf.org/html/rfc2616#section-10',
self::TITLE => $data[self::TITLE] ?? $context[self::TITLE] ?? 'An error occurred',
self::STATUS => $context[self::STATUS] ?? $object->getStatusCode(),
'detail' => $data['detail'] ?? ($debug ? $object->getMessage() : $object->getStatusText()),
] + $data;
if ($debug) {
$data['class'] = $object->getClass();
$data['trace'] = $object->getTrace();
}
return $data;
}
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []) : bool {
return $data instanceof FlattenException;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ProblemNormalizer::getSupportedTypes | public | function | Returns the types potentially supported by this normalizer. | Overrides NormalizerInterface::getSupportedTypes |
ProblemNormalizer::normalize | public | function | Normalizes data into a set of arrays/scalars. | Overrides NormalizerInterface::normalize |
ProblemNormalizer::STATUS | public | constant | ||
ProblemNormalizer::supportsNormalization | public | function | Checks whether the given class is supported for normalization by this normalizer. | Overrides NormalizerInterface::supportsNormalization |
ProblemNormalizer::TITLE | public | constant | ||
ProblemNormalizer::TYPE | public | constant | ||
ProblemNormalizer::__construct | public | function | ||
SerializerAwareTrait::$serializer | protected | property | ||
SerializerAwareTrait::setSerializer | public | function |