Apply contribution guidelines to "Luhn" rule

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Danilo Correa 2018-09-18 21:07:24 -03:00 committed by Henrique Moody
parent 478e248dad
commit 1b4a904871
No known key found for this signature in database
GPG key ID: 221E9281655813A6
4 changed files with 83 additions and 14 deletions

View file

@ -13,8 +13,16 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class LuhnException extends ValidationException
/**
* @author Alexander Gorshkov <mazanax@yandex.ru>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class LuhnException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a valid Luhn number',

View file

@ -13,10 +13,19 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use function mb_strlen;
use function mb_substr;
/**
* Validate whether a given input is a Luhn number.
*
* @see https://en.wikipedia.org/wiki/Luhn_algorithm
*
* @author Alexander Gorshkov <mazanax@yandex.ru>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class Luhn extends AbstractRule
final class Luhn extends AbstractRule
{
/**
* {@inheritdoc}
@ -27,10 +36,10 @@ class Luhn extends AbstractRule
return false;
}
return $this->isValid($input);
return $this->isValid((string) $input);
}
private function isValid($input): bool
private function isValid(string $input): bool
{
$sum = 0;
$numDigits = mb_strlen($input);

View file

@ -0,0 +1,37 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\LuhnException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::luhn()->check('2222400041240021');
} catch (LuhnException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::luhn())->check('2223000048400011');
} catch (LuhnException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::luhn()->assert('340316193809334');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::luhn())->assert('6011000990139424');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"2222400041240021" must be a valid Luhn number
"2223000048400011" must not be a valid Luhn number
- "340316193809334" must be a valid Luhn number
- "6011000990139424" must not be a valid Luhn number

View file

@ -14,34 +14,49 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\RuleTestCase;
use stdClass;
/**
* @group rule
* @group rule
*
* @covers \Respect\Validation\Rules\Luhn
*
* @author Alexander Gorshkov <mazanax@yandex.ru>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class LuhnTest extends RuleTestCase
final class LuhnTest extends RuleTestCase
{
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
$rule = new Luhn();
return [
[$rule, '2222400041240011'],
[$rule, '340316193809364'],
[$rule, '6011000990139424'],
[$rule, '2223000048400011'],
'17 digits string' => [$rule, '2222400041240011'],
'16 digits string' => [$rule, '340316193809364'],
'integer' => [$rule, 6011000990139424],
];
}
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
$rule = new Luhn();
return [
[$rule, '2222400041240021'],
[$rule, '340316193809334'],
[$rule, '6011000990139421'],
[$rule, '2223000048400010'],
'invalid string' => [$rule, '2222400041240021'],
'invalid integer' => [$rule, 340316193809334],
'float' => [$rule, 222240004124001.1],
'boolean true' => [$rule, true],
'boolean false' => [$rule, false],
'empty' => [$rule, ''],
'object' => [$rule, new stdClass()],
'array' => [$rule, [2222400041240011]],
];
}
}