Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added back SlovakIbanAdapter #12

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,38 @@ if (!$validator->isValid()) {
}
```

### Slovak IBAN

Construct IBAN from Slovak account number and bank code

```php
<?php

use Rikudou\Iban\Iban\SlovakIbanAdapter;

$iban = new SlovakIbanAdapter('1325090010', '0900');

echo $iban->asString(); // prints SK5009000000001325090010

```

### Slovak IBAN validator

```php
<?php

use Rikudou\Iban\Iban\SlovakIbanAdapter;

$iban = new SlovakIbanAdapter('1325090010', '0900');

// currently returns just an instance of GenericIbanValidator
$validator = $iban->getValidator();

if (!$validator->isValid()) {
// do something
}
```

### Hungarian IBAN

```php
Expand Down
82 changes: 82 additions & 0 deletions src/Iban/CzechAndSlovakIbanAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Rikudou\Iban\Iban;

use Rikudou\Iban\Helper\ToStringIbanTrait;
use Rikudou\Iban\Validator\GenericIbanValidator;
use Rikudou\Iban\Validator\ValidatorInterface;

/**
* @internal
*/
abstract class CzechAndSlovakIbanAdapter implements IbanInterface
maryo marked this conversation as resolved.
Show resolved Hide resolved
{
use ToStringIbanTrait;

/**
* @var string
*/
protected $accountNumber;

/**
* @var string
*/
protected $bankCode;

/**
* @var string|null
*/
private $iban = null;

public function __construct(string $accountNumber, string $bankCode)
{
$this->accountNumber = $accountNumber;
$this->bankCode = $bankCode;
}

/**
* Returns the resulting IBAN.
*
* @return string
*/
public function asString(): string
{
if (is_null($this->iban)) {
$countryCode = $this->getCountryCode();
$part1 = ord($countryCode[0]) - ord('A') + 10;
$part2 = ord($countryCode[1]) - ord('A') + 10;

$accountPrefix = 0;
$accountNumber = $this->accountNumber;
if (strpos($accountNumber, '-') !== false) {
$accountParts = explode('-', $accountNumber);
$accountPrefix = $accountParts[0];
$accountNumber = $accountParts[1];
}

$numeric = sprintf('%04d%06s%010s%d%d00', $this->bankCode, $accountPrefix, $accountNumber, $part1, $part2);

$mod = '';
foreach (str_split($numeric) as $n) {
$mod = ($mod . $n) % 97;
}
$mod = intval($mod);

$this->iban = sprintf('%.2s%02d%04s%06s%010s', $countryCode, 98 - $mod, $this->bankCode, $accountPrefix, $accountNumber);
}

return $this->iban;
}

/**
* Returns the validator that checks whether the IBAN is valid.
*
* @return ValidatorInterface|null
*/
public function getValidator(): ?ValidatorInterface
{
return new GenericIbanValidator($this);
}

abstract protected function getCountryCode(): string;
}
69 changes: 6 additions & 63 deletions src/Iban/CzechIbanAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,23 @@

namespace Rikudou\Iban\Iban;

use Rikudou\Iban\Helper\ToStringIbanTrait;
use Rikudou\Iban\Validator\CompoundValidator;
use Rikudou\Iban\Validator\CzechIbanValidator;
use Rikudou\Iban\Validator\GenericIbanValidator;
use Rikudou\Iban\Validator\ValidatorInterface;

