OXID eShop CE  4.8.11
 All Classes Files Functions Variables Pages
oxsepaibanvalidator.php
Go to the documentation of this file.
1 <?php
2 
8 {
10 
11  protected $_aCodeLengths = array();
12 
23  public function isValid( $sIBAN )
24  {
25  $blValid = false;
26  $sIBAN = strtoupper( trim( $sIBAN ) );
27 
28  if ( $this->_isLengthValid( $sIBAN ) ) {
29  $blValid = $this->_isAlgorithmValid( $sIBAN );
30  }
31 
32  return $blValid;
33  }
34 
42  public function isValidCodeLengths( $aCodeLengths )
43  {
44  $blValid = false;
45 
46  if ( $this->_isNotEmptyArray( $aCodeLengths ) )
47  {
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 
90  protected function _isLengthValid( $sIBAN )
91  {
92  $iActualLength = getStr()->strlen( $sIBAN );
93 
94  $iCorrectLength = $this->_getLengthForCountry( $sIBAN );
95 
96  return !is_null( $iCorrectLength ) && $iActualLength === $iCorrectLength;
97  }
98 
99 
105  protected function _getLengthForCountry( $sIBAN )
106  {
107  $aIBANRegistry = $this->getCodeLengths();
108 
109  $sCountryCode = getStr()->substr( $sIBAN, 0, 2 );
110 
111  $iCorrectLength = ( isset ( $aIBANRegistry[$sCountryCode] ) ) ? $aIBANRegistry[$sCountryCode] : null;
112 
113  return $iCorrectLength;
114  }
115 
122  protected function _isAlgorithmValid( $sIBAN )
123  {
124  $sIBAN = $this->_moveInitialCharactersToEnd( $sIBAN );
125 
126  $sIBAN = $this->_replaceLettersToNumbers( $sIBAN );
127 
128  return $this->_isIBANChecksumValid( $sIBAN );
129  }
130 
137  protected function _moveInitialCharactersToEnd( $sIBAN )
138  {
139  $oStr = getStr();
140 
141  $sInitialChars = $oStr->substr( $sIBAN, 0, 4 );
142  $sIBAN = $oStr->substr( $sIBAN, 4 );
143  $sIBAN = $sIBAN . $sInitialChars;
144 
145  return $sIBAN;
146  }
147 
154  protected function _replaceLettersToNumbers( $sIBAN )
155  {
156  $aReplaceArray = array(
157  'A' => 10,
158  'B' => 11,
159  'C' => 12,
160  'D' => 13,
161  'E' => 14,
162  'F' => 15,
163  'G' => 16,
164  'H' => 17,
165  'I' => 18,
166  'J' => 19,
167  'K' => 20,
168  'L' => 21,
169  'M' => 22,
170  'N' => 23,
171  'O' => 24,
172  'P' => 25,
173  'Q' => 26,
174  'R' => 27,
175  'S' => 28,
176  'T' => 29,
177  'U' => 30,
178  'V' => 31,
179  'W' => 32,
180  'X' => 33,
181  'Y' => 34,
182  'Z' => 35
183  );
184 
185  $sIBAN = str_replace(
186  array_keys( $aReplaceArray ),
187  $aReplaceArray,
188  $sIBAN
189  );
190 
191  return $sIBAN;
192  }
193 
200  protected function _isIBANChecksumValid( $sIBAN )
201  {
202  $blValid = true;
203 
204  $sModulus = bcmod( $sIBAN, self::IBAN_ALGORITHM_MOD_VALUE );
205  if ( (int) $sModulus != 1 ) {
206  $blValid = false;
207  }
208 
209  return $blValid;
210  }
211 
218  protected function _isNotEmptyArray( $aCodeLengths )
219  {
220  return is_array( $aCodeLengths ) && !empty( $aCodeLengths );
221  }
222 
228  protected function _isEachCodeLengthValid( $aCodeLengths )
229  {
230  $blValid = true;
231 
232  foreach ( $aCodeLengths as $sCountryAbbr => $iLength ) {
233 
234  if ( !$this->_isCodeLengthKeyValid( $sCountryAbbr ) ||
235  !$this->_isCodeLengthValueValid( $iLength ) ) {
236 
237  $blValid = false;
238  break;
239  }
240 
241  }
242 
243  return $blValid;
244  }
245 
253  protected function _isCodeLengthKeyValid( $sCountryAbbr )
254  {
255  return (int) preg_match( "/^[A-Z]{2}$/", $sCountryAbbr ) !== 0;
256  }
257 
265  protected function _isCodeLengthValueValid( $iLength )
266  {
267  return is_numeric( $iLength ) && (int) preg_match( "/\./", $iLength ) !== 1;
268  }
269 
270 }