respect-validation/library/Rules/Imei.php

51 lines
1.2 KiB
PHP
Raw Normal View History

2015-10-23 03:45:56 +02:00
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
2015-10-23 03:45:56 +02:00
*/
declare(strict_types=1);
2015-10-23 03:45:56 +02:00
namespace Respect\Validation\Rules;
use function is_scalar;
use function mb_strlen;
use function preg_replace;
/**
* Validates is the input is a valid IMEI.
*
* @author Alexander Gorshkov <mazanax@yandex.ru>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Diego Oliveira <contato@diegoholiveira.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class Imei extends AbstractRule
2015-10-23 03:45:56 +02:00
{
private const IMEI_SIZE = 15;
2015-10-23 03:45:56 +02:00
/**
* @see https://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity
*
* {@inheritDoc}
2015-10-23 03:45:56 +02:00
*/
public function validate($input): bool
2015-10-23 03:45:56 +02:00
{
if (!is_scalar($input)) {
return false;
}
$numbers = (string) preg_replace('/\D/', '', (string) $input);
if (mb_strlen($numbers) != self::IMEI_SIZE) {
2015-10-23 03:45:56 +02:00
return false;
}
2017-08-18 13:47:36 +02:00
return (new Luhn())->validate($numbers);
2015-10-23 03:45:56 +02:00
}
}