Няма описание

ValidationException.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace MailPoet\Doctrine\Validator;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoetVendor\Symfony\Component\Validator\ConstraintViolationInterface;
  5. use MailPoetVendor\Symfony\Component\Validator\ConstraintViolationListInterface;
  6. class ValidationException extends \RuntimeException {
  7. /** @var string */
  8. private $resourceName;
  9. /** @var ConstraintViolationListInterface|ConstraintViolationInterface[] */
  10. private $violations;
  11. public function __construct($resourceName, ConstraintViolationListInterface $violations) {
  12. $this->resourceName = $resourceName;
  13. $this->violations = $violations;
  14. $linePrefix = ' ';
  15. $message = "Validation failed for '$resourceName'.\nDetails:\n";
  16. $message .= $linePrefix . implode("\n$linePrefix", $this->getErrors());
  17. parent::__construct($message);
  18. }
  19. /** @return string */
  20. public function getResourceName() {
  21. return $this->resourceName;
  22. }
  23. /** @return ConstraintViolationListInterface|ConstraintViolationInterface[] */
  24. public function getViolations() {
  25. return $this->violations;
  26. }
  27. /** @return string[] */
  28. public function getErrors() {
  29. $messages = [];
  30. foreach ($this->violations as $violation) {
  31. $messages[] = $this->formatError($violation);
  32. }
  33. sort($messages);
  34. return $messages;
  35. }
  36. private function formatError(ConstraintViolationInterface $violation) {
  37. return '[' . $violation->getPropertyPath() . '] ' . $violation->getMessage();
  38. }
  39. }