AllOf.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\AllOfException;
  9. use function count;
  10. /**
  11. * @author Alexandre Gomes Gaigalas <alganet@gmail.com>
  12. * @author Henrique Moody <henriquemoody@gmail.com>
  13. */
  14. class AllOf 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. $summary = [
  25. 'total' => $numRules,
  26. 'failed' => $numExceptions,
  27. 'passed' => $numRules - $numExceptions,
  28. ];
  29. if (!empty($exceptions)) {
  30. /** @var AllOfException $allOfException */
  31. $allOfException = $this->reportError($input, $summary);
  32. $allOfException->addChildren($exceptions);
  33. throw $allOfException;
  34. }
  35. }
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function check($input): void
  40. {
  41. foreach ($this->getRules() as $rule) {
  42. $rule->check($input);
  43. }
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. public function validate($input): bool
  49. {
  50. foreach ($this->getRules() as $rule) {
  51. if (!$rule->validate($input)) {
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57. }