/** * @param ConstraintViolationListInterface|null $violations * @param array $item * @param array $messageParameters * @param int $code * @param \Exception|null $previous */ public function __construct(ConstraintViolationListInterface $violations, InvalidItemInterface $item, array $messageParameters = [], $code = 0, \Exception $previous = null) { $this->violations = $violations; $errors = []; /** @var ConstraintViolationInterface $violation */ foreach ($violations as $violation) { // TODO: re-format the message, property path doesn't exist for class constraint // TODO: for instance cf VariantGroupAxis $invalidValue = $violation->getInvalidValue(); if ($invalidValue instanceof ProductPriceInterface) { $invalidValue = sprintf('%s %s', $invalidValue->getData(), $invalidValue->getCurrency()); } elseif (is_object($invalidValue) && method_exists($invalidValue, '__toString')) { $invalidValue = (string) $invalidValue; } elseif (is_object($invalidValue)) { $invalidValue = get_class($invalidValue); } elseif (is_array($invalidValue)) { $invalidValue = implode(', ', $invalidValue); } $errors[] = sprintf("%s: %s: %s\n", $violation->getPropertyPath(), $violation->getMessage(), $invalidValue); } parent::__construct(implode("\n", $errors), $item, $messageParameters, $code, $previous); }
/** * Constructor * * @param string $code * @param string $value * @param array $item */ public function __construct($code, $value, array $item) { $this->messageParameters = ['%code%' => $code, '%value%' => $value]; $exception = new ParametrizedException($this->messageTemplate, $this->messageParameters); parent::__construct($exception->getMessage(), $item, $this->messageParameters); }
/** * Constructor * * @param string $identifier * @param array $item */ public function __construct($identifier, array $item) { $messageParameters = ['%identifier%' => $identifier]; $exception = new ParametrizedException($this->messageTemplate, $messageParameters); parent::__construct($exception->getMessage(), $item, $messageParameters); }