Create "Imei" rule

This commit is contained in:
Diego Oliveira 2015-10-22 23:45:56 -02:00 committed by Henrique Moody
parent 84fa389d4c
commit 70997912eb
10 changed files with 199 additions and 0 deletions

View file

@ -18,6 +18,7 @@ All notable changes of the Respect\Validation releases are documented in this fi
- Create "Finite" rule (#397)
- Create "FloatType" rule (#565)
- Create "Identical" rule (#442)
- Create "Imei" rule (#590)
- Create "Infinite" rule (#397)
- Create "IntType" rule (#451)
- Create "Iterable" rule (#570)

20
docs/Imei.md Normal file
View file

@ -0,0 +1,20 @@
# Imei
- `v::imei()`
Validates is the input is a valid [IMEI][].
```php
v::imei()->validate('35-209900-176148-1'); // true
v::imei()->validate('490154203237518'); // true
```
***
See also:
* [Bsn](Bsn.md)
* [Cnh](Cnh.md)
* [Cnpj](Cnpj.md)
* [Cpf](Cpf.md)
[IMEI]: https://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity "International Mobile Station Equipment Identity"

View file

@ -172,6 +172,7 @@
* [Domain](Domain.md)
* [Email](Email.md)
* [HexRgbColor](HexRgbColor.md)
* [Imei](Imei.md)
* [Ip](Ip.md)
* [Json](Json.md)
* [MacAddress](MacAddress.md)
@ -240,6 +241,7 @@
* [FloatType](FloatType.md)
* [Graph](Graph.md)
* [HexRgbColor](HexRgbColor.md)
* [Imei](Imei.md)
* [In](In.md)
* [Infinite](Infinite.md)
* [Instance](Instance.md)

View file

@ -0,0 +1,24 @@
<?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\Exceptions;
class ImeiException extends ValidationException
{
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a valid IMEI',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be a valid IMEI',
],
];
}

44
library/Rules/Imei.php Normal file
View file

@ -0,0 +1,44 @@
<?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 (strlen($numbers) != self::IMEI_SIZE) {
return false;
}
$sum = 0;
for ($position = 0; $position < (self::IMEI_SIZE - 1); ++$position) {
$number = $numbers[$position] * (($position % 2) + 1);
$sum += ($number % 10) + intval($number / 10);
}
return ((ceil($sum / 10) * 10) - $sum == $numbers[14]);
}
}

View file

@ -69,6 +69,7 @@ use Respect\Validation\Rules\Key;
* @method static Validator floatType()
* @method static Validator graph(string $additionalChars = null)
* @method static Validator hexRgbColor()
* @method static Validator imei()
* @method static Validator in(mixed $haystack, bool $compareIdentical = false)
* @method static Validator infinite()
* @method static Validator instance(string $instanceName)

View file

@ -0,0 +1,10 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
v::imei()->check('490154203237518');
v::imei()->assert('356938035643809');
?>
--EXPECTF--

View file

@ -0,0 +1,23 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Exceptions\ImeiException;
use Respect\Validation\Validator as v;
try {
v::imei()->check('497511659092062');
} catch (ImeiException $e) {
echo $e->getMainMessage().PHP_EOL;
}
try {
v::imei()->assert([]);
} catch (AllOfException $e) {
echo $e->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"497511659092062" must be a valid IMEI
- { } must be a valid IMEI

View file

@ -0,0 +1,23 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Exceptions\ImeiException;
use Respect\Validation\Validator as v;
try {
v::not(v::imei())->check('35-007752-323751-3');
} catch (ImeiException $e) {
echo $e->getMainMessage().PHP_EOL;
}
try {
v::not(v::imei())->assert('350077523237513');
} catch (AllOfException $e) {
echo $e->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"35-007752-323751-3" must not be a valid IMEI
- "350077523237513" must not be a valid IMEI

View file

@ -0,0 +1,51 @@
<?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;
/**
* @group rule
* @covers Respect\Validation\Rules\Imei
*/
class ImeiTest extends RuleTestCase
{
public function providerForValidInput()
{
$rule = new Imei();
return [
[$rule, '35-007752-323751-3'],
[$rule, '35-209900-176148-1'],
[$rule, '350077523237513'],
[$rule, '356938035643809'],
[$rule, '490154203237518'],
[$rule, 350077523237513],
[$rule, 356938035643809],
[$rule, 490154203237518],
];
}
public function providerForInvalidInput()
{
$rule = new Imei();
return [
[$rule, ''],
[$rule, null],
[$rule, 1.0],
[$rule, new \stdClass()],
[$rule, '490154203237512'],
[$rule, '4901542032375125'],
[$rule, 'Whateveeeeeerrr'],
[$rule, true],
];
}
}