From f2bed15a4ffc6dc8186b1f577aa0ce74d72b781e Mon Sep 17 00:00:00 2001 From: Thomas Bordinat Date: Mon, 22 Aug 2016 14:53:09 +0200 Subject: [PATCH 1/3] New function : nir() to generate NIR number This commit allow us to generate a NIR number for a french person --- src/Faker/Provider/fr_FR/Person.php | 59 +++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/Faker/Provider/fr_FR/Person.php b/src/Faker/Provider/fr_FR/Person.php index 1907f5f004..e50e8901fb 100644 --- a/src/Faker/Provider/fr_FR/Person.php +++ b/src/Faker/Provider/fr_FR/Person.php @@ -67,4 +67,63 @@ public static function prefix() { return static::randomElement(static::$prefix); } + + /** + * Generates a NIR number (13 digits + 2 digits for the key) + * + * @see https://fr.wikipedia.org/wiki/Num%C3%A9ro_de_s%C3%A9curit%C3%A9_sociale_en_France + * @return string + */ + public function nir($gender = null, $formatted = false) + { + // Gender + if ($gender === static::GENDER_MALE) { + $nir = 1; + } elseif ($gender === static::GENDER_FEMALE) { + $nir = 2; + } else { + $nir = $this->numberBetween(1, 2); + } + + $nir .= + // Year of birth (aa) + $this->numerify('##') . + // Mont of birth (mm) + sprintf('%02d', $this->numberBetween(1, 12)); + + // Department + $department = key(Address::department()); + $nir .= $department; + + // Town number, depends on department length + if (strlen($department) === 2) { + $nir .= $this->numerify('###'); + } elseif (strlen($department) === 3) { + $nir .= $this->numerify('##'); + } + + // Born number (dependending of town and month of birth) + $nir .= $this->numerify('###'); + + /** + * The key for a given NIR is `97 - 97 % NIR` + * NIR has to be an integer, so we have to do a little replacment + * for departments 2A and 2B + */ + if ($department === '2A') { + $nirInteger = str_replace('2A', '19', $nir); + } elseif ($department === '2B') { + $nirInteger = str_replace('2B', '18', $nir); + } else { + $nirInteger = $nir; + } + $nir .= sprintf('%02d', 97 - $nirInteger % 97); + + // Format is x xx xx xx xxx xxx xx + if ($formatted) { + $nir = substr($nir, 0, 1) . ' ' . substr($nir, 1, 2) . ' ' . substr($nir, 3, 2) . ' ' . substr($nir, 5, 2) . ' ' . substr($nir, 7, 3). ' ' . substr($nir, 10, 3). ' ' . substr($nir, 13, 2); + } + + return $nir; + } } From d12e61fcef5224811ed46ccc065ffa50077b5dc2 Mon Sep 17 00:00:00 2001 From: Thomas Bordinat Date: Mon, 22 Aug 2016 14:55:23 +0200 Subject: [PATCH 2/3] README.md - Generates a random NIR number --- readme.md | 9 +++++++++ src/Faker/Provider/fr_FR/Person.php | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index ece727bee5..227729605f 100644 --- a/readme.md +++ b/readme.md @@ -991,6 +991,15 @@ echo $faker->siren; // 082 250 104 echo $faker->siret; // 347 355 708 00224 ``` +### `Faker\Provider\fr_FR\Person` + +```php +nir; // 1 88 07 35 127 571 - 19 +``` + ### `Faker\Provider\he_IL\Payment` ```php diff --git a/src/Faker/Provider/fr_FR/Person.php b/src/Faker/Provider/fr_FR/Person.php index e50e8901fb..5778a7c02f 100644 --- a/src/Faker/Provider/fr_FR/Person.php +++ b/src/Faker/Provider/fr_FR/Person.php @@ -69,7 +69,7 @@ public static function prefix() } /** - * Generates a NIR number (13 digits + 2 digits for the key) + * Generates a NIR / Sécurité Sociale number (13 digits + 2 digits for the key) * * @see https://fr.wikipedia.org/wiki/Num%C3%A9ro_de_s%C3%A9curit%C3%A9_sociale_en_France * @return string @@ -102,7 +102,7 @@ public function nir($gender = null, $formatted = false) $nir .= $this->numerify('##'); } - // Born number (dependending of town and month of birth) + // Born number (depending of town and month of birth) $nir .= $this->numerify('###'); /** From 2dcd85a6c5f0cd2ef4385e2b5ae581274f7b97b9 Mon Sep 17 00:00:00 2001 From: Thomas Bordinat Date: Sat, 17 Dec 2016 11:33:07 +0100 Subject: [PATCH 3/3] Run some tests on the NIR generator --- test/Faker/Provider/fr_FR/PersonTest.php | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test/Faker/Provider/fr_FR/PersonTest.php diff --git a/test/Faker/Provider/fr_FR/PersonTest.php b/test/Faker/Provider/fr_FR/PersonTest.php new file mode 100644 index 0000000000..b5bc805623 --- /dev/null +++ b/test/Faker/Provider/fr_FR/PersonTest.php @@ -0,0 +1,36 @@ +addProvider(new Person($faker)); + $this->faker = $faker; + } + + public function testNIRReturnsTheRightGender() + { + $nir = $this->faker->nir(\Faker\Provider\Person::GENDER_MALE); + $this->assertStringStartsWith('1', $nir); + } + + public function testNIRReturnsTheRightPattern() + { + $nir = $this->faker->nir; + $this->assertRegExp("/^[12]\d{5}[0-9A-B]\d{8}$/", $nir); + } + + public function testNIRFormattedReturnsTheRightPattern() + { + $nir = $this->faker->nir(null, true); + $this->assertRegExp("/^[12]\s\d{2}\s\d{2}\s\d{1}[0-9A-B]\s\d{3}\s\d{3}\s\d{2}$/", $nir); + } +}