/** * International Bank Account Number (IBAN) * * @link http://en.wikipedia.org/wiki/International_Bank_Account_Number * @param string $countryCode ISO 3166-1 alpha-2 country code * @param string $prefix for generating bank account number of a specific bank * @param integer $length total length without country code and 2 check digits * @return string */ public static function iban($countryCode, $prefix = '', $length = null) { $countryCode = is_null($countryCode) ? self::randomKey(self::$ibanFormats) : strtoupper($countryCode); $format = !isset(static::$ibanFormats[$countryCode]) ? null : static::$ibanFormats[$countryCode]; if ($length === null) { if ($format === null) { $length = 24; } else { $length = 0; foreach ($format as $part) { list($class, $groupCount) = $part; $length += $groupCount; } } } if ($format === null) { $format = array(array('n', $length)); } $expandedFormat = ''; foreach ($format as $item) { list($class, $length) = $item; $expandedFormat .= str_repeat($class, $length); } $result = $prefix; $expandedFormat = substr($expandedFormat, strlen($result)); foreach (str_split($expandedFormat) as $class) { switch ($class) { default: case 'c': $result .= mt_rand(0, 100) <= 50 ? static::randomDigit() : strtoupper(static::randomLetter()); break; case 'a': $result .= strtoupper(static::randomLetter()); break; case 'n': $result .= static::randomDigit(); break; } } $result = static::addBankCodeChecksum($result, $countryCode); $checksum = Iban::checksum($countryCode . '00' . $result); return $countryCode . $checksum . $result; }
/** * @dataProvider localeDataProvider */ public function testIban($locale) { $parts = explode('_', $locale); $countryCode = array_pop($parts); $this->loadLocalProviders($locale); try { $iban = $this->faker->bankAccountNumber; } catch (\InvalidArgumentException $e) { // Not implemented, nothing to test return; } // Test format $this->assertRegExp($this->ibanFormats[$countryCode], $iban); // Test checksum $this->assertTrue(Iban::isValid($iban), "Checksum for {$iban} is invalid"); }
/** * @dataProvider mod97Provider */ public function testMod97($number, $result) { $this->assertEquals($result, Iban::mod97($number), $number); }
/** * @dataProvider ibanFormatProvider */ public function testIban($countryCode, $regex) { $iban = $this->faker->iban($countryCode); // Test format $this->assertRegExp($regex, $iban); // Test checksum $this->assertTrue(Iban::isValid($iban), "Checksum for {$iban} is invalid"); }