|
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
|
|
|
} |
|
96
|
|
|
|