class CzechIbanAdapter implements IbanInterface
class CzechIbanAdapter extends CzechAndSlovakIbanAdapter
{
use ToStringIbanTrait;

/**
* @var string
*/
private $accountNumber;

/**
* @var string
*/
private $bankCode;

/**
* @var string|null
*/
private $iban = null;

public function __construct(string $accountNumber, string $bankCode)
{
$this->accountNumber = $accountNumber;
$this->bankCode = $bankCode;
}

/**
* Returns the resulting IBAN.
*
* @return string
*/
public function asString(): string
{
if (is_null($this->iban)) {
$part1 = ord('C') - ord('A') + 10;
$part2 = ord('Z') - ord('A') + 10;

$accountPrefix = 0;
$accountNumber = $this->accountNumber;
if (strpos($accountNumber, '-') !== false) {
$accountParts = explode('-', $accountNumber);
$accountPrefix = $accountParts[0];
$accountNumber = $accountParts[1];
}

$numeric = sprintf('%04d%06s%010s%d%d00', $this->bankCode, $accountPrefix, $accountNumber, $part1, $part2);

$mod = '';
foreach (str_split($numeric) as $n) {
$mod = ($mod . $n) % 97;
}
$mod = intval($mod);

$this->iban = sprintf('%.2s%02d%04s%06s%010s', 'CZ', 98 - $mod, $this->bankCode, $accountPrefix, $accountNumber);
}

return $this->iban;
}

/**
* Returns the validator that checks whether the IBAN is valid.
*
* @return ValidatorInterface|null
*/
public function getValidator(): ?ValidatorInterface
{
return new CompoundValidator(
new CzechIbanValidator($this->accountNumber, $this->bankCode),
new GenericIbanValidator($this)
);
}

protected function getCountryCode(): string
{
return 'CZ';
}
}
11 changes: 11 additions & 0 deletions src/Iban/SlovakIbanAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rikudou\Iban\Iban;

class SlovakIbanAdapter extends CzechAndSlovakIbanAdapter
{
protected function getCountryCode(): string
{
return 'SK';
}
}
90 changes: 90 additions & 0 deletions tests/SlovakIbanAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Rikudou\Iban\Tests;

use PHPUnit\Framework\TestCase;
use Rikudou\Iban\Iban\SlovakIbanAdapter;
use Rikudou\Iban\Validator\ValidatorInterface;

class SlovakIbanAdapterTest extends TestCase
{
public function testAccountsWithoutPrefix()
{
$accounts = [
'SK47 7500 0000 0013 2509 0010' => [
'acc' => '1325090010',
'bank' => '7500',
],
'SK26 0720 0000 0000 0398 3815' => [
'acc' => '3983815',
'bank' => '0720',
],
'SK71 0200 0000 0013 2509 0061' => [
'acc' => '1325090061',
'bank' => '0200',
],
'SK27 0900 0000 0002 8111 5217' => [
'acc' => '281115217',
'bank' => '0900',
],
'SK67 5600 0000 0005 0011 4004' => [
'acc' => '500114004',
'bank' => '5600',
],
'SK38 1100 0000 0029 0197 2682' => [
'acc' => '2901972682',
'bank' => '1100',
],
];

foreach ($accounts as $iban => $accountData) {
$iban = str_replace(' ', '', $iban);
$this->assertEquals($iban, $this->getIban($accountData['acc'], $accountData['bank'])->asString());
$this->assertEquals($iban, strval($this->getIban($accountData['acc'], $accountData['bank'])));
}
}

public function testAccountsWithPrefix()
{
$accounts = [
'SK98 7500 0010 1100 1792 9051' => [
'acc' => '17929051',
'bank' => '7500',
'prefix' => '1011',
],
'SK86 0720 0210 1200 2792 4051' => [
'acc' => '27924051',
'bank' => '0720',
'prefix' => '21012',
],
];

foreach ($accounts as $iban => $accountData) {
$iban = str_replace(' ', '', $iban);
$this->assertEquals($iban, $this->getIban($accountData['acc'], $accountData['bank'], $accountData['prefix'])->asString());
$this->assertEquals($iban, strval($this->getIban($accountData['acc'], $accountData['bank'], $accountData['prefix'])));
}
}

public function testGetValidator()
{
$this->assertInstanceOf(ValidatorInterface::class, $this->getIban(1325090010, 7500)->getValidator());
}

/**
* @param string|int $account
* @param string|int $bankCode
* @param string|int|null $prefix
*
* @return SlovakIbanAdapter
*/
private function getIban($account, $bankCode, $prefix = null): SlovakIbanAdapter
{
if (!is_null($prefix)) {
$account = "{$prefix}-{$account}";
}
$ibanAdapter = new SlovakIbanAdapter($account, $bankCode);

return $ibanAdapter;
}
}