1 | <?php |
||
8 | class IBAN |
||
9 | { |
||
10 | const PATTERN = '/^[A-Z]{2,2}[0-9]{2,2}[A-Z0-9]{1,30}$/'; |
||
11 | |||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $iban; |
||
16 | /** |
||
17 | * @var boolean |
||
18 | */ |
||
19 | private $validIban = false; |
||
20 | |||
21 | /** |
||
22 | * Constructor |
||
23 | * |
||
24 | * @param string $iban |
||
25 | * |
||
26 | * @throws \InvalidArgumentException When the IBAN does contain invalid characters or the checksum calculation fails. |
||
27 | */ |
||
28 | 9 | public function __construct($iban) |
|
29 | { |
||
30 | 9 | $cleanedIban = str_replace(' ', '', strtoupper($iban)); |
|
31 | 9 | if (preg_match(self::PATTERN, $cleanedIban) && self::check($cleanedIban)) { |
|
32 | 6 | $this->validIban = true; |
|
33 | 6 | } |
|
34 | |||
35 | 9 | $this->iban = $cleanedIban; |
|
36 | 9 | } |
|
37 | |||
38 | /** |
||
39 | * Format the IBAN either in a human-readable manner |
||
40 | * |
||
41 | * @return string The formatted IBAN |
||
42 | */ |
||
43 | 1 | public function format() |
|
44 | { |
||
45 | 1 | $parts = str_split($this->iban, 4); |
|
46 | |||
47 | 1 | return implode(' ', $parts); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * Normalize the IBAN |
||
52 | * |
||
53 | * @return string The normalized IBAN |
||
54 | */ |
||
55 | 3 | public function normalize() |
|
59 | |||
60 | /** |
||
61 | * Gets the country |
||
62 | * |
||
63 | * @return string A ISO 3166-1 alpha-2 country code |
||
64 | */ |
||
65 | 6 | public function getCountry() |
|
69 | |||
70 | /** |
||
71 | * Checks whether the checksum of an IBAN is correct |
||
72 | * |
||
73 | * @param string $iban |
||
74 | * |
||
75 | * @return bool true if checksum is correct, false otherwise |
||
76 | */ |
||
77 | 4 | protected static function check($iban) |
|
78 | { |
||
79 | 4 | $chars = str_split(substr($iban, 4).substr($iban, 0, 4)); |
|
80 | 4 | $length = count($chars); |
|
81 | 4 | for ($i = 0; $i < $length; $i++) { |
|
82 | 4 | $code = ord($chars[$i]); |
|
83 | 4 | if ($code >= 65 && $code <= 90) { // A-Z |
|
84 | 4 | $chars[$i] = $code - 65 + 10; |
|
85 | 4 | } |
|
86 | 4 | } |
|
87 | 4 | $prepared = implode($chars); |
|
88 | |||
89 | 4 | $r = ''; |
|
90 | 4 | $rLength = 0; |
|
91 | 4 | $i = 0; |
|
92 | 4 | $length = strlen($prepared); |
|
93 | 4 | while ($i < $length) { |
|
94 | 4 | $d = $r.substr($prepared, $i, 9 - $rLength); |
|
95 | 4 | $i += 9 - $rLength; |
|
96 | 4 | $r = $d % 97; |
|
97 | 4 | $rLength = 1 + ($r >= 10); |
|
98 | 4 | } |
|
99 | |||
100 | 4 | return ($r == 1); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Returns a string representation. |
||
105 | * |
||
106 | * @return string The string representation. |
||
107 | */ |
||
108 | 3 | public function __toString() |
|
112 | |||
113 | 2 | public function getElementName() |
|
114 | { |
||
115 | 2 | if ($this->isValidIban()) { |
|
121 | |||
122 | /** |
||
123 | * @return boolean |
||
124 | */ |
||
125 | 2 | public function isValidIban() |
|
129 | } |
||
130 |