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

Fix IBAN generator #590

Merged
merged 5 commits into from
Jun 15, 2015
Merged
Changes from 3 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
46 changes: 27 additions & 19 deletions src/Faker/Provider/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Payment extends Base
'IL' => array(array('n', 3), array('n', 3), array('n', 13)),
'IS' => array(array('n', 4), array('n', 2), array('n', 6), array('n', 10)),
'IT' => array(array('a', 1), array('n', 5), array('n', 5), array('c', 12)),
'KW' => array(array('a', 4), array('c', 22)),
'KW' => array(array('a', 4), array('n', 22)),
'KZ' => array(array('n', 3), array('c', 13)),
'LB' => array(array('n', 4), array('c', 20)),
'LI' => array(array('n', 5), array('c', 12)),
Expand Down Expand Up @@ -115,7 +115,7 @@ class Payment extends Base
'SK' => array(array('n', 4), array('n', 6), array('n', 10)),
'SM' => array(array('a', 1), array('n', 5), array('n', 5), array('c', 12)),
'TN' => array(array('n', 2), array('n', 3), array('n', 13), array('n', 2)),
'TR' => array(array('n', 5), array('c', 1), array('c', 16)),
'TR' => array(array('n', 5), array('n', 1), array('c', 16)),
'VG' => array(array('a', 4), array('n', 16)),
);

Expand Down Expand Up @@ -213,7 +213,7 @@ public function creditCardDetails($valid = true)
protected static function iban($countryCode, $prefix = '', $length = null)
{
$countryCode = strtoupper($countryCode);
$format = !isset(static::$ibanFormats[$countryCode]) ? array() : static::$ibanFormats[$countryCode];
$format = !isset(static::$ibanFormats[$countryCode]) ? null : static::$ibanFormats[$countryCode];
if ($length === null) {
if ($format === null) {
$length = 24;
Expand All @@ -225,22 +225,20 @@ protected static function iban($countryCode, $prefix = '', $length = null)
}
}
}
if ($format === null) {
return false;
Copy link
Owner

Choose a reason for hiding this comment

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

please remove

$format = array(array('n', $length));
}

$result = $prefix;
$length -= strlen($prefix);
$nextPart = array_shift($format);
if ($nextPart !== false) {
list($class, $groupCount) = $nextPart;
} else {
$class = 'n';
$groupCount = 0;
$expandedFormat = '';
foreach ($format as $item) {
list($class, $length) = $item;
$expandedFormat .= str_repeat($class, $length);
}
$groupCount = $nextPart === false ? 0 : $nextPart[1];
for ($i = 0; $i < $length; $i++) {
if ($nextPart !== false && $groupCount-- < 1) {
$nextPart = array_shift($format);
list($class, $groupCount) = $nextPart;
}

$result = $prefix;
$expandedFormat = substr($expandedFormat, strlen($result));
foreach (str_split($expandedFormat) as $class) {
switch ($class) {
default:
case 'c':
Expand All @@ -257,8 +255,18 @@ protected static function iban($countryCode, $prefix = '', $length = null)

$result = static::addBankCodeChecksum($result, $countryCode);

$countryNumber = 100 * (ord($countryCode[0])-55) + (ord($countryCode[1])-55);
$tempResult = $result . $countryNumber . '00';
$tempResult = str_split($result . $countryCode . '00');

// Convert letters to numbers
foreach ($tempResult as &$letter) {
$num = ord($letter) - 55;
if ($num >= 10 && $num <= 35) {
$letter = str_pad($num, 2, '0', STR_PAD_LEFT);
}
}
unset($letter);
$tempResult = join('', $tempResult);

// perform MOD97-10 checksum calculation
$checksum = (int) $tempResult[0];
for ($i = 1, $size = strlen($tempResult); $i < $size; $i++) {
Expand Down