vendor/api-platform/core/src/Problem/Serializer/ConstraintViolationListNormalizer.php line 37

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\Problem\Serializer;
  12. use ApiPlatform\Serializer\AbstractConstraintViolationListNormalizer;
  13. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  14. /**
  15. * Converts {@see \Symfony\Component\Validator\ConstraintViolationListInterface} the API Problem spec (RFC 7807).
  16. *
  17. * @see https://tools.ietf.org/html/rfc7807
  18. *
  19. * @author Kévin Dunglas <dunglas@gmail.com>
  20. */
  21. final class ConstraintViolationListNormalizer extends AbstractConstraintViolationListNormalizer
  22. {
  23. public const FORMAT = 'jsonproblem';
  24. public const TYPE = 'type';
  25. public const TITLE = 'title';
  26. private $defaultContext = [
  27. self::TYPE => 'https://tools.ietf.org/html/rfc2616#section-10',
  28. self::TITLE => 'An error occurred',
  29. ];
  30. public function __construct(array $serializePayloadFields = null, NameConverterInterface $nameConverter = null, array $defaultContext = [])
  31. {
  32. parent::__construct($serializePayloadFields, $nameConverter);
  33. $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function normalize($object, $format = null, array $context = []): array
  39. {
  40. [$messages, $violations] = $this->getMessagesAndViolations($object);
  41. return [
  42. 'type' => $context[self::TYPE] ?? $this->defaultContext[self::TYPE],
  43. 'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
  44. 'detail' => $messages ? implode("\n", $messages) : (string) $object,
  45. 'violations' => $violations,
  46. ];
  47. }
  48. }
  49. class_alias(ConstraintViolationListNormalizer::class, \ApiPlatform\Core\Problem\Serializer\ConstraintViolationListNormalizer::class);