Create "Pesel" rule

Create validator for PESEL - Polish Human Identification Number.
This commit is contained in:
Tomasz Regdos 2015-11-26 20:46:51 +01:00 committed by Henrique Moody
parent c828420438
commit b8da3a947b
11 changed files with 221 additions and 0 deletions

12
docs/Pesel.md Normal file
View file

@ -0,0 +1,12 @@
# Pesel
- `v::pesel()`
Validates PESEL (Polish human identification number).
```php
v::pesel()->validate('21120209256'); // true
v::pesel()->validate('97072704800'); // true
v::pesel()->validate('97072704801'); // false
v::pesel()->validate('PESEL123456'); // false
```

View file

@ -181,6 +181,7 @@
* [NfeAccessKey](NfeAccessKey.md)
* [NotBlank](NotBlank.md)
* [NotOptional](NotOptional.md)
* [Pesel](Pesel.md)
* [Phone](Phone.md)
* [Sf](Sf.md)
* [Url](Url.md)
@ -284,6 +285,7 @@
* [OneOf](OneOf.md)
* [Optional](Optional.md)
* [PerfectSquare](PerfectSquare.md)
* [Pesel](Pesel.md)
* [Phone](Phone.md)
* [Positive](Positive.md)
* [PostalCode](PostalCode.md)

View file

@ -0,0 +1,27 @@
<?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 PeselException extends ValidationException
{
/**
* @var array
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a valid PESEL',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be a valid PESEL',
],
];
}

35
library/Rules/Pesel.php Normal file
View file

@ -0,0 +1,35 @@
<?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 Pesel extends AbstractRule
{
public function validate($pesel)
{
$weights = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3];
if (!is_numeric($pesel) || strlen($pesel) != 11) {
return false;
}
$targetControlNumber = $pesel[10];
$calculateControlNumber = 0;
for ($i = 0; $i < 10; $i++) {
$calculateControlNumber += $pesel[$i] * $weights[$i];
}
$calculateControlNumber = (10 - $calculateControlNumber % 10) % 10;
return $targetControlNumber == $calculateControlNumber;
}
}

View file

@ -110,6 +110,7 @@ use Respect\Validation\Rules\Key;
* @method static Validator oneOf()
* @method static Validator optional(Validatable $rule)
* @method static Validator perfectSquare()
* @method static Validator pesel()
* @method static Validator phone()
* @method static Validator positive()
* @method static Validator postalCode(string $countryCode)

View file

@ -0,0 +1,11 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Validator as v;
v::pesel()->check('21120209256');
v::pesel()->assert('21120209256');
--EXPECTF--;

View file

@ -0,0 +1,17 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\PeselException;
try
{
v::pesel()->check('21120209251');
} catch (PeselException $e) {
echo $e->getMainMessage();
}
--EXPECTF--;
"21120209251" must be a valid PESEL

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\AllOfException;
try
{
v::pesel()->assert('21120209251');
} catch (AllOfException $e) {
echo $e->getFullMessage();
}
--EXPECTF--
- "21120209251" must be a valid PESEL

View file

@ -0,0 +1,17 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\PeselException;
try
{
v::not(v::pesel())->check('21120209256');
} catch (PeselException $e) {
echo $e->getMainMessage();
}
--EXPECTF--
"21120209256" must not be a valid PESEL

View file

@ -0,0 +1,17 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\AllOfException;
try
{
v::not(v::pesel())->assert('21120209256');
} catch (AllOfException $e) {
echo $e->getFullMessage();
}
--EXPECTF--
- "21120209256" must not be a valid PESEL

View file

@ -0,0 +1,66 @@
<?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\Pesel
* @covers Respect\Validation\Exceptions\PeselException
*/
class PeselTest extends \PHPUnit_Framework_TestCase
{
protected $peselValidator;
protected function setUp()
{
$this->peselValidator = new Pesel();
}
/**
* @dataProvider providerValidPesel
*/
public function testPeselShouldValidate($input)
{
$this->assertTrue($this->peselValidator->validate($input));
}
/**
* @dataProvider providerInvalidPesel
*/
public function testPeselShouldNotValidate($input)
{
$this->assertFalse($this->peselValidator->validate($input));
}
public function providerValidPesel()
{
return [
['49040501580'],
['39012110375'],
['50083014540'],
['69090515504'],
['21120209256']
];
}
public function providerInvalidPesel()
{
return [
['1'],
['22'],
['PESEL'],
['PESEL123456'],
['21120209251'],
['21120209250'],
];
}
}