Skip to content
This repository was archived by the owner on Dec 11, 2020. It is now read-only.

+1 SWIFT/BIC payment type #380

Closed
wants to merge 1 commit into from
Closed
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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
}
],
"require": {
"php": ">=5.3.3"
"php": ">=5.3.3",
"icomefromthenet/reverse-regex": "v0.0.6.2"
Copy link
Owner

Choose a reason for hiding this comment

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

Great tool! I didn't know it. If you want my opinion, this deserves a dedicated regexp() formatter in the Base Provider - there will be more applications for this.

Copy link
Owner

Choose a reason for hiding this comment

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

As a side note, since parsing regexp may be slow, it might be a good idea to implement a cache (only one Parser instance per regexp string).

Copy link
Owner

Choose a reason for hiding this comment

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

Note that Faker now has its own regex-based generator (see #453), that you must use for your formatter.

},
"require-dev": {
"phpunit/phpunit": "~4.0",
Expand Down
24 changes: 24 additions & 0 deletions src/Faker/Provider/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Faker\Provider;

use ReverseRegex\Lexer;
use ReverseRegex\Random\SimpleRandom;
use ReverseRegex\Parser;
use ReverseRegex\Generator\Scope;

class Payment extends Base
{
public static $expirationDateFormat = "m/y";
Expand Down Expand Up @@ -156,6 +161,25 @@ public static function creditCardNumber($type = null, $formatted = false, $separ
return $number;
}

/**
* Return the String of a SWIFT/BIC number
*
* @example 'RZTIAT22263'
* @link http://en.wikipedia.org/wiki/ISO_9362
*/
public static function swiftBicNumber()
{
$lexer = new Lexer("^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$");
$gen = new SimpleRandom(10002);
Copy link
Owner

Choose a reason for hiding this comment

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

You'll have to use a MersenneRandom and seed it with the current generator seed (hence, this cannot be a static method).

$result = '';

$parser = new Parser($lexer, new Scope(), new Scope());
$parser->parse()->getResult()->generate($result, $gen);

return $result;

}

/**
* @param boolean $valid True (by default) to get a valid expiration date, false to get a maybe valid date
* @example 04/13
Expand Down
5 changes: 5 additions & 0 deletions test/Faker/Provider/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public function testRandomCard()
$this->assertEquals(count($cardDetails), 4);
$this->assertEquals(array('type', 'number', 'name', 'expirationDate'), array_keys($cardDetails));
}

public function testSwiftBicTypeReturnsValidSwitBic()
{
$this->assertRegExp('/^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$/', $this->faker->swiftBicNumber);
}
}