Apply contribution guidelines to "Imei" rule

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Danilo Benevides 2018-08-01 21:34:21 -03:00 committed by Henrique Moody
parent afab4eb2b4
commit f01972e208
No known key found for this signature in database
GPG key ID: 221E9281655813A6
7 changed files with 75 additions and 64 deletions

View file

@ -13,8 +13,16 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class ImeiException extends ValidationException
/**
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Diego Oliveira <contato@diegoholiveira.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class ImeiException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a valid IMEI',

View file

@ -13,16 +13,26 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
class Imei extends AbstractRule
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
{
public const IMEI_SIZE = 15;
private const IMEI_SIZE = 15;
/**
* @see https://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity
*
* @param string $input
*
* @return bool
* {@inheritdoc}
*/
public function validate($input): bool
{

View file

@ -0,0 +1,38 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\ImeiException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::imei()->check('490154203237512');
} catch (ImeiException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::imei())->check('350077523237513');
} catch (ImeiException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::imei()->assert(null);
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::imei())->assert('356938035643809');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"490154203237512" must be a valid IMEI
"350077523237513" must not be a valid IMEI
- `NULL` must be a valid IMEI
- "356938035643809" must not be a valid IMEI

View file

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

View file

@ -1,23 +0,0 @@
--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->getMessage().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

@ -1,23 +0,0 @@
--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->getMessage().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

@ -16,11 +16,19 @@ namespace Respect\Validation\Rules;
use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
* @group rule
*
* @covers \Respect\Validation\Rules\Imei
*
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Diego Oliveira <contato@diegoholiveira.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class ImeiTest extends RuleTestCase
final class ImeiTest extends RuleTestCase
{
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
$rule = new Imei();
@ -37,6 +45,9 @@ class ImeiTest extends RuleTestCase
];
}
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
$rule = new Imei();