forked from hpmewes/php-iban
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIBANGenerator.php
More file actions
121 lines (106 loc) · 3.59 KB
/
IBANGenerator.php
File metadata and controls
121 lines (106 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
class IBANGenerator {
protected $bankCode;
protected $locale = 'DE';
protected $bankAccountNr;
/**
* Constructor
*
* @param type $bankCode
* @param type $bankAccountNr
* @param type $locale
*/
function __construct($bankCode, $bankAccountNr, $locale = 'DE') {
$this->locale = $locale;
$this->bankCode = $bankCode;
$this->bankAccountNr = $bankAccountNr;
}
/**
* Generate the IBAN Code
*
* @param type $bankCode
* @param type $bankAccountNr
* @param type $locale
*
* @return string
*/
public function generate($bankCode = '', $bankAccountNr = '', $locale = '') {
if(empty($locale)) $locale = $this->locale;
if(empty($bankCode)) $bankCode = $this->bankCode;
if(empty($bankAccountNr)) $bankAccountNr = $this->bankAccountNr;
$BBAN = $this->getBBAN($bankCode, $bankAccountNr);
$checksum = $this->getChecksum($bankCode, $bankAccountNr, $locale);
// ISO 7064 per Modulo 97-10
$checkcipher = $this->getCheckcipher($checksum);
// the ready IBAN ;)
return $locale.$checkcipher.$BBAN;
}
/**
* Get the Checkcipher like ISO 7064 per Modulo 97-10
* if value lower then 10 "0" append to left
*
* @param type $checksum
*
* @return string
*/
public function getCheckcipher($checksum = '') {
return str_pad(98 - bcmod($checksum, 97), 2, "0", STR_PAD_LEFT);
}
/**
* Get Checksum for IBAN
*
* @param type $bankCode
* @param type $bankAccountNr
* @param type $locale
*
* @return string
*/
public function getChecksum($bankCode = '', $bankAccountNr = '', $locale = '') {
if(empty($locale)) $locale = $this->locale;
if(empty($bankCode)) $bankCode = $this->bankCode;
if(empty($bankAccountNr)) $bankAccountNr = $this->bankAccountNr;
return $this->getBBAN($bankCode, $bankAccountNr).$this->getNumericLanguageCode($locale);
}
/**
* Get the BBAN from Bank Code and Account Identification Number
*
* @param type $bankCode
* @param type $bankAccountNr
*
* @return string
*/
public function getBBAN($bankCode = '', $bankAccountNr = '') {
if(empty($bankCode)) $bankCode = $this->bankCode;
if(empty($bankAccountNr)) $bankAccountNr = $this->bankAccountNr;
return $bankCode.str_pad($bankAccountNr, 10, "0", STR_PAD_LEFT);
}
/**
* Generate the Numeric Language Code from Locale
* add präfix 00
*
* Example: DE => 00314
*
* Every letter should be converted to it's count number (a = 1, g = 8)
* then add 9 while this is position in latin alphabet (a = 9, g = 17)
*
* @param type $locale
*
* @return string
*/
public function getNumericLanguageCode($locale = '') {
if(empty($locale)) $locale = $this->locale;
$alphabet = array(
1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E', 6 => 'F', 7 => 'G', 8 => 'H',
9 => 'I', 10 => 'J', 11 => 'K', 12 => 'L', 13 => 'M', 14 => 'N', 15 => 'O',
16 => 'P', 17 => 'Q', 18 => 'R', 19 => 'S', 20 => 'T', 21 => 'U', 22 => 'V',
23 => 'W', 24 => 'X', 25 => 'Y', 26 => 'Z'
);
$numericLanguageCode = "";
// step over each char from language code
foreach(str_split($locale) as $char) {
// use the number for latein alphabet to decode
$numericLanguageCode .= array_search($char, $alphabet) + 9;
}
return $numericLanguageCode."00";
}
}