vendor/api-platform/core/src/Serializer/AbstractConstraintViolationListNormalizer.php line 36

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the API Platform project.
  4. *
  5. * (c) Kévin Dunglas <dunglas@gmail.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. declare(strict_types=1);
  11. namespace ApiPlatform\Serializer;
  12. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  13. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  14. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  15. use Symfony\Component\Validator\ConstraintViolation;
  16. use Symfony\Component\Validator\ConstraintViolationListInterface;
  17. /**
  18. * Common features regarding Constraint Violation normalization.
  19. *
  20. * @author Kévin Dunglas <dunglas@gmail.com>
  21. *
  22. * @internal
  23. */
  24. abstract class AbstractConstraintViolationListNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
  25. {
  26. public const FORMAT = null; // Must be overrode
  27. private $serializePayloadFields;
  28. private $nameConverter;
  29. public function __construct(array $serializePayloadFields = null, NameConverterInterface $nameConverter = null)
  30. {
  31. $this->nameConverter = $nameConverter;
  32. $this->serializePayloadFields = null === $serializePayloadFields ? null : array_flip($serializePayloadFields);
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function supportsNormalization($data, $format = null, array $context = []): bool
  38. {
  39. return static::FORMAT === $format && $data instanceof ConstraintViolationListInterface;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function hasCacheableSupportsMethod(): bool
  45. {
  46. return true;
  47. }
  48. protected function getMessagesAndViolations(ConstraintViolationListInterface $constraintViolationList): array
  49. {
  50. $violations = $messages = [];
  51. foreach ($constraintViolationList as $violation) {
  52. $class = \is_object($root = $violation->getRoot()) ? \get_class($root) : null;
  53. $violationData = [
  54. 'propertyPath' => $this->nameConverter ? $this->nameConverter->normalize($violation->getPropertyPath(), $class, static::FORMAT) : $violation->getPropertyPath(),
  55. 'message' => $violation->getMessage(),
  56. 'code' => $violation->getCode(),
  57. ];
  58. $constraint = $violation instanceof ConstraintViolation ? $violation->getConstraint() : null;
  59. if (
  60. [] !== $this->serializePayloadFields &&
  61. $constraint &&
  62. $constraint->payload &&
  63. // If some fields are whitelisted, only them are added
  64. $payloadFields = null === $this->serializePayloadFields ? $constraint->payload : array_intersect_key($constraint->payload, $this->serializePayloadFields)
  65. ) {
  66. $violationData['payload'] = $payloadFields;
  67. }
  68. $violations[] = $violationData;
  69. $messages[] = ($violationData['propertyPath'] ? "{$violationData['propertyPath']}: " : '').$violationData['message'];
  70. }
  71. return [$messages, $violations];
  72. }
  73. }
  74. class_alias(AbstractConstraintViolationListNormalizer::class, \ApiPlatform\Core\Serializer\AbstractConstraintViolationListNormalizer::class);