00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00035 class Zend_Validate_Iban extends Zend_Validate_Abstract 00036 { 00037 const NOTSUPPORTED = 'ibanNotSupported'; 00038 const FALSEFORMAT = 'ibanFalseFormat'; 00039 const CHECKFAILED = 'ibanCheckFailed'; 00040 00046 protected $_messageTemplates = array( 00047 self::NOTSUPPORTED => "'%value%' does not have IBAN", 00048 self::FALSEFORMAT => "'%value%' has a false format", 00049 self::CHECKFAILED => "'%value%' has failed the IBAN check" 00050 ); 00051 00057 protected $_locale; 00058 00064 protected $_ibanregex = array( 00065 'AD' => '/^AD[0-9]{2}[0-9]{8}[A-Z0-9]{12}$/', 00066 'AT' => '/^AT[0-9]{2}[0-9]{5}[0-9]{11}$/', 00067 'BA' => '/^BA[0-9]{2}[0-9]{6}[0-9]{10}$/', 00068 'BE' => '/^BE[0-9]{2}[0-9]{3}[0-9]{9}$/', 00069 'BG' => '/^BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}$/', 00070 'CH' => '/^CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}$/', 00071 'CS' => '/^CS[0-9]{2}[0-9]{3}[0-9]{15}$/', 00072 'CY' => '/^CY[0-9]{2}[0-9]{8}[A-Z0-9]{16}$/', 00073 'CZ' => '/^CZ[0-9]{2}[0-9]{4}[0-9]{16}$/', 00074 'DE' => '/^DE[0-9]{2}[0-9]{8}[0-9]{10}$/', 00075 'DK' => '/^DK[0-9]{2}[0-9]{4}[0-9]{10}$/', 00076 'EE' => '/^EE[0-9]{2}[0-9]{4}[0-9]{12}$/', 00077 'ES' => '/^ES[0-9]{2}[0-9]{8}[0-9]{12}$/', 00078 'FR' => '/^FR[0-9]{2}[0-9]{10}[A-Z0-9]{13}$/', 00079 'FI' => '/^FI[0-9]{2}[0-9]{6}[0-9]{8}$/', 00080 'GB' => '/^GB[0-9]{2}[A-Z]{4}[0-9]{14}$/', 00081 'GI' => '/^GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}$/', 00082 'GR' => '/^GR[0-9]{2}[0-9]{7}[A-Z0-9]{16}$/', 00083 'HR' => '/^HR[0-9]{2}[0-9]{7}[0-9]{10}$/', 00084 'HU' => '/^HU[0-9]{2}[0-9]{7}[0-9]{1}[0-9]{15}[0-9]{1}$/', 00085 'IE' => '/^IE[0-9]{2}[A-Z0-9]{4}[0-9]{6}[0-9]{8}$/', 00086 'IS' => '/^IS[0-9]{2}[0-9]{4}[0-9]{18}$/', 00087 'IT' => '/^IT[0-9]{2}[A-Z]{1}[0-9]{10}[A-Z0-9]{12}$/', 00088 'LI' => '/^LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}$/', 00089 'LU' => '/^LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}$/', 00090 'LT' => '/^LT[0-9]{2}[0-9]{5}[0-9]{11}$/', 00091 'LV' => '/^LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}$/', 00092 'MK' => '/^MK[0-9]{2}[A-Z]{3}[A-Z0-9]{10}[0-9]{2}$/', 00093 'MT' => '/^MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}$/', 00094 'NL' => '/^NL[0-9]{2}[A-Z]{4}[0-9]{10}$/', 00095 'NO' => '/^NO[0-9]{2}[0-9]{4}[0-9]{7}$/', 00096 'PL' => '/^PL[0-9]{2}[0-9]{8}[0-9]{16}$/', 00097 'PT' => '/^PT[0-9]{2}[0-9]{8}[0-9]{13}$/', 00098 'RO' => '/^RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}$/', 00099 'SE' => '/^SE[0-9]{2}[0-9]{3}[0-9]{17}$/', 00100 'SI' => '/^SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}$/', 00101 'SK' => '/^SK[0-9]{2}[0-9]{4}[0-9]{16}$/', 00102 'TN' => '/^TN[0-9]{2}[0-9]{5}[0-9]{15}$/', 00103 'TR' => '/^TR[0-9]{2}[0-9]{5}[A-Z0-9]{17}$/' 00104 ); 00105 00112 public function __construct($locale = null) 00113 { 00114 if ($locale !== null) { 00115 $this->setLocale($locale); 00116 } 00117 } 00118 00124 public function getLocale() 00125 { 00126 return $this->_locale; 00127 } 00128 00135 public function setLocale($locale = null) 00136 { 00137 require_once 'Zend/Locale.php'; 00138 $locale = Zend_Locale::findLocale($locale); 00139 if (strlen($locale) < 4) { 00140 require_once 'Zend/Validate/Exception.php'; 00141 throw new Zend_Validate_Exception('Region must be given for IBAN validation'); 00142 } 00143 00144 $this->_locale = $locale; 00145 return $this; 00146 } 00147 00156 public function isValid($value) 00157 { 00158 $value = strtoupper($value); 00159 $this->_setValue($value); 00160 00161 if (empty($this->_locale)) { 00162 $region = substr($value, 0, 2); 00163 } else { 00164 $region = new Zend_Locale($this->_locale); 00165 $region = $region->getRegion(); 00166 } 00167 00168 if (!array_key_exists($region, $this->_ibanregex)) { 00169 $this->_setValue($region); 00170 $this->_error(self::NOTSUPPORTED); 00171 return false; 00172 } 00173 00174 if (!preg_match($this->_ibanregex[$region], $value)) { 00175 $this->_error(self::FALSEFORMAT); 00176 return false; 00177 } 00178 00179 $format = substr($value, 4) . substr($value, 0, 4); 00180 $format = str_replace( 00181 array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 00182 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'), 00183 array('10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', 00184 '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'), 00185 $format); 00186 00187 $temp = intval(substr($format, 0, 1)); 00188 $len = strlen($format); 00189 for ($x = 1; $x < $len; ++$x) { 00190 $temp *= 10; 00191 $temp += intval(substr($format, $x, 1)); 00192 $temp %= 97; 00193 } 00194 00195 if ($temp != 1) { 00196 $this->_error(self::CHECKFAILED); 00197 return false; 00198 } 00199 00200 return true; 00201 } 00202 }