vendor/api-platform/core/src/Symfony/Validator/Exception/ValidationException.php line 70

  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\Symfony\Validator\Exception;
  12. use ApiPlatform\JsonLd\ContextBuilderInterface;
  13. use ApiPlatform\Metadata\Error as ErrorOperation;
  14. use ApiPlatform\Metadata\ErrorResource;
  15. use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
  16. use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
  17. use ApiPlatform\Validator\Exception\ValidationException as BaseValidationException;
  18. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  19. use Symfony\Component\Serializer\Annotation\Groups;
  20. use Symfony\Component\Serializer\Annotation\SerializedName;
  21. use Symfony\Component\Validator\ConstraintViolationListInterface;
  22. use Symfony\Component\WebLink\Link;
  23. /**
  24.  * Thrown when a validation error occurs.
  25.  *
  26.  * @author Kévin Dunglas <dunglas@gmail.com>
  27.  */
  28. #[ErrorResource(
  29.     uriTemplate'/validation_errors/{id}',
  30.     status422,
  31.     openapifalse,
  32.     uriVariables: ['id'],
  33.     provider'api_platform.validator.state.error_provider',
  34.     shortName'ConstraintViolationList',
  35.     operations: [
  36.         new ErrorOperation(
  37.             name'_api_validation_errors_problem',
  38.             outputFormats: ['json' => ['application/problem+json']],
  39.             normalizationContext: ['groups' => ['json'],
  40.                 'skip_null_values' => true,
  41.                 'rfc_7807_compliant_errors' => true,
  42.             ]),
  43.         new ErrorOperation(
  44.             name'_api_validation_errors_hydra',
  45.             outputFormats: ['jsonld' => ['application/problem+json']],
  46.             links: [new Link(relContextBuilderInterface::JSONLD_NS.'error'href'http://www.w3.org/ns/hydra/error')],
  47.             normalizationContext: [
  48.                 'groups' => ['jsonld'],
  49.                 'skip_null_values' => true,
  50.                 'rfc_7807_compliant_errors' => true,
  51.             ]
  52.         ),
  53.         new ErrorOperation(
  54.             name'_api_validation_errors_jsonapi',
  55.             outputFormats: ['jsonapi' => ['application/vnd.api+json']],
  56.             normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true'rfc_7807_compliant_errors' => true]
  57.         ),
  58.     ],
  59.     graphQlOperations: []
  60. )]
  61. final class ValidationException extends BaseValidationException implements ConstraintViolationListAwareExceptionInterface\StringableProblemExceptionInterfaceHttpExceptionInterface
  62. {
  63.     private int $status 422;
  64.     public function __construct(private readonly ConstraintViolationListInterface $constraintViolationListstring $message ''int $code 0\Throwable $previous nullstring $errorTitle null)
  65.     {
  66.         parent::__construct($message ?: $this->__toString(), $code$previous$errorTitle);
  67.     }
  68.     public function getId(): string
  69.     {
  70.         $ids = [];
  71.         foreach ($this->getConstraintViolationList() as $violation) {
  72.             $ids[] = $violation->getCode();
  73.         }
  74.         $id \count($ids) ? CompositeIdentifierParser::stringify(identifiers$ids) : ($ids[0] ?? null);
  75.         if (!$id) {
  76.             return spl_object_hash($this);
  77.         }
  78.         return $id;
  79.     }
  80.     #[SerializedName('hydra:title')]
  81.     #[Groups(['jsonld'])]
  82.     public function getHydraTitle(): string
  83.     {
  84.         return $this->errorTitle ?? 'An error occurred';
  85.     }
  86.     #[Groups(['jsonld'])]
  87.     #[SerializedName('hydra:description')]
  88.     public function getHydraDescription(): string
  89.     {
  90.         return $this->detail;
  91.     }
  92.     #[Groups(['jsonld''json'])]
  93.     public function getType(): string
  94.     {
  95.         return '/validation_errors/'.$this->getId();
  96.     }
  97.     #[Groups(['jsonld''json'])]
  98.     public function getTitle(): ?string
  99.     {
  100.         return $this->errorTitle ?? 'An error occurred';
  101.     }
  102.     #[Groups(['jsonld''json'])]
  103.     private string $detail;
  104.     public function getDetail(): ?string
  105.     {
  106.         return $this->detail;
  107.     }
  108.     public function setDetail(string $detail): void
  109.     {
  110.         $this->detail $detail;
  111.     }
  112.     #[Groups(['jsonld''json'])]
  113.     public function getStatus(): ?int
  114.     {
  115.         return $this->status;
  116.     }
  117.     public function setStatus(int $status): void
  118.     {
  119.         $this->status $status;
  120.     }
  121.     #[Groups(['jsonld''json'])]
  122.     public function getInstance(): ?string
  123.     {
  124.         return null;
  125.     }
  126.     #[SerializedName('violations')]
  127.     #[Groups(['json''jsonld'])]
  128.     public function getConstraintViolationList(): ConstraintViolationListInterface
  129.     {
  130.         return $this->constraintViolationList;
  131.     }
  132.     public function __toString(): string
  133.     {
  134.         $message '';
  135.         foreach ($this->constraintViolationList as $violation) {
  136.             if ('' !== $message) {
  137.                 $message .= "\n";
  138.             }
  139.             if ($propertyPath $violation->getPropertyPath()) {
  140.                 $message .= "$propertyPath: ";
  141.             }
  142.             $message .= $violation->getMessage();
  143.         }
  144.         return $message;
  145.     }
  146.     public function getStatusCode(): int
  147.     {
  148.         return $this->status;
  149.     }
  150.     public function getHeaders(): array
  151.     {
  152.         return [];
  153.     }
  154. }