Imei.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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_scalar;
  9. use function mb_strlen;
  10. use function preg_replace;
  11. /**
  12. * Validates is the input is a valid IMEI.
  13. *
  14. * @author Alexander Gorshkov <mazanax@yandex.ru>
  15. * @author Danilo Benevides <danilobenevides01@gmail.com>
  16. * @author Diego Oliveira <contato@diegoholiveira.com>
  17. * @author Henrique Moody <henriquemoody@gmail.com>
  18. */
  19. final class Imei extends AbstractRule
  20. {
  21. private const IMEI_SIZE = 15;
  22. /**
  23. * @see https://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity
  24. *
  25. * {@inheritDoc}
  26. */
  27. public function validate($input): bool
  28. {
  29. if (!is_scalar($input)) {
  30. return false;
  31. }
  32. $numbers = (string) preg_replace('/\D/', '', (string) $input);
  33. if (mb_strlen($numbers) != self::IMEI_SIZE) {
  34. return false;
  35. }
  36. return (new Luhn())->validate($numbers);
  37. }
  38. }