Skip to content

Commit

Permalink
Don't generate birth number of '000' for Swedish personal identity nu…
Browse files Browse the repository at this point in the history
…mber (#306)

* Don't genereate birth number of '000' for Swedish personal identity number

* Fix Docblock and add default parameter value

* Add tests

* Remove empty line

* Use early returns

* Don't use arrow functions to support PHP versions < 7.4

* Make gender checks type safe

* Added static to closures acording to feedback from PHP CS Fixer
  • Loading branch information
pelmered authored May 24, 2021
1 parent 0485486 commit abde3b0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/Faker/Provider/sv_SE/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,40 @@ public function personalIdentityNumber(\DateTime $birthdate = null, $gender = nu
$birthdate = \Faker\Provider\DateTime::dateTimeThisCentury();
}
$datePart = $birthdate->format('ymd');

if ($gender && $gender == static::GENDER_MALE) {
$randomDigits = (string) static::numerify('##') . static::randomElement([1, 3, 5, 7, 9]);
} elseif ($gender && $gender == static::GENDER_FEMALE) {
$randomDigits = (string) static::numerify('##') . static::randomElement([0, 2, 4, 6, 8]);
} else {
$randomDigits = (string) static::numerify('###');
}
$randomDigits = $this->getBirthNumber($gender);

$checksum = Luhn::computeCheckDigit($datePart . $randomDigits);

return $datePart . '-' . $randomDigits . $checksum;
}

/**
* @param string $gender Person::GENDER_MALE || Person::GENDER_FEMALE
*
* @return string of three digits
*/
protected function getBirthNumber($gender = null)
{
if ($gender && $gender === static::GENDER_MALE) {
return (string) static::numerify('##') . static::randomElement([1, 3, 5, 7, 9]);
}

$zeroCheck = static function ($callback) {
do {
$randomDigits = $callback();
} while ($randomDigits === '000');

return $randomDigits;
};

if ($gender && $gender === static::GENDER_FEMALE) {
return $zeroCheck(static function () {
return (string) static::numerify('##') . static::randomElement([0, 2, 4, 6, 8]);
});
}

return $zeroCheck(static function () {
return (string) static::numerify('###');
});
}
}
18 changes: 18 additions & 0 deletions test/Faker/Provider/sv_SE/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ public function testPersonalIdentityNumberGeneratesEvenValuesForFemales()
self::assertEquals(0, $pin[9] % 2);
}

public function testBirthNumberNot000()
{
$faker = $this->faker;
$faker->seed(97270);
$pin = $this->faker->personalIdentityNumber();

self::assertNotEquals('000', substr($pin, 7, 3));
}

public function testBirthNumberGeneratesEvenValuesForFemales()
{
$faker = $this->faker;
$faker->seed(372920);
$pin = $this->faker->personalIdentityNumber(null, 'female');

self::assertNotEquals('000', substr($pin, 7, 3));
}

protected function getProviders(): iterable
{
yield new Person($this->faker);
Expand Down

0 comments on commit abde3b0

Please sign in to comment.