From 21a845415b30eda031f67a2e69cb45011c0cc6bb Mon Sep 17 00:00:00 2001 From: Andrea Bergamasco Date: Sat, 7 Dec 2024 23:23:40 +0100 Subject: [PATCH] fix: Generate valid BIC/SWIFT numbers (#902) - swiftBicNumber now accepts an optional $countryCode argument to localize the generated value - using Symfony Validator in tests --- CHANGELOG.md | 3 ++- src/Faker/Generator.php | 2 +- src/Faker/Provider/Payment.php | 12 +++++++++--- test/Faker/Provider/PaymentTest.php | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81214b960d..b2effe64c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index d1320312b1..65f4f5d4c6 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -443,7 +443,7 @@ * * @property string $swiftBicNumber * - * @method string swiftBicNumber() + * @method string swiftBicNumber($countryCode = null) * * @property string $name * diff --git a/src/Faker/Provider/Payment.php b/src/Faker/Provider/Payment.php index 707ef059b8..0a1af1da05 100644 --- a/src/Faker/Provider/Payment.php +++ b/src/Faker/Provider/Payment.php @@ -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) { - 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})?$'); } } diff --git a/test/Faker/Provider/PaymentTest.php b/test/Faker/Provider/PaymentTest.php index 1e90da0d9e..e9f1a83c08 100644 --- a/test/Faker/Provider/PaymentTest.php +++ b/test/Faker/Provider/PaymentTest.php @@ -197,4 +197,20 @@ protected function getProviders(): iterable yield new PaymentProvider($this->faker); } + + 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'), + ); + } }