oxsepaibanvalidator.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxSepaIBANValidator
00008 {
00009 
00010     const IBAN_ALGORITHM_MOD_VALUE = 97;
00011 
00012     protected $_aCodeLengths = array();
00013 
00024     public function isValid($sIBAN)
00025     {
00026         $blValid = false;
00027         $sIBAN = strtoupper(trim($sIBAN));
00028 
00029         if ($this->_isLengthValid($sIBAN)) {
00030             $blValid = $this->_isAlgorithmValid($sIBAN);
00031         }
00032 
00033         return $blValid;
00034     }
00035 
00043     public function isValidCodeLengths($aCodeLengths)
00044     {
00045         $blValid = false;
00046 
00047         if ($this->_isNotEmptyArray($aCodeLengths)) {
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 
00091     protected function _isLengthValid($sIBAN)
00092     {
00093         $iActualLength = getStr()->strlen($sIBAN);
00094 
00095         $iCorrectLength = $this->_getLengthForCountry($sIBAN);
00096 
00097         return !is_null($iCorrectLength) && $iActualLength === $iCorrectLength;
00098     }
00099 
00100 
00108     protected function _getLengthForCountry($sIBAN)
00109     {
00110         $aIBANRegistry = $this->getCodeLengths();
00111 
00112         $sCountryCode = getStr()->substr($sIBAN, 0, 2);
00113 
00114         $iCorrectLength = (isset ($aIBANRegistry[$sCountryCode])) ? $aIBANRegistry[$sCountryCode] : null;
00115 
00116         return $iCorrectLength;
00117     }
00118 
00126     protected function _isAlgorithmValid($sIBAN)
00127     {
00128         $sIBAN = $this->_moveInitialCharactersToEnd($sIBAN);
00129 
00130         $sIBAN = $this->_replaceLettersToNumbers($sIBAN);
00131 
00132         return $this->_isIBANChecksumValid($sIBAN);
00133     }
00134 
00142     protected function _moveInitialCharactersToEnd($sIBAN)
00143     {
00144         $oStr = getStr();
00145 
00146         $sInitialChars = $oStr->substr($sIBAN, 0, 4);
00147         $sIBAN = $oStr->substr($sIBAN, 4);
00148         $sIBAN = $sIBAN . $sInitialChars;
00149 
00150         return $sIBAN;
00151     }
00152 
00160     protected function _replaceLettersToNumbers($sIBAN)
00161     {
00162         $aReplaceArray = array(
00163             'A' => 10,
00164             'B' => 11,
00165             'C' => 12,
00166             'D' => 13,
00167             'E' => 14,
00168             'F' => 15,
00169             'G' => 16,
00170             'H' => 17,
00171             'I' => 18,
00172             'J' => 19,
00173             'K' => 20,
00174             'L' => 21,
00175             'M' => 22,
00176             'N' => 23,
00177             'O' => 24,
00178             'P' => 25,
00179             'Q' => 26,
00180             'R' => 27,
00181             'S' => 28,
00182             'T' => 29,
00183             'U' => 30,
00184             'V' => 31,
00185             'W' => 32,
00186             'X' => 33,
00187             'Y' => 34,
00188             'Z' => 35
00189         );
00190 
00191         $sIBAN = str_replace(
00192             array_keys($aReplaceArray),
00193             $aReplaceArray,
00194             $sIBAN
00195         );
00196 
00197         return $sIBAN;
00198     }
00199 
00207     protected function _isIBANChecksumValid($sIBAN)
00208     {
00209         $blValid = true;
00210 
00211         $sModulus = bcmod($sIBAN, self::IBAN_ALGORITHM_MOD_VALUE);
00212         if ((int) $sModulus != 1) {
00213             $blValid = false;
00214         }
00215 
00216         return $blValid;
00217     }
00218 
00226     protected function _isNotEmptyArray($aCodeLengths)
00227     {
00228         return is_array($aCodeLengths) && !empty($aCodeLengths);
00229     }
00230 
00238     protected function _isEachCodeLengthValid($aCodeLengths)
00239     {
00240         $blValid = true;
00241 
00242         foreach ($aCodeLengths as $sCountryAbbr => $iLength) {
00243 
00244             if (!$this->_isCodeLengthKeyValid($sCountryAbbr) ||
00245                 !$this->_isCodeLengthValueValid($iLength)
00246             ) {
00247 
00248                 $blValid = false;
00249                 break;
00250             }
00251 
00252         }
00253 
00254         return $blValid;
00255     }
00256 
00264     protected function _isCodeLengthKeyValid($sCountryAbbr)
00265     {
00266         return (int) preg_match("/^[A-Z]{2}$/", $sCountryAbbr) !== 0;
00267     }
00268 
00276     protected function _isCodeLengthValueValid($iLength)
00277     {
00278         return is_numeric($iLength) && (int) preg_match("/\./", $iLength) !== 1;
00279     }
00280 }