mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 09:40:05 +02:00
Create "Pesel" rule
Create validator for PESEL - Polish Human Identification Number.
This commit is contained in:
parent
c828420438
commit
b8da3a947b
12
docs/Pesel.md
Normal file
12
docs/Pesel.md
Normal 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
|
||||
```
|
|
@ -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)
|
||||
|
|
27
library/Exceptions/PeselException.php
Normal file
27
library/Exceptions/PeselException.php
Normal 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
35
library/Rules/Pesel.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
|
11
tests/integration/rules/pesel_1.phpt
Normal file
11
tests/integration/rules/pesel_1.phpt
Normal 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--;
|
17
tests/integration/rules/pesel_2.phpt
Normal file
17
tests/integration/rules/pesel_2.phpt
Normal 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
|
16
tests/integration/rules/pesel_3.phpt
Normal file
16
tests/integration/rules/pesel_3.phpt
Normal 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
|
17
tests/integration/rules/pesel_4.phpt
Normal file
17
tests/integration/rules/pesel_4.phpt
Normal 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
|
17
tests/integration/rules/pesel_5.phpt
Normal file
17
tests/integration/rules/pesel_5.phpt
Normal 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
|
66
tests/unit/Rules/PeselTest.php
Normal file
66
tests/unit/Rules/PeselTest.php
Normal 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'],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue