|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace byrokrat\banking; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Standard account number implementation |
|
7
|
|
|
*/ |
|
8
|
|
|
class BaseAccount implements AccountNumber |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string The name of the Bank this number belongs to |
|
12
|
|
|
*/ |
|
13
|
|
|
private $bankName; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string The raw parsed number |
|
17
|
|
|
*/ |
|
18
|
|
|
private $raw; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string Account clearing number |
|
22
|
|
|
*/ |
|
23
|
|
|
private $clearing; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string Check digit of the clearing number |
|
27
|
|
|
*/ |
|
28
|
|
|
private $clearingCheckDigit; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string Account serial number |
|
32
|
|
|
*/ |
|
33
|
|
|
private $serial; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string Check digit |
|
37
|
|
|
*/ |
|
38
|
|
|
private $checkDigit; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Load account number data |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $bankName |
|
44
|
|
|
* @param string $raw |
|
45
|
|
|
* @param string $clearing |
|
46
|
|
|
* @param string $clearingCheckDigit |
|
47
|
|
|
* @param string $serial |
|
48
|
|
|
* @param string $checkDigit |
|
49
|
|
|
*/ |
|
50
|
180 |
|
public function __construct($bankName, $raw, $clearing, $clearingCheckDigit, $serial, $checkDigit) |
|
51
|
|
|
{ |
|
52
|
180 |
|
$this->bankName = $bankName; |
|
53
|
180 |
|
$this->raw = $raw; |
|
54
|
180 |
|
$this->clearing = $clearing; |
|
55
|
180 |
|
$this->clearingCheckDigit = $clearingCheckDigit; |
|
56
|
180 |
|
$this->serial = $serial; |
|
57
|
180 |
|
$this->checkDigit = $checkDigit; |
|
58
|
180 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get name of the Bank this number belongs to |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
101 |
|
public function getBankName() |
|
66
|
|
|
{ |
|
67
|
101 |
|
return $this->bankName; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the raw number |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
38 |
|
public function getRawNumber() |
|
76
|
|
|
{ |
|
77
|
38 |
|
return $this->raw; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get account as string |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
162 |
|
public function getNumber() |
|
86
|
|
|
{ |
|
87
|
162 |
|
return sprintf( |
|
88
|
162 |
|
'%s%s,%s-%s', |
|
89
|
162 |
|
$this->getClearingNumber(), |
|
90
|
162 |
|
$this->getClearingCheckDigit() !== '' ? '-' . $this->getClearingCheckDigit() : '', |
|
91
|
162 |
|
trim(chunk_split($this->getSerialNumber(), 3, ' ')), |
|
92
|
162 |
|
$this->getCheckDigit() |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get account as string |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
172 |
|
public function __toString() |
|
102
|
|
|
{ |
|
103
|
172 |
|
return $this->getNumber(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get clearing number, 4 digits |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
170 |
|
public function getClearingNumber() |
|
112
|
|
|
{ |
|
113
|
170 |
|
return $this->clearing; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get the check digit of the clearing number |
|
118
|
|
|
* |
|
119
|
|
|
* @return string Empty if not applicable |
|
120
|
|
|
*/ |
|
121
|
167 |
|
public function getClearingCheckDigit() |
|
122
|
|
|
{ |
|
123
|
167 |
|
return $this->clearingCheckDigit; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get account serial number |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
180 |
|
public function getSerialNumber() |
|
132
|
|
|
{ |
|
133
|
180 |
|
return $this->serial; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get account check digit |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
180 |
|
public function getCheckDigit() |
|
142
|
|
|
{ |
|
143
|
180 |
|
return $this->checkDigit; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get account as a 16 digit number |
|
148
|
|
|
* |
|
149
|
|
|
* Clearing number (4 digits) + x number of ceros + serial number |
|
150
|
|
|
* |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
100 |
|
public function get16() |
|
154
|
|
|
{ |
|
155
|
100 |
|
return $this->getClearingNumber() |
|
156
|
100 |
|
. str_pad($this->getSerialNumber(), 11, '0', STR_PAD_LEFT) |
|
157
|
100 |
|
. $this->getCheckDigit(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Check if account is considered equal to this account |
|
162
|
|
|
* |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
100 |
|
public function equals(AccountNumber $account, $strict = false) |
|
166
|
|
|
{ |
|
167
|
100 |
|
return $this->getBankName() == $account->getBankName() |
|
168
|
100 |
|
&& $this->getClearingNumber() == $account->getClearingNumber() |
|
169
|
100 |
|
&& $this->getSerialNumber() == $account->getSerialNumber() |
|
170
|
100 |
|
&& $this->getCheckDigit() == $account->getCheckDigit() |
|
171
|
100 |
|
&& !(($this->getClearingCheckDigit() xor $account->getClearingCheckDigit()) && $strict) |
|
172
|
|
|
&& ( |
|
173
|
100 |
|
!$this->getClearingCheckDigit() |
|
174
|
2 |
|
|| !$account->getClearingCheckDigit() |
|
175
|
100 |
|
|| $this->getClearingCheckDigit() == $account->getClearingCheckDigit() |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|