Even.php 721 B

123456789101112131415161718192021222324252627282930313233343536
  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 function filter_var;
  9. use const FILTER_VALIDATE_INT;
  10. /**
  11. * Validates whether the input is an even number or not.
  12. *
  13. * @author Henrique Moody <henriquemoody@gmail.com>
  14. * @author Jean Pimentel <jeanfap@gmail.com>
  15. * @author Paul Karikari <paulkarikari1@gmail.com>
  16. */
  17. final class Even extends AbstractRule
  18. {
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function validate($input): bool
  23. {
  24. if (filter_var($input, FILTER_VALIDATE_INT) === false) {
  25. return false;
  26. }
  27. return (int) $input % 2 === 0;
  28. }
  29. }