Go to the documentation of this file.00001 <?php
00002
00007 class oxSepaIBANValidator
00008 {
00009 const IBAN_ALGORITHM_MOD_VALUE = 97;
00010
00011 protected $_aCodeLengths = array();
00012
00023 public function isValid( $sIBAN )
00024 {
00025 $blValid = false;
00026 $sIBAN = strtoupper( trim( $sIBAN ) );
00027
00028 if ( $this->_isLengthValid( $sIBAN ) ) {
00029 $blValid = $this->_isAlgorithmValid( $sIBAN );
00030 }
00031
00032 return $blValid;
00033 }
00034
00042 public function isValidCodeLengths( $aCodeLengths )
00043 {
00044 $blValid = false;
00045
00046 if ( $this->_isNotEmptyArray( $aCodeLengths ) )
00047 {
00048 $blValid = $this->_isEachCodeLengthValid( $aCodeLengths );
00049 }
00050
00051 return $blValid;
00052 }
00053
00061 public function setCodeLengths( $aCodeLengths )
00062 {
00063 if ( $this->isValidCodeLengths( $aCodeLengths ) ) {
00064 $this->_aCodeLengths = $aCodeLengths;
00065
00066 return true;
00067 } else {
00068 return false;
00069 }
00070
00071 }
00072
00078 public function getCodeLengths()
00079 {
00080 return $this->_aCodeLengths;
00081 }
00082
00083
00090 protected function _isLengthValid( $sIBAN )
00091 {
00092 $iActualLength = getStr()->strlen( $sIBAN );
00093
00094 $iCorrectLength = $this->_getLengthForCountry( $sIBAN );
00095
00096 return !is_null( $iCorrectLength ) && $iActualLength === $iCorrectLength;
00097 }
00098
00099
00105 protected function _getLengthForCountry( $sIBAN )
00106 {
00107 $aIBANRegistry = $this->getCodeLengths();
00108
00109 $sCountryCode = getStr()->substr( $sIBAN, 0, 2 );
00110
00111 $iCorrectLength = ( isset ( $aIBANRegistry[$sCountryCode] ) ) ? $aIBANRegistry[$sCountryCode] : null;
00112
00113 return $iCorrectLength;
00114 }
00115
00122 protected function _isAlgorithmValid( $sIBAN )
00123 {
00124 $sIBAN = $this->_moveInitialCharactersToEnd( $sIBAN );
00125
00126 $sIBAN = $this->_replaceLettersToNumbers( $sIBAN );
00127
00128 return $this->_isIBANChecksumValid( $sIBAN );
00129 }
00130
00137 protected function _moveInitialCharactersToEnd( $sIBAN )
00138 {
00139 $oStr = getStr();
00140
00141 $sInitialChars = $oStr->substr( $sIBAN, 0, 4 );
00142 $sIBAN = $oStr->substr( $sIBAN, 4 );
00143 $sIBAN = $sIBAN . $sInitialChars;
00144
00145 return $sIBAN;
00146 }
00147
00154 protected function _replaceLettersToNumbers( $sIBAN )
00155 {
00156 $aReplaceArray = array(
00157 'A' => 10,
00158 'B' => 11,
00159 'C' => 12,
00160 'D' => 13,
00161 'E' => 14,
00162 'F' => 15,
00163 'G' => 16,
00164 'H' => 17,
00165 'I' => 18,
00166 'J' => 19,
00167 'K' => 20,
00168 'L' => 21,
00169 'M' => 22,
00170 'N' => 23,
00171 'O' => 24,
00172 'P' => 25,
00173 'Q' => 26,
00174 'R' => 27,
00175 'S' => 28,
00176 'T' => 29,
00177 'U' => 30,
00178 'V' => 31,
00179 'W' => 32,
00180 'X' => 33,
00181 'Y' => 34,
00182 'Z' => 35
00183 );
00184
00185 $sIBAN = str_replace(
00186 array_keys( $aReplaceArray ),
00187 $aReplaceArray,
00188 $sIBAN
00189 );
00190
00191 return $sIBAN;
00192 }
00193
00200 protected function _isIBANChecksumValid( $sIBAN )
00201 {
00202 $blValid = true;
00203
00204 $sModulus = bcmod( $sIBAN, self::IBAN_ALGORITHM_MOD_VALUE );
00205 if ( (int) $sModulus != 1 ) {
00206 $blValid = false;
00207 }
00208
00209 return $blValid;
00210 }
00211
00218 protected function _isNotEmptyArray( $aCodeLengths )
00219 {
00220 return is_array( $aCodeLengths ) && !empty( $aCodeLengths );
00221 }
00222
00228 protected function _isEachCodeLengthValid( $aCodeLengths )
00229 {
00230 $blValid = true;
00231
00232 foreach ( $aCodeLengths as $sCountryAbbr => $iLength ) {
00233
00234 if ( !$this->_isCodeLengthKeyValid( $sCountryAbbr ) ||
00235 !$this->_isCodeLengthValueValid( $iLength ) ) {
00236
00237 $blValid = false;
00238 break;
00239 }
00240
00241 }
00242
00243 return $blValid;
00244 }
00245
00253 protected function _isCodeLengthKeyValid( $sCountryAbbr )
00254 {
00255 return (int) preg_match( "/^[A-Z]{2}$/", $sCountryAbbr ) !== 0;
00256 }
00257
00265 protected function _isCodeLengthValueValid( $iLength )
00266 {
00267 return is_numeric( $iLength ) && (int) preg_match( "/\./", $iLength ) !== 1;
00268 }
00269
00270 }