OXID eShop CE  4.10.2
 All Classes Namespaces Files Functions Variables Pages
oxsepaibanvalidator.php
Go to the documentation of this file.
1 <?php
2 
8 {
9 
11 
12  protected $_aCodeLengths = array();
13 
24  public function isValid($sIBAN)
25  {
26  $blValid = false;
27  $sIBAN = strtoupper(trim($sIBAN));
28 
29  if ($this->_isLengthValid($sIBAN)) {
30  $blValid = $this->_isAlgorithmValid($sIBAN);
31  }
32 
33  return $blValid;
34  }
35 
43  public function isValidCodeLengths($aCodeLengths)
44  {
45  $blValid = false;
46 
47  if ($this->_isNotEmptyArray($aCodeLengths)) {
48  $blValid = $this->_isEachCodeLengthValid($aCodeLengths);
49  }
50 
51  return $blValid;
52  }
53 
61  public function setCodeLengths($aCodeLengths)
62  {
63  if ($this->isValidCodeLengths($aCodeLengths)) {
64  $this->_aCodeLengths = $aCodeLengths;
65 
66  return true;
67  } else {
68  return false;
69  }
70 
71  }
72 
78  public function getCodeLengths()
79  {
80  return $this->_aCodeLengths;
81  }
82 
83 
91  protected function _isLengthValid($sIBAN)
92  {
93  $iActualLength = getStr()->strlen($sIBAN);
94 
95  $iCorrectLength = $this->_getLengthForCountry($sIBAN);
96 
97  return !is_null($iCorrectLength) && $iActualLength === $iCorrectLength;
98  }
99 
100 
108  protected function _getLengthForCountry($sIBAN)
109  {
110  $aIBANRegistry = $this->getCodeLengths();
111 
112  $sCountryCode = getStr()->substr($sIBAN, 0, 2);
113 
114  $iCorrectLength = (isset ($aIBANRegistry[$sCountryCode])) ? $aIBANRegistry[$sCountryCode] : null;
115 
116  return $iCorrectLength;
117  }
118 
126  protected function _isAlgorithmValid($sIBAN)
127  {
128  $sIBAN = $this->_moveInitialCharactersToEnd($sIBAN);
129 
130  $sIBAN = $this->_replaceLettersToNumbers($sIBAN);
131 
132  return $this->_isIBANChecksumValid($sIBAN);
133  }
134 
142  protected function _moveInitialCharactersToEnd($sIBAN)
143  {
144  $oStr = getStr();
145 
146  $sInitialChars = $oStr->substr($sIBAN, 0, 4);
147  $sIBAN = $oStr->substr($sIBAN, 4);
148  $sIBAN = $sIBAN . $sInitialChars;
149 
150  return $sIBAN;
151  }
152 
160  protected function _replaceLettersToNumbers($sIBAN)
161  {
162  $aReplaceArray = array(
163  'A' => 10,
164  'B' => 11,
165  'C' => 12,
166  'D' => 13,
167  'E' => 14,
168  'F' => 15,
169  'G' => 16,
170  'H' => 17,
171  'I' => 18,
172  'J' => 19,
173  'K' => 20,
174  'L' => 21,
175  'M' => 22,
176  'N' => 23,
177  'O' => 24,
178  'P' => 25,
179  'Q' => 26,
180  'R' => 27,
181  'S' => 28,
182  'T' => 29,
183  'U' => 30,
184  'V' => 31,
185  'W' => 32,
186  'X' => 33,
187  'Y' => 34,
188  'Z' => 35
189  );
190 
191  $sIBAN = str_replace(
192  array_keys($aReplaceArray),
193  $aReplaceArray,
194  $sIBAN
195  );
196 
197  return $sIBAN;
198  }
199 
207  protected function _isIBANChecksumValid($sIBAN)
208  {
209  $blValid = true;
210 
211  $sModulus = bcmod($sIBAN, self::IBAN_ALGORITHM_MOD_VALUE);
212  if ((int) $sModulus != 1) {
213  $blValid = false;
214  }
215 
216  return $blValid;
217  }
218 
226  protected function _isNotEmptyArray($aCodeLengths)
227  {
228  return is_array($aCodeLengths) && !empty($aCodeLengths);
229  }
230 
238  protected function _isEachCodeLengthValid($aCodeLengths)
239  {
240  $blValid = true;
241 
242  foreach ($aCodeLengths as $sCountryAbbr => $iLength) {
243 
244  if (!$this->_isCodeLengthKeyValid($sCountryAbbr) ||
245  !$this->_isCodeLengthValueValid($iLength)
246  ) {
247 
248  $blValid = false;
249  break;
250  }
251 
252  }
253 
254  return $blValid;
255  }
256 
264  protected function _isCodeLengthKeyValid($sCountryAbbr)
265  {
266  return (int) preg_match("/^[A-Z]{2}$/", $sCountryAbbr) !== 0;
267  }
268 
276  protected function _isCodeLengthValueValid($iLength)
277  {
278  return is_numeric($iLength) && (int) preg_match("/\./", $iLength) !== 1;
279  }
280 }