NoneOf.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
  4. * SPDX-License-Identifier: MIT
  5. */
  6. declare(strict_types=1);
  7. namespace Respect\Validation\Rules;
  8. use Respect\Validation\Exceptions\NoneOfException;
  9. use function count;
  10. /**
  11. * @author Alexandre Gomes Gaigalas <alganet@gmail.com>
  12. * @author Henrique Moody <henriquemoody@gmail.com>
  13. */
  14. final class NoneOf extends AbstractComposite
  15. {
  16. /**
  17. * {@inheritDoc}
  18. */
  19. public function assert($input): void
  20. {
  21. $exceptions = $this->getAllThrownExceptions($input);
  22. $numRules = count($this->getRules());
  23. $numExceptions = count($exceptions);
  24. if ($numRules !== $numExceptions) {
  25. /** @var NoneOfException $noneOfException */
  26. $noneOfException = $this->reportError($input);
  27. $noneOfException->addChildren($exceptions);
  28. throw $noneOfException;
  29. }
  30. }
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function validate($input): bool
  35. {
  36. foreach ($this->getRules() as $rule) {
  37. if ($rule->validate($input)) {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. }