Skip to content
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

fix: Generate valid BIC/SWIFT numbers (#902) #921

Open
wants to merge 2 commits into
base: 1.24
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

- Removed domain `gmail.com.au` from `Provider\en_AU\Internet` (#886)
- Refreshed ISO currencies (#919)
-
- Generate valid BIC/SWIFT numnbers (#902)

## [2024-11-09, v1.24.0](https://github.com/FakerPHP/Faker/compare/v1.23.1..v1.24.0)

- Fix internal deprecations in Doctrine's populator by @gnutix in (#889)
Expand Down
2 changes: 1 addition & 1 deletion src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@
*
* @property string $swiftBicNumber
*
* @method string swiftBicNumber()
* @method string swiftBicNumber($countryCode = null)
*
* @property string $name
*
Expand Down
12 changes: 9 additions & 3 deletions src/Faker/Provider/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,22 @@ public static function iban($countryCode = null, $prefix = '', $length = null)
}

/**
* Return the String of a SWIFT/BIC number
* Return the String of a SWIFT/BIC number.
*
* @example 'RZTIAT22263'
*
* @see http://en.wikipedia.org/wiki/ISO_9362
*
* @param string|null $countryCode ISO 3166-1 alpha-2 country code
*
* @return string Swift/Bic number
*/
public static function swiftBicNumber()
public static function swiftBicNumber($countryCode = null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit shady since you have no clue what can be put in here and all can be invalid. I think we have a list of all country codes already in the package. Can you use that to verify it exists?

{
return self::regexify('^([A-Z]){4}([A-Z]){2}([0-9A-Z]){2}([0-9A-Z]{3})?$');
if (null !== $countryCode && 1 !== preg_match('/^[A-Z]{2}$/', $countryCode)) {
throw new \InvalidArgumentException('Invalid country code format.');
}

return self::regexify('^([A-Z]){4}' . ($countryCode ?? Miscellaneous::countryCode()) . '([0-9A-Z]){2}([0-9A-Z]{3})?$');
}
}
16 changes: 16 additions & 0 deletions test/Faker/Provider/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,20 @@ protected function getProviders(): iterable

yield new PaymentProvider($this->faker);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test for an invalid country code

public function testSwiftBicNumber(): void
{
self::assertMatchesRegularExpression(
'/^([A-Z]){4}([A-Z]){2}([0-9A-Z]){2}([0-9A-Z]{3})?$/',
$this->faker->swiftBicNumber(),
);
}

public function testLocalizedSwiftBicNumber(): void
{
self::assertMatchesRegularExpression(
'/^([A-Z]){4}DE([0-9A-Z]){2}([0-9A-Z]{3})?$/',
$this->faker->swiftBicNumber('DE'),
);
}
}
Loading