respect-validation/library/Rules/Imei.php
2017-10-16 20:38:56 +02:00

39 lines
835 B
PHP

<?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.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation\Rules;
class Imei extends AbstractRule
{
const IMEI_SIZE = 15;
/**
* @see https://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity
*
* @param string $input
*
* @return bool
*/
public function validate($input)
{
if (!is_scalar($input)) {
return false;
}
$numbers = preg_replace('/\D/', '', $input);
if (mb_strlen($numbers) != self::IMEI_SIZE) {
return false;
}
return (new Luhn())->validate($numbers);
}
}