PageRenderTime 29ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Rules/IntVal.php

http://github.com/Respect/Validation
PHP | 44 lines | 18 code | 6 blank | 20 comment | 2 complexity | 8819edd323d6fed7fd6f8a2d300bc857 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of Respect/Validation.
  4. *
  5. * (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
  6. *
  7. * For the full copyright and license information, please view the LICENSE file
  8. * that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Respect\Validation\Rules;
  12. use function filter_var;
  13. use function is_bool;
  14. use function is_float;
  15. use const FILTER_FLAG_ALLOW_OCTAL;
  16. use const FILTER_VALIDATE_INT;
  17. /**
  18. * Validates if the input is an integer.
  19. *
  20. * @author Adam Benson <adam.benson@bigcommerce.com>
  21. * @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
  22. * @author Andrei Drulchenko <andrdru@gmail.com>
  23. * @author Danilo Benevides <danilobenevides01@gmail.com>
  24. * @author Henrique Moody <henriquemoody@gmail.com>
  25. */
  26. final class IntVal extends AbstractRule
  27. {
  28. /**
  29. * {@inheritDoc}
  30. */
  31. public function validate($input): bool
  32. {
  33. if (is_float($input) || is_bool($input)) {
  34. return false;
  35. }
  36. return filter_var($input, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_OCTAL) !== false;
  37. }
  38. }