Skip to content

Commit 66660fd

Browse files
committed
fix: Generate valid BIC/SWIFT numbers (#902)
- swiftBicNumber now accepts an optional $countryCode argument to localize the generated value - using Symfony Validator in tests
1 parent bbc79fe commit 66660fd

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
- Removed domain `gmail.com.au` from `Provider\en_AU\Internet` (#886)
66
- Refreshed ISO currencies (#919)
7-
-
7+
- Generate valid BIC/SWIFT numnbers (#902)
8+
89
## [2024-11-09, v1.24.0](https://github.com/FakerPHP/Faker/compare/v1.23.1..v1.24.0)
910

1011
- Fix internal deprecations in Doctrine's populator by @gnutix in (#889)

src/Faker/Generator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@
443443
*
444444
* @property string $swiftBicNumber
445445
*
446-
* @method string swiftBicNumber()
446+
* @method string swiftBicNumber($countryCode = null)
447447
*
448448
* @property string $name
449449
*

src/Faker/Provider/Payment.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,22 @@ public static function iban($countryCode = null, $prefix = '', $length = null)
297297
}
298298

299299
/**
300-
* Return the String of a SWIFT/BIC number
300+
* Return the String of a SWIFT/BIC number.
301301
*
302302
* @example 'RZTIAT22263'
303303
*
304304
* @see http://en.wikipedia.org/wiki/ISO_9362
305305
*
306+
* @param string|null $countryCode ISO 3166-1 alpha-2 country code
307+
*
306308
* @return string Swift/Bic number
307309
*/
308-
public static function swiftBicNumber()
310+
public static function swiftBicNumber($countryCode = null)
309311
{
310-
return self::regexify('^([A-Z]){4}([A-Z]){2}([0-9A-Z]){2}([0-9A-Z]{3})?$');
312+
if (null !== $countryCode && 1 !== preg_match('/^[A-Z]{2}$/', $countryCode)) {
313+
throw new \InvalidArgumentException('Invalid country code.');
314+
}
315+
316+
return self::regexify('^([A-Z]){4}' . ($countryCode ?? Miscellaneous::countryCode()) . '([0-9A-Z]){2}([0-9A-Z]{3})?$');
311317
}
312318
}

test/Faker/Provider/PaymentTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,20 @@ protected function getProviders(): iterable
197197

198198
yield new PaymentProvider($this->faker);
199199
}
200+
201+
public function testSwiftBicNumber(): void
202+
{
203+
self::assertMatchesRegularExpression(
204+
'/^([A-Z]){4}([A-Z]){2}([0-9A-Z]){2}([0-9A-Z]{3})?$/',
205+
$this->faker->swiftBicNumber(),
206+
);
207+
}
208+
209+
public function testLocalizedSwiftBicNumber(): void
210+
{
211+
self::assertMatchesRegularExpression(
212+
'/^([A-Z]){4}DE([0-9A-Z]){2}([0-9A-Z]{3})?$/',
213+
$this->faker->swiftBicNumber('DE'),
214+
);
215+
}
200216
}

0 commit comments

Comments
 (0)