Skip to content

Commit

Permalink
Fix BBAN not validated for IBAN validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Dec 13, 2022
1 parent dad8b1b commit 0c6a58a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [1.0.0-beta4] - In progress
## [1.0.0-beta4] - 2022-12-13

### Added

- All missing countries code, who throw exception on certain functionalities

### Fixed

- BBAN not validated for IBAN validation

## [1.0.0-beta3] - 2022-08-17

### Changed
Expand Down
9 changes: 8 additions & 1 deletion src/Bban.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace ElGigi\Iban;

use ElGigi\Iban\Validation\BbanValidation;
use InvalidArgumentException;
use JsonSerializable;
use RuntimeException;
use Stringable;
Expand Down Expand Up @@ -75,7 +76,13 @@ public static function parse(string $bban, Country|string|null $country): static
$arguments[$key] = substr($bban, ...$value);
}

return new static(...$arguments);
$bban = new static(...$arguments);

if (false === $bban->isValid()) {
throw new InvalidArgumentException(sprintf('"%s" is not a valid BBAN', $bban));
}

return $bban;
}

/**
Expand Down
25 changes: 15 additions & 10 deletions src/Validation/IbanValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace ElGigi\Iban\Validation;

use ElGigi\Iban\Iban;
use Throwable;

class IbanValidation
{
Expand All @@ -25,18 +26,22 @@ class IbanValidation
*/
public static function validate(Iban|string $iban): bool
{
is_string($iban) && $iban = Iban::parse($iban);
try {
is_string($iban) && $iban = Iban::parse($iban);

if (0 === preg_match(sprintf('/^%s$/i', $iban->getCountry()->getIbanRegex()), $iban->format(true))) {
return false;
}
if (0 === preg_match(sprintf('/^%s$/i', $iban->getCountry()->getIbanRegex()), $iban->format(true))) {
return false;
}

$ibanStr = strtoupper(
$iban->bban->format(true) .
$iban->bban->country->name .
sprintf('%02d', $iban->checkDigits)
);
$ibanStr = strtoupper(
$iban->bban->format(true) .
$iban->bban->country->name .
sprintf('%02d', $iban->checkDigits)
);

return 1 === Checksum::modulo(Checksum::numericConversion($ibanStr), 97);
return 1 === Checksum::modulo(Checksum::numericConversion($ibanStr), 97);
} catch (Throwable) {
return false;
}
}
}
39 changes: 39 additions & 0 deletions tests/Validation/IbanValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/*
* @license https://opensource.org/licenses/MIT MIT License
* @copyright 2022 Ronan GIRON
* @author Ronan GIRON <https://github.com/ElGigi>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code, to the root.
*/

namespace ElGigi\Iban\Tests\Validation;

use ElGigi\Iban\Validation\IbanValidation;
use PHPUnit\Framework\TestCase;

class IbanValidationTest extends TestCase
{
public function provider(): array
{
return [
[
'iban' => 'AD12 0001 2030 2003 5910 0100',
'valid' => true,
],
[
'iban' => 'rs18datagadisp',
'valid' => false,
],
];
}

/**
* @dataProvider provider
*/
public function testValidate(string $iban, bool $valid)
{
$this->assertSame($valid, IbanValidation::validate($iban));
}
}

0 comments on commit 0c6a58a

Please sign in to comment.