Skip to content

Commit

Permalink
issue #4 - finalize iban/bic anonymizer
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed May 6, 2024
1 parent ce5b692 commit 2662d55
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/Anonymization/Anonymizer/Core/IbanBicAnonymizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,36 @@
use MakinaCorpus\DbToolsBundle\Helper\Iban;
use MakinaCorpus\DbToolsBundle\Attribute\AsAnonymizer;

/**
* Anonymize an IBAN/BIC couple.
*
* This anonymizer handle:
* - "iban": A valid IBAN,
* - "bic": The associated to IBAN BIC number (bank identifier)
*
* You may specify the following two options:
* - "country": 2-chars country code for the IBAN to use for generation, default
* is to randomly choose a country for each generated IBAN, default is 500.
* - "sample_size": total number of different IBAN that will be generated, the higher
* is this number, the less duplicates you will have in the end.
*/
#[AsAnonymizer(
name: 'iban-bic',
pack: 'core',
description: <<<TXT
Anonymize IBAN and BIC on two columns.
Map columns for each part with options ('iban' and 'bic')
You can specify the 'country' option, which must be a two-letter country
code (default is 'FR').
You can also specify the sample table size using the 'sample_size' option
(default is 500). The more samples you have, the less duplicates you will
end up with.
Generated BIC code will be 100% random.
TXT
)]
class IbanBicAnonymizer extends AbstractMultipleColumnAnonymizer
{
#[\Override]
protected function validateOptions(): void
{
if ($this->options->has('sample_size')) {
$this->options->getInt('sample_size');
}
if ($this->options->has('country')) {
$value = $this->options->getString('country');
if (!\ctype_alpha($value) || 2 !== \strlen($value)) {
throw new \InvalidArgumentException("'country' option must be a 2-letters country code.");
}
}
}

#[\Override]
protected function getColumnNames(): array
{
Expand All @@ -43,12 +50,15 @@ protected function getColumnNames(): array
#[\Override]
protected function getSamples(): array
{
$sampleSize = $this->options->getInt('sample_size', 500);
$countryCode = $this->options->getString('country', 'FR');

// @todo, pas d'options, pas de count ni de country, désolé.
$ret = [];
for ($i = 0; $i < 500; ++$i) {
for ($i = 0; $i < $sampleSize; ++$i) {
$ret[] = [
Iban::iban('FR'),
'BDFEFR2L', // @todo génération de bic aléatoire selon l'IBAN
Iban::iban($countryCode),
Iban::bic(),
];
}
return $ret;
Expand Down
20 changes: 20 additions & 0 deletions src/Helper/Iban.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ public static function iban(?string $countryCode = null, string $prefix = '', ?i
return $countryCode . $checksum . $result;
}

public static function bic(): string
{
$result = '';

$letterMin = \ord('A');
$letterMax = \ord('Z');

// BIC format can be 8 or 11 characters long.
// Format is: LLLL LL XX (XXX)
// Where L means letter, and X can be either a letter or a number.
for ($i = 0; $i < 6; ++$i) {
$result .= \chr(\rand($letterMin, $letterMax));
}
for ($i = 0; $i < 2; ++$i) {
$result .= \rand(0, 1) ? \rand(0, 9) : \chr(\rand($letterMin, $letterMax));
}

return $result;
}

/**
* @see https://github.com/FakerPHP/Faker
*/
Expand Down

0 comments on commit 2662d55

Please sign in to comment.