Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

adds rijksregisternummer #1361

Merged
merged 6 commits into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,15 @@ echo $faker->vat; // "BE 0123456789" - Belgian Value Added Tax number
echo $faker->vat(false); // "BE0123456789" - unspaced Belgian Value Added Tax number
```

### `Faker\Provider\nl_BE\Person`

```php
<?php

echo $faker->rrn(); // "83051711784" - Belgian Rijksregisternummer
echo $faker->rrn('female'); // "50032089858" - Belgian Rijksregisternummer for a female
```

### `Faker\Provider\nl_NL\Company`

```php
Expand Down
34 changes: 34 additions & 0 deletions src/Faker/Provider/nl_BE/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Faker\Provider\nl_BE;

use Faker\Provider\DateTime;

class Person extends \Faker\Provider\Person
{
protected static $firstNameMale = array(
Expand Down Expand Up @@ -69,4 +71,36 @@ class Person extends \Faker\Provider\Person
'Vermeersch', 'Vermeiren', 'Vermeulen', 'Verschueren', 'Verstraete', 'Verstraeten',
'Vervoort', 'Wauters', 'Willems', 'Wouters', 'Wuyts', 'Yildirim', 'Yilmaz'
);

/**
* Belgian Rijksregister numbers are used to identify each citizen,
* it consists of three parts, the person's day of birth, in the
* format 'ymd', followed by a number between 1 and 997, odd for
* males, even for females. The last part is used to check if it's
* a valid number.
*
* @link https://nl.wikipedia.org/wiki/Rijksregisternummer
*
* @param string|null $gender 'male', 'female' or null for any
* @return string
*/
public static function rrn($gender = null)
{
$middle = self::numberBetween(1, 997);
if ($gender === static::GENDER_MALE) {
$middle = $middle %2 === 1 ? $middle : $middle+1;
} elseif ($gender === static::GENDER_FEMALE) {
$middle = $middle %2 === 0 ? $middle : $middle+1;
}
$middle = sprintf('%03d', $middle);

$date = DateTime::dateTimeThisCentury();
$dob = sprintf('%06d', $date->format('ymd'));
$help = $date->format('Y') >= 2000 ? 2 : null;

$check = intval($help.$dob.$middle);
$rest = sprintf('%02d', 97 - ($check % 97));

return $dob.$middle.$rest;
}
}
54 changes: 54 additions & 0 deletions test/Faker/Provider/nl_BE/PersonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Faker\Test\Provider\nl_BE;

use Faker\Generator;
use Faker\Provider\nl_BE\Person;
use PHPUnit\Framework\TestCase;
use Datetime;

/**
* @group Person
*/
class PersonTest extends TestCase
{
/**
* @var Generator
*/
private $faker;

public function setUp()
{
$faker = new Generator();
$faker->addProvider(new Person($faker));
$this->faker = $faker;
}

public function testRrnIsValid()
{
$rrn = $this->faker->rrn();

$this->assertEquals(11, strlen($rrn));

$ctrlNumber = substr($rrn, 9, 2);
$calcCtrl = 97 - (substr($rrn, 0, 9) % 97);
$altcalcCtrl = 97 - ((2 . substr($rrn, 0, 9)) % 97);
$this->assertTrue(in_array($ctrlNumber, array($calcCtrl, $altcalcCtrl)));

$middle = substr($rrn, 6, 3);
$this->assertGreaterThan(1, $middle);
$this->assertLessThan(997, $middle);
}

public function testRrnIsMale()
{
$rrn = $this->faker->rrn('male');
$this->assertEquals(substr($rrn, 6, 3) % 2, 1);
}

public function testRrnIsFemale()
{
$rrn = $this->faker->rrn('female');
$this->assertEquals(substr($rrn, 6, 3) % 2, 0);
}
}