vendor/symfony/serializer/Normalizer/ConstraintViolationListNormalizer.php line 45

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Serializer\Normalizer;
  11. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  12. use Symfony\Component\Validator\ConstraintViolationListInterface;
  13. /**
  14.  * A normalizer that normalizes a ConstraintViolationListInterface instance.
  15.  *
  16.  * This Normalizer implements RFC7807 {@link https://tools.ietf.org/html/rfc7807}.
  17.  *
  18.  * @author Grégoire Pineau <lyrixx@lyrixx.info>
  19.  * @author Kévin Dunglas <dunglas@gmail.com>
  20.  */
  21. class ConstraintViolationListNormalizer implements NormalizerInterfaceCacheableSupportsMethodInterface
  22. {
  23.     public const INSTANCE 'instance';
  24.     public const STATUS 'status';
  25.     public const TITLE 'title';
  26.     public const TYPE 'type';
  27.     public const PAYLOAD_FIELDS 'payload_fields';
  28.     private $defaultContext;
  29.     private $nameConverter;
  30.     public function __construct(array $defaultContext = [], NameConverterInterface $nameConverter null)
  31.     {
  32.         $this->defaultContext $defaultContext;
  33.         $this->nameConverter $nameConverter;
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function normalize(mixed $objectstring $format null, array $context = []): array
  39.     {
  40.         if (\array_key_exists(self::PAYLOAD_FIELDS$context)) {
  41.             $payloadFieldsToSerialize $context[self::PAYLOAD_FIELDS];
  42.         } elseif (\array_key_exists(self::PAYLOAD_FIELDS$this->defaultContext)) {
  43.             $payloadFieldsToSerialize $this->defaultContext[self::PAYLOAD_FIELDS];
  44.         } else {
  45.             $payloadFieldsToSerialize = [];
  46.         }
  47.         if (\is_array($payloadFieldsToSerialize) && [] !== $payloadFieldsToSerialize) {
  48.             $payloadFieldsToSerialize array_flip($payloadFieldsToSerialize);
  49.         }
  50.         $violations = [];
  51.         $messages = [];
  52.         foreach ($object as $violation) {
  53.             $propertyPath $this->nameConverter $this->nameConverter->normalize($violation->getPropertyPath(), null$format$context) : $violation->getPropertyPath();
  54.             $violationEntry = [
  55.                 'propertyPath' => $propertyPath,
  56.                 'title' => $violation->getMessage(),
  57.                 'parameters' => $violation->getParameters(),
  58.             ];
  59.             if (null !== $code $violation->getCode()) {
  60.                 $violationEntry['type'] = sprintf('urn:uuid:%s'$code);
  61.             }
  62.             $constraint $violation->getConstraint();
  63.             if (
  64.                 [] !== $payloadFieldsToSerialize &&
  65.                 $constraint &&
  66.                 $constraint->payload &&
  67.                 // If some or all payload fields are whitelisted, add them
  68.                 $payloadFields null === $payloadFieldsToSerialize || true === $payloadFieldsToSerialize $constraint->payload array_intersect_key($constraint->payload$payloadFieldsToSerialize)
  69.             ) {
  70.                 $violationEntry['payload'] = $payloadFields;
  71.             }
  72.             $violations[] = $violationEntry;
  73.             $prefix $propertyPath sprintf('%s: '$propertyPath) : '';
  74.             $messages[] = $prefix.$violation->getMessage();
  75.         }
  76.         $result = [
  77.             'type' => $context[self::TYPE] ?? $this->defaultContext[self::TYPE] ?? 'https://symfony.com/errors/validation',
  78.             'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE] ?? 'Validation Failed',
  79.         ];
  80.         if (null !== $status = ($context[self::STATUS] ?? $this->defaultContext[self::STATUS] ?? null)) {
  81.             $result['status'] = $status;
  82.         }
  83.         if ($messages) {
  84.             $result['detail'] = implode("\n"$messages);
  85.         }
  86.         if (null !== $instance = ($context[self::INSTANCE] ?? $this->defaultContext[self::INSTANCE] ?? null)) {
  87.             $result['instance'] = $instance;
  88.         }
  89.         return $result + ['violations' => $violations];
  90.     }
  91.     /**
  92.      * {@inheritdoc}
  93.      *
  94.      * @param array $context
  95.      */
  96.     public function supportsNormalization(mixed $datastring $format null /* , array $context = [] */): bool
  97.     {
  98.         return $data instanceof ConstraintViolationListInterface;
  99.     }
  100.     /**
  101.      * {@inheritdoc}
  102.      */
  103.     public function hasCacheableSupportsMethod(): bool
  104.     {
  105.         return __CLASS__ === static::class;
  106.     }
  107. }