IntVal.php 890 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 is_int;
  9. use function is_string;
  10. use function preg_match;
  11. /**
  12. * Validates if the input is an integer.
  13. *
  14. * @author Adam Benson <adam.benson@bigcommerce.com>
  15. * @author Alexandre Gomes Gaigalas <alganet@gmail.com>
  16. * @author Andrei Drulchenko <andrdru@gmail.com>
  17. * @author Danilo Benevides <danilobenevides01@gmail.com>
  18. * @author Henrique Moody <henriquemoody@gmail.com>
  19. */
  20. final class IntVal extends AbstractRule
  21. {
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function validate($input): bool
  26. {
  27. if (is_int($input)) {
  28. return true;
  29. }
  30. if (!is_string($input)) {
  31. return false;
  32. }
  33. return preg_match('/^-?\d+$/', $input) === 1;
  34. }
  35. }