PerfectSquare.php 744 B

123456789101112131415161718192021222324252627282930313233
  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 floor;
  9. use function is_numeric;
  10. use function sqrt;
  11. /**
  12. * Validates whether the input is a perfect square.
  13. *
  14. * @author Danilo Benevides <danilobenevides01@gmail.com>
  15. * @author Henrique Moody <henriquemoody@gmail.com>
  16. * @author Kleber Hamada Sato <kleberhs007@yahoo.com>
  17. * @author Nick Lombard <github@jigsoft.co.za>
  18. */
  19. final class PerfectSquare extends AbstractRule
  20. {
  21. /**
  22. * {@inheritDoc}
  23. */
  24. public function validate($input): bool
  25. {
  26. return is_numeric($input) && floor(sqrt((float) $input)) == sqrt((float) $input);
  27. }
  28. }