Odd.php 814 B

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