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

En_GB local payment provider new methods #1832

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,21 @@ echo $faker->bank; // "Volksbank Stuttgart"

```

### `Faker\Provider\en_GB\Payment`

```php
<?php

echo $faker->ukBankAccountNumber(); // 8 digit UK bank account number, eg. "40506070"
echo $faker->ukSortCode(); // 3 times 2 digits UK Sort code, eg. "01-02-03"

//Optional parameters
echo $faker->ukBankAccountNumber('0123'); // Parameter prefix will used as the beginning of the account number
// and the remaining digits will be generated, eg. "01239876"
echo $faker->ukSortCode('', '01', ''); // Any of the 2 digit pairs can be prefilled, result eg. "10-01-20"

```

### `Faker\Provider\en_HK\Address`

```php
Expand Down
2 changes: 2 additions & 0 deletions src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@
* @property string $colorName
*
* @method string randomHtml($maxDepth = 4, $maxWidth = 4)
* @method ukSortCode($ab = '', $cd = '', $ef = '')
* @method ukBankAccountNumber($prefix = '')
*
*/
class Generator
Expand Down
33 changes: 33 additions & 0 deletions src/Faker/Provider/en_GB/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,37 @@ public static function bankAccountNumber($prefix = '', $countryCode = 'GB', $len
{
return static::iban($countryCode, $prefix, $length);
}

/**
* @param string $prefix
* @return string
*/
public static function ukBankAccountNumber($prefix = '')
{
if (preg_match('^\d+$^', $prefix) !== 1) {
$prefix = '';
}
$ukBankAccountNumber =
$prefix . static::numerify(
str_pad('', 8-strlen($prefix), '#')
);

return $ukBankAccountNumber;
}

/**
* @param string $ab
* @param string $cd
* @param string $ef
* @return string
*/
public static function ukSortCode($ab = '', $cd = '', $ef = '')
{
$ukSortCode =
(preg_match('^\d+$^', $ab) === 1 ? $ab : static::numerify('##')).'-'.
(preg_match('^\d+$^', $cd) === 1 ? $cd : static::numerify('##')).'-'.
(preg_match('^\d+$^', $ef) === 1 ? $ef : static::numerify('##'));

return $ukSortCode;
}
}
53 changes: 53 additions & 0 deletions test/Faker/Provider/en_GB/PaymentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php


namespace test\Faker\Provider\en_GB;

use Faker\Generator;
use Faker\Provider\en_GB\Payment;

class PaymentTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Generator
*/
private $faker;

public function setUp()
{
$faker = new Generator();
$faker->addProvider(new Payment($faker));
$this->faker = $faker;
}

/**
*
*/
public function testUkSortCode()
{
$sortcode = $this->faker->ukSortCode();
$this->assertInternalType('string', $sortcode);
$this->assertEquals(8, strlen($sortcode));
$parts = explode('-', $sortcode);
foreach ($parts as $part)
{
$this->assertEquals(1, preg_match('^\d+$^', $part));
}
}

public function testUkBankAccount()
{
$UkBankAccount = $this->faker->ukBankAccountNumber();
$this->assertInternalType('string', $UkBankAccount);
$this->assertEquals(1, preg_match('^\d+$^', $UkBankAccount));
$this->assertEquals(8, strlen($UkBankAccount));
$UkBankAccount = $this->faker->ukBankAccountNumber('0123');
$this->assertInternalType('string', $UkBankAccount);
$this->assertEquals(1, preg_match('^\d+$^', $UkBankAccount));
$this->assertEquals(8, strlen($UkBankAccount));
$UkBankAccount = $this->faker->ukBankAccountNumber('hello');
$this->assertInternalType('string', $UkBankAccount);
$this->assertEquals(1, preg_match('^\d+$^', $UkBankAccount));
$this->assertEquals(8, strlen($UkBankAccount));
}
}