2015-10-23 03:45:56 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
2019-05-23 16:21:34 +02:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2015-10-23 03:45:56 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2018-08-02 02:34:21 +02:00
|
|
|
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
|
|
|
{
|
2018-08-02 02:34:21 +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
|
|
|
|
*
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2015-10-23 03:45:56 +02:00
|
|
|
*/
|
2018-01-28 15:41:31 +01:00
|
|
|
public function validate($input): bool
|
2015-10-23 03:45:56 +02:00
|
|
|
{
|
|
|
|
if (!is_scalar($input)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-22 14:02:39 +02:00
|
|
|
$numbers = (string) preg_replace('/\D/', '', (string) $input);
|
2019-02-09 13:28:08 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|