mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 14:52:17 +02:00
Create "Bsn" rule
This commit is contained in:
parent
d53811f7b7
commit
8cab57052e
|
@ -7,6 +7,7 @@ All notable changes of the Respect\Validation releases are documented in this fi
|
|||
### Added
|
||||
|
||||
- Add support for PHP 7 (#426)
|
||||
- Create "Bsn" rule (#450)
|
||||
- Create "CallableType" rule (#397)
|
||||
- Create "Extension" rule (#360)
|
||||
- Create "Factor" rule (#405)
|
||||
|
|
9
docs/Bsn.md
Normal file
9
docs/Bsn.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# Bsn
|
||||
|
||||
- `v::bsn()`
|
||||
|
||||
Validates a Dutch citizen service number ([BSN](https://nl.wikipedia.org/wiki/Burgerservicenummer)).
|
||||
|
||||
```php
|
||||
v::bsn()->validate('612890053'); //true
|
||||
```
|
|
@ -155,6 +155,7 @@
|
|||
|
||||
## Other
|
||||
|
||||
* [Bsn](Bsn.md)
|
||||
* [Cnh](Cnh.md)
|
||||
* [Cnpj](Cnpj.md)
|
||||
* [Cpf](Cpf.md)
|
||||
|
@ -193,6 +194,7 @@
|
|||
* [Between](Between.md)
|
||||
* [Bic](Bic.md)
|
||||
* [BoolType](BoolType.md)
|
||||
* [Bsn](Bsn.md)
|
||||
* [Call](Call.md)
|
||||
* [CallableType](CallableType.md)
|
||||
* [Callback](Callback.md)
|
||||
|
|
30
library/Exceptions/BsnException.php
Normal file
30
library/Exceptions/BsnException.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* @author Ronald Drenth <ronalddrenth@gmail.com>
|
||||
*/
|
||||
class BsnException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be a BSN',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be a BSN',
|
||||
),
|
||||
);
|
||||
}
|
42
library/Rules/Bsn.php
Normal file
42
library/Rules/Bsn.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Validates a Dutch citizen service number (BSN)
|
||||
*
|
||||
* @author Ronald Drenth <ronalddrenth@gmail.com>
|
||||
* @see https://nl.wikipedia.org/wiki/Burgerservicenummer
|
||||
*/
|
||||
class Bsn extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input)
|
||||
{
|
||||
if (!ctype_digit($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strlen($input) !== 9) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$sum = -1 * $input[8];
|
||||
for ($i = 9; $i > 1; $i--) {
|
||||
$sum += $i * $input[9 - $i];
|
||||
}
|
||||
|
||||
return $sum !== 0 && $sum % 11 === 0;
|
||||
}
|
||||
}
|
|
@ -32,6 +32,7 @@ use Respect\Validation\Rules\Key;
|
|||
* @method static Validator between(mixed $min = null, mixed $max = null, bool $inclusive = true)
|
||||
* @method static Validator bic(string $countryCode)
|
||||
* @method static Validator boolType()
|
||||
* @method static Validator bsn()
|
||||
* @method static Validator call()
|
||||
* @method static Validator callableType()
|
||||
* @method static Validator callback(mixed $callback)
|
||||
|
|
95
tests/unit/Rules/BsnTest.php
Normal file
95
tests/unit/Rules/BsnTest.php
Normal file
|
@ -0,0 +1,95 @@
|
|||
<?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;
|
||||
|
||||
use PHPUnit_Framework_TestCase;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers Respect\Validation\Rules\Bsn
|
||||
* @covers Respect\Validation\Exceptions\BsnException
|
||||
*/
|
||||
class BsnTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var Bsn
|
||||
*/
|
||||
private $rule;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->rule = new Bsn();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForBsn
|
||||
*
|
||||
* @param string $input
|
||||
*/
|
||||
public function testShouldValidateBsn($input)
|
||||
{
|
||||
$this->assertTrue($this->rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidBsn
|
||||
* @expectedException \Respect\Validation\Exceptions\BsnException
|
||||
*
|
||||
* @param string $input
|
||||
*/
|
||||
public function testShouldNotValidateBsn($input)
|
||||
{
|
||||
$this->assertFalse($this->rule->validate($input));
|
||||
$this->assertFalse($this->rule->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function providerForBsn()
|
||||
{
|
||||
return array(
|
||||
array('612890053'),
|
||||
array('087880532'),
|
||||
array('386625918'),
|
||||
array('601608021'),
|
||||
array('254650703'),
|
||||
array('478063441'),
|
||||
array('478063441'),
|
||||
array('187368429'),
|
||||
array('541777348'),
|
||||
array('254283883'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function providerForInvalidBsn()
|
||||
{
|
||||
return array(
|
||||
array('1234567890'),
|
||||
array('0987654321'),
|
||||
array('13579024'),
|
||||
array('612890054'),
|
||||
array('854650703'),
|
||||
array('283958721'),
|
||||
array('231859081'),
|
||||
array('189023323'),
|
||||
array('238150912'),
|
||||
array('382409678'),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue