Skip to content

Commit

Permalink
Merge pull request #36 from xefi/feature/format-string
Browse files Browse the repository at this point in the history
✨ generic format string
  • Loading branch information
GautierDele authored Nov 18, 2024
2 parents 9de9e16 + cfa6eca commit 82ce3c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
17 changes: 17 additions & 0 deletions src/Extensions/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,21 @@ protected function pickArrayRandomElement(array $array): mixed

return reset($elements);
}

protected function formatString(string $string): string
{
while (($pos = strpos($string, '{a}')) !== false) {
$string = substr_replace($string, $this->pickArrayRandomElement(['{d}', '{l}']), $pos, 3);
}

while (($pos = strpos($string, '{d}')) !== false) {
$string = substr_replace($string, (string) $this->randomizer->getInt(0, 9), $pos, 3);
}

while (($pos = strpos($string, '{l}')) !== false) {
$string = substr_replace($string, $this->randomizer->getBytesFromString(implode(range('A', 'Z')), 1), $pos, 3);
}

return $string;
}
}
12 changes: 1 addition & 11 deletions src/Extensions/FinancialExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,7 @@ public function iban(?string $countryCode = null, ?string $format = null): strin
$countryCode = $this->randomizer->getBytesFromString(implode(range('A', 'Z')), 2);
}

while (($pos = strpos($format, '{a}')) !== false) {
$format = substr_replace($format, $this->pickArrayRandomElement(['{d}', '{l}']), $pos, 3);
}

while (($pos = strpos($format, '{d}')) !== false) {
$format = substr_replace($format, (string) $this->randomizer->getInt(0, 9), $pos, 3);
}

while (($pos = strpos($format, '{l}')) !== false) {
$format = substr_replace($format, $this->randomizer->getBytesFromString(implode(range('A', 'Z')), 1), $pos, 3);
}
$format = $this->formatString($format);

$checksum = Iban::checksum($countryCode.'00'.$format);

Expand Down

0 comments on commit 82ce3c0

Please sign in to comment.