LeapYear.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 DateTimeInterface;
  9. use function date;
  10. use function is_numeric;
  11. use function is_scalar;
  12. use function sprintf;
  13. use function strtotime;
  14. /**
  15. * Validates if a year is leap.
  16. *
  17. * @author Danilo Correa <danilosilva87@gmail.com>
  18. * @author Henrique Moody <henriquemoody@gmail.com>
  19. * @author Jayson Reis <santosdosreis@gmail.com>
  20. */
  21. final class LeapYear extends AbstractRule
  22. {
  23. /**
  24. * {@inheritDoc}
  25. */
  26. public function validate($input): bool
  27. {
  28. if (is_numeric($input)) {
  29. $date = strtotime(sprintf('%d-02-29', (int) $input));
  30. return (bool) date('L', (int) $date);
  31. }
  32. if (is_scalar($input)) {
  33. return $this->validate((int) date('Y', (int) strtotime((string) $input)));
  34. }
  35. if ($input instanceof DateTimeInterface) {
  36. return $this->validate($input->format('Y'));
  37. }
  38. return false;
  39. }
  40. }