vendor/shopware/core/Framework/Validation/WriteConstraintViolationException.php line 10

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Validation;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Write\FieldException\WriteFieldException;
  4. use Shopware\Core\Framework\ShopwareHttpException;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Validator\ConstraintViolationList;
  7. class WriteConstraintViolationException extends ShopwareHttpException implements WriteFieldExceptionConstraintViolationExceptionInterface
  8. {
  9.     /**
  10.      * @var string
  11.      */
  12.     private $path;
  13.     /**
  14.      * @var ConstraintViolationList
  15.      */
  16.     private $constraintViolationList;
  17.     /**
  18.      * @var int
  19.      */
  20.     private $statusCode;
  21.     public function __construct(ConstraintViolationList $constraintViolationListstring $path ''int $statusCode Response::HTTP_BAD_REQUEST)
  22.     {
  23.         $this->path $path;
  24.         $this->constraintViolationList $constraintViolationList;
  25.         $this->statusCode $statusCode;
  26.         parent::__construct(
  27.             'Caught {{ count }} constraint violation errors.',
  28.             ['count' => $constraintViolationList->count()]
  29.         );
  30.     }
  31.     public function getErrorCode(): string
  32.     {
  33.         return 'FRAMEWORK__WRITE_CONSTRAINT_VIOLATION';
  34.     }
  35.     public function getStatusCode(): int
  36.     {
  37.         return $this->statusCode;
  38.     }
  39.     public function getViolations(): ConstraintViolationList
  40.     {
  41.         return $this->constraintViolationList;
  42.     }
  43.     public function setPath(string $path): void
  44.     {
  45.         $this->path $path;
  46.     }
  47.     public function getPath(): string
  48.     {
  49.         return $this->path;
  50.     }
  51.     public function toArray(): array
  52.     {
  53.         $result = [];
  54.         foreach ($this->constraintViolationList as $violation) {
  55.             $result[] = [
  56.                 'message' => $violation->getMessage(),
  57.                 'messageTemplate' => $violation->getMessageTemplate(),
  58.                 'parameters' => $violation->getParameters(),
  59.                 'propertyPath' => $violation->getPropertyPath(),
  60.             ];
  61.         }
  62.         return $result;
  63.     }
  64.     public function getErrors(bool $withTrace false): \Generator
  65.     {
  66.         foreach ($this->getViolations() as $violation) {
  67.             $path $this->getPath() . $violation->getPropertyPath();
  68.             $error = [
  69.                 'code' => $violation->getCode() ?? $this->getErrorCode(),
  70.                 'status' => (string) $this->getStatusCode(),
  71.                 'detail' => $violation->getMessage(),
  72.                 'template' => $violation->getMessageTemplate(),
  73.                 'meta' => [
  74.                     'parameters' => $violation->getParameters(),
  75.                 ],
  76.                 'source' => [
  77.                     'pointer' => $path,
  78.                 ],
  79.             ];
  80.             if ($withTrace) {
  81.                 $error['trace'] = $this->getTrace();
  82.             }
  83.             yield $error;
  84.         }
  85.     }
  86. }