diff --git a/.travis.yml b/.travis.yml index efd3c8f..e7735f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,12 +9,12 @@ php: before_script: - composer self-update - composer install --prefer-source --no-interaction --dev + - wget https://scrutinizer-ci.com/ocular.phar script: - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover after_script: - - wget https://scrutinizer-ci.com/ocular.phar - php ocular.phar code-coverage:upload --format=php-clover coverage.clover matrix: diff --git a/src/Bban/SpainBban.php b/src/Bban/SpainBban.php index 76c8998..6060bd7 100644 --- a/src/Bban/SpainBban.php +++ b/src/Bban/SpainBban.php @@ -42,11 +42,11 @@ public function __construct( $checkDigits, $accountNumber ) { - static::validateBankCodeFormat($bankCode); - static::validateBranchCodeFormat($branchCode); - static::validateCheckDigitsFormat($checkDigits); - static::validateAccountNumberFormat($accountNumber); - static::validateControlDigit( + self::validateBankCodeFormat($bankCode); + self::validateBranchCodeFormat($branchCode); + self::validateCheckDigitsFormat($checkDigits); + self::validateAccountNumberFormat($accountNumber); + self::validateControlDigit( $bankCode, $branchCode, $checkDigits, @@ -68,6 +68,8 @@ public function __construct( */ public static function fromString($bban) { + $bban = preg_replace('/[^0-9a-zA-Z]+/', '', $bban); + if (! preg_match('/^[\d]{20}$/', $bban)) { throw new InvalidArgumentException('Bban should be 20 numbers'); } diff --git a/src/Iban.php b/src/Iban.php index 7506fd9..b068caf 100644 --- a/src/Iban.php +++ b/src/Iban.php @@ -42,9 +42,9 @@ class Iban public function __construct($countryCode, $checkDigits, BbanInterface $bban) { $countryCode = strtoupper($countryCode); - static::validateCountryCodeFormat($countryCode); - static::validateCheckDigitsFormat($checkDigits); - static::validateControlDigit($countryCode, $checkDigits, $bban); + self::validateCountryCodeFormat($countryCode); + self::validateCheckDigitsFormat($checkDigits); + self::validateControlDigit($countryCode, $checkDigits, $bban); $this->countryCode = $countryCode; $this->checkDigits = $checkDigits; $this->bban = $bban; @@ -59,13 +59,15 @@ public function __construct($countryCode, $checkDigits, BbanInterface $bban) */ public static function fromString($iban) { - if (! preg_match('/^[0-9A-Z]{16,34}$/', $iban)) { + $iban = preg_replace('/[^0-9a-zA-Z]+/', '', $iban); + + if (! preg_match('/^[0-9a-zA-Z]{16,34}$/', $iban)) { throw new InvalidArgumentException('Iban should be between 16 and 34 characters'); } - $countryCode = substr($iban, 0, 2); - $checkDigits = substr($iban, 2, 2); - $bbanString = substr($iban, 4); + $countryCode = strtoupper(substr($iban, 0, 2)); + $checkDigits = strtoupper(substr($iban, 2, 2)); + $bbanString = strtoupper(substr($iban, 4)); self::validateSupportedCountry($countryCode); $bbanClass = self::$countriesSupported[$countryCode]; @@ -88,9 +90,9 @@ public static function fromString($iban) */ public static function fromBbanAndCountry(BbanInterface $bban, $countryCode) { - static::validateCountryCodeFormat($countryCode); - static::validateCountryCodeFormat($countryCode); - static::validateSupportedCountry($countryCode); + self::validateCountryCodeFormat($countryCode); + self::validateCountryCodeFormat($countryCode); + self::validateSupportedCountry($countryCode); $checksum = self::validateChecksum($countryCode, '00', $bban); $checkDigit = 98 - (int) $checksum; diff --git a/tests/IbanTest.php b/tests/IbanTest.php index 29cee78..09f69f6 100644 --- a/tests/IbanTest.php +++ b/tests/IbanTest.php @@ -37,7 +37,7 @@ public function testValidIban( $iban = new Iban($countryCode, $ibanChecksum, $bban); - $this->assertEquals($countryCode, $iban->countryCode()); + $this->assertEquals(strtoupper($countryCode), $iban->countryCode()); $this->assertEquals($ibanChecksum, $iban->ibanCheckDigits()); $this->assertEquals($bankCode, $iban->bankCode()); $this->assertEquals($branchCode, $iban->branchCode()); @@ -68,7 +68,7 @@ public function testCreateFromValidString( ) { $stringIban = $countryCode . $ibanChecksum . $bankCode . $branchCode . $controlDigits . $bankAccount; $iban = Iban::fromString($stringIban); - $this->assertEquals($stringIban, $iban->__toString()); + $this->assertEquals(strtoupper($stringIban), $iban->__toString()); } /** @@ -257,7 +257,7 @@ public function invalidChecksum() public function validIbans() { return [ - ['ES', '68', '3841', '2436', '11', '6183191503'], + ['es', '68', '3841', '2436', '11', '6183191503'], ['ES', '78', '0989', '5990', '44', '6462241825'], ]; }