Skip to content

Commit f1e26fa

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 f1e26fa

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 9 additions & 3 deletions
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 null|string $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 (!is_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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Faker\Provider\Payment as PaymentProvider;
1010
use Faker\Provider\Person as PersonProvider;
1111
use Faker\Test\TestCase;
12+
use Symfony\Component\Validator\Constraints as Assert;
13+
use Symfony\Component\Validator\Validation;
1214

1315
/**
1416
* @group legacy
@@ -197,4 +199,20 @@ protected function getProviders(): iterable
197199

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

0 commit comments

Comments
 (0)