-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/faker #2
base: master
Are you sure you want to change the base?
Changes from all commits
3e43412
8e0ee31
17d4a8b
f3b199b
7d8412b
244c3d3
f6f5dbe
c33d532
1da83a0
03a45cf
180a005
0ca6414
f4cfedf
55c738c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cnamts\Nir\Faker; | ||
|
||
use Faker\Provider\fr_FR\Address; | ||
use Faker\Provider\fr_FR\Person; | ||
|
||
class AssureProvider extends Person | ||
{ | ||
/* @phpstan-ignore-next-line */ | ||
public function nir($gender = null, $formatted = false, \DateTime $birthday = null, string $departmentCode = null): string | ||
{ | ||
$departementPart = $this->getDepartmentPart($departmentCode); | ||
$nir = $this->getGenderPart($gender) | ||
. $this->getDateBirthPart($birthday) | ||
. $departementPart | ||
. $this->numerify('###'); | ||
|
||
$nir .= $this->getKeyPart($nir, $departementPart); | ||
// Format is x xx xx xx xxx xxx xx | ||
if ($formatted) { | ||
$nir = mb_substr($nir, 0, 1) . ' ' . mb_substr($nir, 1, 2) . ' ' . mb_substr($nir, 3, 2) . ' ' . mb_substr($nir, 5, 2) . ' ' . mb_substr($nir, 7, 3) . ' ' . mb_substr($nir, 10, 3) . ' ' . mb_substr($nir, 13, 2); | ||
} | ||
|
||
return $nir; | ||
} | ||
|
||
/* @phpstan-ignore-next-line */ | ||
private function getGenderPart($gender): int | ||
{ | ||
// Gender | ||
if ($gender === Person::GENDER_MALE) { | ||
return 1; | ||
} | ||
if ($gender === Person::GENDER_FEMALE) { | ||
return 2; | ||
} | ||
|
||
return $this->numberBetween(1, 2); | ||
} | ||
|
||
private function getDateBirthPart(?\DateTime $birthday): string | ||
{ | ||
if ($birthday === null) { | ||
return | ||
// Year of birth (aa) | ||
$this->numerify('##') . | ||
// Month of birth (mm) | ||
sprintf('%02d', $this->numberBetween(1, 12)); | ||
} | ||
|
||
return $birthday->format('ym'); | ||
} | ||
|
||
private function getDepartmentPart(?string $departmentCode): string | ||
{ | ||
// Department | ||
$department = $departmentCode ?? (string) key(Address::department()); | ||
|
||
// Town number, depends on department length | ||
if (mb_strlen($department) === 2) { | ||
$department .= $this->numerify('###'); | ||
} elseif (mb_strlen($department) === 3) { | ||
$department .= $this->numerify('##'); | ||
} | ||
|
||
return $department; | ||
} | ||
|
||
private function getKeyPart(string $nir, string $department): string | ||
{ | ||
/** | ||
* 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 | ||
*/ | ||
$nirInteger = $nir; | ||
switch ($department) { | ||
case '2A': | ||
$nirInteger = str_replace('2A', '19', $nir); | ||
break; | ||
case '2B': | ||
$nirInteger = str_replace('2B', '18', $nir); | ||
break; | ||
} | ||
|
||
return sprintf('%02d', 97 - (int) $nirInteger % 97); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cnamts\Nir\Faker; | ||
|
||
use Faker\Provider\Base; | ||
use Faker\Provider\fr_FR\Person; | ||
|
||
class NnpProvider extends Base | ||
{ | ||
public function nnp(string $gender = null): string | ||
{ | ||
return $this->getGenderPart($gender) | ||
// le second chiffre (2ème composant) est la vérification du type de création (1 pour création et 2 pour numéros fictifs générés par le système RFI à ne pas prendre en compte) | ||
. $this->numberBetween(1, 2) | ||
// le 3ème composant est un groupe de trois chiffres correspondant au numéro de caisse | ||
. $this->numerify('###') | ||
// le 4ème composant correspond au numéro d'ordre par caisse | ||
. $this->numerify('########'); | ||
} | ||
|
||
/* @phpstan-ignore-next-line */ | ||
private function getGenderPart($gender): int | ||
{ | ||
// Gender | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Du coup même remarque sur le fait de stocker chaque valeur dans une variable et concaténer à la fin There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. donc tu preferes que je stocke les parts dans des var ? car la je renvois direct la concatenation |
||
if ($gender === Person::GENDER_MALE) { | ||
return 7; | ||
} | ||
if ($gender === Person::GENDER_FEMALE) { | ||
return 8; | ||
} | ||
|
||
return $this->numberBetween(7, 8); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spec\Cnamts\Nir\Faker; | ||
|
||
use Cnamts\Nir\Constraints\Nir; | ||
use Cnamts\Nir\Constraints\NirValidator; | ||
use Faker\Factory; | ||
use Faker\Generator; | ||
use Faker\Provider\Person; | ||
use PhpSpec\ObjectBehavior; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; | ||
|
||
class AssureProviderSpec extends ObjectBehavior | ||
{ | ||
/** @var Generator */ | ||
private $faker; | ||
|
||
public function let(Generator $generator) | ||
{ | ||
$this->faker = Factory::create('fr_FR'); | ||
$this->beConstructedWith($generator); | ||
} | ||
|
||
public function it_generates_valid_nir( | ||
ExecutionContextInterface $context, | ||
ConstraintViolationBuilderInterface $constraintViolationBuilder | ||
) { | ||
$constraintViolationBuilder->addViolation()->shouldNotBeCalled(); | ||
$this->validateNir($this->nir(), $context); | ||
} | ||
|
||
public function it_generates_different_nir_on_call() | ||
{ | ||
$nir = $this->nir(); | ||
$nirNext = $this->nir(); | ||
$nir->shouldNotEqual($nirNext); | ||
} | ||
|
||
public function it_generates_nir_from_params( | ||
ExecutionContextInterface $context, | ||
ConstraintViolationBuilderInterface $constraintViolationBuilder | ||
) { | ||
$gender = $this->faker->randomElement([Person::GENDER_MALE, Person::GENDER_FEMALE]); | ||
$dateNaissance = $this->faker->dateTime(); | ||
$departement = $this->faker->departmentNumber(); | ||
$nir = $this->nir($gender, false, $dateNaissance, $departement); | ||
$constraintViolationBuilder->addViolation()->shouldNotBeCalled(); | ||
$this->validateNir($nir, $context); | ||
|
||
$nir->shouldbeNir($dateNaissance, $departement); | ||
} | ||
|
||
/** Valide un NIR généré à partir du validateur du package */ | ||
private function validateNir($nir, $context) | ||
{ | ||
$constraint = new Nir(); | ||
$nirValidator = new NirValidator(); | ||
$nirValidator->initialize($context->getWrappedObject()); | ||
$nirValidator->validate($nir->getWrappedObject(), $constraint); | ||
} | ||
|
||
public function getMatchers(): array | ||
{ | ||
return [ | ||
'beNir' => function (string $value, \DateTime $dateNaissance, string $departement) { | ||
$isNirRegex = preg_match(NirValidator::NIR_REGEX, $value, $matches); | ||
$isSameYear = $matches['anneeNaissance'] === $dateNaissance->format('y'); | ||
$isSameMonth = $matches['moisNaissance'] === $dateNaissance->format('m'); | ||
$isSameDepartment = mb_strpos($departement, $matches['departementNaissance']) === 0; | ||
|
||
return $isNirRegex && $isSameYear && $isSameMonth && $isSameDepartment; | ||
}, | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spec\Cnamts\Nir\Faker; | ||
|
||
use Cnamts\Nir\Constraints\Nnp; | ||
use Cnamts\Nir\Constraints\NnpValidator; | ||
use Faker\Generator; | ||
use PhpSpec\ObjectBehavior; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; | ||
|
||
class NnpProviderSpec extends ObjectBehavior | ||
{ | ||
public function let(Generator $generator) | ||
{ | ||
$this->beConstructedWith($generator); | ||
} | ||
|
||
public function it_generates_valid_nnp( | ||
ExecutionContextInterface $context, | ||
ConstraintViolationBuilderInterface $constraintViolationBuilder | ||
) { | ||
$constraintViolationBuilder->addViolation()->shouldNotBeCalled(); | ||
$this->validateNnp($this->nnp(), $context); | ||
} | ||
|
||
public function it_generates_different_nir_on_call() | ||
{ | ||
$nnp = $this->nnp(); | ||
$nnpNext = $this->nnp(); | ||
$nnp->shouldNotEqual($nnpNext); | ||
} | ||
|
||
/** Valide un NNP généré à partir du validateur du package */ | ||
private function validateNnp($nnp, $context) | ||
{ | ||
$constraint = new Nnp(); | ||
$nnpValidator = new NnpValidator(); | ||
$nnpValidator->initialize($context->getWrappedObject()); | ||
$nnpValidator->validate($nnp->getWrappedObject(), $constraint); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pourquoi ne pas typer gender et formatted ?