Skip to content

Commit 3788f9d

Browse files
committed
Added Iban value object
1 parent 09c4bb6 commit 3788f9d

File tree

5 files changed

+129
-4
lines changed

5 files changed

+129
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
This changelog references the relevant changes done between versions.
44

55
To get the diff for a specific change, go to https://github.com/LIN3S/SharedKernel/commit/XXX where XXX is the change hash
6-
To get the diff between two versions, go to https://github.com/LIN3S/SharedKernel/compare/v0.6.0...v0.7.0
6+
To get the diff between two versions, go to https://github.com/LIN3S/SharedKernel/compare/v0.7.0...v0.8.0
77

8+
* 0.8.0
9+
* Added Iban value object.
810
* 0.7.1
911
* Added accesses to the primitive value of the TaxIdNumber.
1012
* 0.7.0

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"keyvanakbary/slugifier": "^4.0",
1919
"ramsey/uuid": "^3.5",
2020
"uvinum/zipcodevalidator": "^1.0",
21-
"ulabox/nif-validator": "^1.2"
21+
"ulabox/nif-validator": "^1.2",
22+
"jschaedl/iban": "^1.3"
2223
},
2324
"require-dev": {
2425
"doctrine/orm": "^2.5",
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Shared Kernel library.
5+
*
6+
* Copyright (c) 2016-present LIN3S <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace LIN3S\SharedKernel\Domain\Model\Iban;
15+
16+
use IBAN\Core\IBAN as BaseIban;
17+
use IBAN\Generation\IBANGeneratorES;
18+
19+
/**
20+
* @author Beñat Espiña <[email protected]>
21+
*/
22+
class Iban
23+
{
24+
/** @var BaseIban */
25+
private $iban;
26+
27+
public static function from(string $iban) : self
28+
{
29+
return new self($iban);
30+
}
31+
32+
public static function fromSpain(string $instituteIdentification, string $bankAccountNumber) : self
33+
{
34+
return new self((new IBANGeneratorES())->generate($instituteIdentification, $bankAccountNumber));
35+
}
36+
37+
private function __construct(string $iban)
38+
{
39+
$this->setIban($iban);
40+
}
41+
42+
private function setIban(string $iban) : void
43+
{
44+
$iban = new BaseIban($iban);
45+
$this->checkIbanIsValid($iban);
46+
$this->iban = $iban;
47+
}
48+
49+
private function checkIbanIsValid(BaseIban $iban) : void
50+
{
51+
if (!$iban->validate()) {
52+
throw new IbanInvalidException($iban->format());
53+
}
54+
}
55+
56+
public function iban() : string
57+
{
58+
return $this->iban->format();
59+
}
60+
61+
public function localCode() : string
62+
{
63+
return (string) $this->iban->getLocaleCode();
64+
}
65+
66+
public function checksum() : string
67+
{
68+
return (string) $this->iban->getChecksum();
69+
}
70+
71+
public function accountIdentification() : string
72+
{
73+
return (string) $this->iban->getAccountIdentification();
74+
}
75+
76+
public function instituteIdentification() : string
77+
{
78+
return (string) $this->iban->getInstituteIdentification();
79+
}
80+
81+
public function bankAccountNumber() : string
82+
{
83+
return (string) $this->iban->getBankAccountNumber();
84+
}
85+
86+
public function equals(Iban $iban) : bool
87+
{
88+
return $this->iban() === $iban->iban();
89+
}
90+
91+
public function __toString() : string
92+
{
93+
return (string) $this->iban();
94+
}
95+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Shared Kernel library.
5+
*
6+
* Copyright (c) 2016-present LIN3S <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace LIN3S\SharedKernel\Domain\Model\Iban;
15+
16+
use LIN3S\SharedKernel\Exception\DomainException;
17+
18+
/**
19+
* @author Beñat Espiña <[email protected]>
20+
*/
21+
class IbanInvalidException extends DomainException
22+
{
23+
public function __construct(string $iban)
24+
{
25+
parent::__construct(sprintf('The given "%s" is not a valid.', $iban));
26+
}
27+
}

src/LIN3S/SharedKernel/Domain/Model/TaxIdNumber/TaxIdNumberInvalidException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
namespace LIN3S\SharedKernel\Domain\Model\TaxIdNumber;
1515

16-
use LIN3S\SharedKernel\Exception\Exception;
16+
use LIN3S\SharedKernel\Exception\DomainException;
1717

1818
/**
1919
* @author Beñat Espiña <[email protected]>
2020
*/
21-
class TaxIdNumberInvalidException extends Exception
21+
class TaxIdNumberInvalidException extends DomainException
2222
{
2323
public static function fromSpain(string $tin) : self
2424
{

0 commit comments

Comments
 (0)