LazyAssertionException.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Assert
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to kontakt@beberlei.de so I can send you a copy immediately.
  12. */
  13. namespace Assert;
  14. class LazyAssertionException extends InvalidArgumentException
  15. {
  16. /**
  17. * @var InvalidArgumentException[]
  18. */
  19. private $errors = [];
  20. /**
  21. * @param InvalidArgumentException[] $errors
  22. */
  23. public static function fromErrors(array $errors): self
  24. {
  25. $message = \sprintf('The following %d assertions failed:', \count($errors))."\n";
  26. $i = 1;
  27. foreach ($errors as $error) {
  28. $message .= \sprintf("%d) %s: %s\n", $i++, $error->getPropertyPath(), $error->getMessage());
  29. }
  30. return new static($message, $errors);
  31. }
  32. public function __construct($message, array $errors)
  33. {
  34. parent::__construct($message, 0, null, null);
  35. $this->errors = $errors;
  36. }
  37. /**
  38. * @return InvalidArgumentException[]
  39. */
  40. public function getErrorExceptions(): array
  41. {
  42. return $this->errors;
  43. }
  44. }