1 | <?php |
||
8 | class Iban |
||
9 | { |
||
10 | /** |
||
11 | * @var Bban\BbanInterface |
||
12 | */ |
||
13 | private $bban; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $countryCode; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $checkDigits; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private static $countriesSupported = [ |
||
29 | 'ES' => Bban\SpainBban::class, |
||
30 | 'AD' => Bban\AndorraBban::class, |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * Iban constructor. |
||
35 | * |
||
36 | * @param string $countryCode |
||
37 | * @param string $checkDigits |
||
38 | * @param Bban\BbanInterface $bban |
||
39 | * |
||
40 | * @throws InvalidArgumentException |
||
41 | */ |
||
42 | 46 | public function __construct($countryCode, $checkDigits, Bban\BbanInterface $bban) |
|
52 | |||
53 | /** |
||
54 | * @param string $iban |
||
55 | * |
||
56 | * @throws InvalidArgumentException |
||
57 | * |
||
58 | * @return static |
||
59 | */ |
||
60 | 16 | public static function fromString($iban) |
|
61 | { |
||
62 | 16 | $iban = preg_replace('/[^0-9a-zA-Z]+/', '', $iban); |
|
63 | |||
64 | 16 | if (! preg_match('/^[0-9a-zA-Z]{16,34}$/', $iban)) { |
|
65 | 3 | throw new InvalidArgumentException('Iban should be between 16 and 34 characters'); |
|
66 | } |
||
67 | |||
68 | 13 | $countryCode = strtoupper(substr($iban, 0, 2)); |
|
69 | 13 | $checkDigits = strtoupper(substr($iban, 2, 2)); |
|
70 | 13 | $bbanString = strtoupper(substr($iban, 4)); |
|
71 | |||
72 | 13 | self::validateSupportedCountry($countryCode); |
|
73 | 12 | $bbanClass = self::$countriesSupported[$countryCode]; |
|
74 | |||
75 | /** |
||
76 | * @var Bban\BbanInterface |
||
77 | */ |
||
78 | 12 | $bban = $bbanClass::fromString($bbanString); |
|
79 | |||
80 | 12 | return new static($countryCode, $checkDigits, $bban); |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param Bban\BbanInterface $bban |
||
85 | * @param string $countryCode |
||
86 | * |
||
87 | * @throws InvalidArgumentException |
||
88 | * |
||
89 | * @return static |
||
90 | */ |
||
91 | 12 | public static function fromBbanAndCountry(Bban\BbanInterface $bban, $countryCode) |
|
92 | { |
||
93 | 12 | self::validateCountryCodeFormat($countryCode); |
|
94 | 12 | self::validateCountryCodeFormat($countryCode); |
|
95 | 12 | self::validateSupportedCountry($countryCode); |
|
96 | |||
97 | 12 | $checksum = self::validateChecksum($countryCode, '00', $bban); |
|
98 | 12 | $checkDigit = 98 - (int) $checksum; |
|
99 | 12 | $checkDigit = str_pad($checkDigit, 2, 0, STR_PAD_LEFT); |
|
100 | |||
101 | 12 | return new static($countryCode, $checkDigit, $bban); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * @return string |
||
106 | */ |
||
107 | 12 | public function countryCode() |
|
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | 24 | public function ibanCheckDigits() |
|
119 | |||
120 | /** |
||
121 | * @return string |
||
122 | */ |
||
123 | 12 | public function bankCode() |
|
127 | |||
128 | /** |
||
129 | * @return string |
||
130 | */ |
||
131 | 12 | public function branchCode() |
|
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | 12 | public function countryCheckDigits() |
|
143 | |||
144 | /** |
||
145 | * @return string |
||
146 | */ |
||
147 | 12 | public function accountNumber() |
|
151 | |||
152 | /** |
||
153 | * @return string |
||
154 | */ |
||
155 | 12 | public function __toString() |
|
161 | |||
162 | /** |
||
163 | * @param $countryCode |
||
164 | * |
||
165 | * @throws InvalidArgumentException |
||
166 | */ |
||
167 | 46 | private static function validateCountryCodeFormat($countryCode) |
|
173 | |||
174 | /** |
||
175 | * @param $checkDigits |
||
176 | * |
||
177 | * @throws InvalidArgumentException |
||
178 | */ |
||
179 | 42 | private static function validateCheckDigitsFormat($checkDigits) |
|
185 | |||
186 | /** |
||
187 | * @param string $countryCode |
||
188 | * @param string $checkDigits |
||
189 | * @param Bban\BbanInterface $bban |
||
190 | * |
||
191 | * @throws InvalidArgumentException |
||
192 | */ |
||
193 | 38 | private static function validateControlDigit( |
|
204 | |||
205 | /** |
||
206 | * @param $countryCode |
||
207 | * |
||
208 | * @throws InvalidArgumentException |
||
209 | */ |
||
210 | 25 | private static function validateSupportedCountry($countryCode) |
|
211 | { |
||
212 | 25 | if (!array_key_exists($countryCode, self::$countriesSupported)) { |
|
213 | 1 | throw new InvalidArgumentException( |
|
214 | 1 | sprintf( |
|
215 | 1 | 'The country code %s is not supported', |
|
216 | 1 | $countryCode |
|
217 | ) |
||
218 | ); |
||
219 | } |
||
220 | 24 | } |
|
221 | |||
222 | /** |
||
223 | * @param $countryCode |
||
224 | * @param $checkDigits |
||
225 | * @param Bban\BbanInterface $bban |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | 38 | private static function validateChecksum($countryCode, $checkDigits, Bban\BbanInterface $bban) |
|
241 | |||
242 | /** |
||
243 | * @param string $value |
||
244 | * |
||
245 | * @return int |
||
246 | */ |
||
247 | 38 | private static function digitToInt($value) |
|
255 | } |
||
256 |