diff --git a/composer.json b/composer.json index 88724f2a60..f8da50d5ce 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,9 @@ "bamarni/composer-bin-plugin": "^1.4.1", "doctrine/persistence": "^1.3 || ^2.0", "phpunit/phpunit": "^9.5.26", - "symfony/phpunit-bridge": "^5.4.16" + "symfony/intl": "^7.2", + "symfony/phpunit-bridge": "^5.4.16", + "symfony/validator": "^7.2" }, "autoload": { "psr-4": { 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..94770bc5cb 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 null|string $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 (!is_null($countryCode) && 1 !== preg_match('/^[A-Z]{2}$/', $countryCode)) { + throw new \InvalidArgumentException('Invalid country code.'); + } + + 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..ba4303dc64 100644 --- a/test/Faker/Provider/PaymentTest.php +++ b/test/Faker/Provider/PaymentTest.php @@ -9,6 +9,8 @@ use Faker\Provider\Payment as PaymentProvider; use Faker\Provider\Person as PersonProvider; use Faker\Test\TestCase; +use Symfony\Component\Validator\Constraints as Assert; +use Symfony\Component\Validator\Validation; /** * @group legacy @@ -197,4 +199,26 @@ protected function getProviders(): iterable yield new PaymentProvider($this->faker); } + + public function testSwiftBicNumber(): void + { + for ($i = 0; $i < 32; $i++) { + $violations = Validation::createValidator()->validate($this->faker->swiftBicNumber(), [ + new Assert\Bic(), + ]); + + self::assertSame(0, $violations->count()); + } + } + + public function testLocalizedSwiftBicNumber(): void + { + $localizedBic = $this->faker->swiftBicNumber('DE'); + $violations = Validation::createValidator()->validate($localizedBic, [ + new Assert\Bic(), + ]); + + self::assertSame(0, $violations->count()); + self::assertMatchesRegularExpression('/^([A-Z]){4}DE([0-9A-Z]){2}([0-9A-Z]{3})?$/', $localizedBic); + } }