vendor/api-platform/core/src/Hydra/Serializer/ConstraintViolationListNormalizer.php line 31

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\Hydra\Serializer;
  12. use ApiPlatform\Api\UrlGeneratorInterface;
  13. use ApiPlatform\Serializer\AbstractConstraintViolationListNormalizer;
  14. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  15. /**
  16. * Converts {@see \Symfony\Component\Validator\ConstraintViolationListInterface} to a Hydra error representation.
  17. *
  18. * @author Kévin Dunglas <dunglas@gmail.com>
  19. */
  20. final class ConstraintViolationListNormalizer extends AbstractConstraintViolationListNormalizer
  21. {
  22. public const FORMAT = 'jsonld';
  23. private $urlGenerator;
  24. public function __construct(UrlGeneratorInterface $urlGenerator, array $serializePayloadFields = null, NameConverterInterface $nameConverter = null)
  25. {
  26. parent::__construct($serializePayloadFields, $nameConverter);
  27. $this->urlGenerator = $urlGenerator;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. *
  32. * @return array|string|int|float|bool|\ArrayObject|null
  33. */
  34. public function normalize($object, $format = null, array $context = [])
  35. {
  36. [$messages, $violations] = $this->getMessagesAndViolations($object);
  37. return [
  38. '@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'ConstraintViolationList']),
  39. '@type' => 'ConstraintViolationList',
  40. 'hydra:title' => $context['title'] ?? 'An error occurred',
  41. 'hydra:description' => $messages ? implode("\n", $messages) : (string) $object,
  42. 'violations' => $violations,
  43. ];
  44. }
  45. }
  46. class_alias(ConstraintViolationListNormalizer::class, \ApiPlatform\Core\Hydra\Serializer\ConstraintViolationListNormalizer::class);