Skip to content

Commit

Permalink
Merge pull request #6 from snamor/add-string-sanitation-on-string-met…
Browse files Browse the repository at this point in the history
…hods

Add string sanitation on "fromString" functions.
  • Loading branch information
xafardero authored Feb 3, 2017
2 parents 0c65355 + f68d927 commit 5b824e0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 7 additions & 5 deletions src/Bban/SpainBban.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
}
Expand Down
22 changes: 12 additions & 10 deletions src/Iban.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions tests/IbanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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'],
];
}
Expand Down

0 comments on commit 5b824e0

Please sign in to comment.