Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Validate/Iban.php
Go to the documentation of this file.
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 => "Unknown country within the IBAN '%value%'",
00048         self::FALSEFORMAT  => "'%value%' has a false IBAN 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 instanceof Zend_Config) {
00115             $locale = $locale->toArray();
00116         }
00117 
00118         if (is_array($locale)) {
00119             if (array_key_exists('locale', $locale)) {
00120                 $locale = $locale['locale'];
00121             } else {
00122                 $locale = null;
00123             }
00124         }
00125 
00126         if (empty($locale)) {
00127             require_once 'Zend/Registry.php';
00128             if (Zend_Registry::isRegistered('Zend_Locale')) {
00129                 $locale = Zend_Registry::get('Zend_Locale');
00130             }
00131         }
00132 
00133         if ($locale !== null) {
00134             $this->setLocale($locale);
00135         }
00136     }
00137 
00143     public function getLocale()
00144     {
00145         return $this->_locale;
00146     }
00147 
00154     public function setLocale($locale = null)
00155     {
00156         if ($locale !== false) {
00157             require_once 'Zend/Locale.php';
00158             $locale = Zend_Locale::findLocale($locale);
00159             if (strlen($locale) < 4) {
00160                 require_once 'Zend/Validate/Exception.php';
00161                 throw new Zend_Validate_Exception('Region must be given for IBAN validation');
00162             }
00163         }
00164 
00165         $this->_locale = $locale;
00166         return $this;
00167     }
00168 
00177     public function isValid($value)
00178     {
00179         $value = strtoupper($value);
00180         $this->_setValue($value);
00181 
00182         if (empty($this->_locale)) {
00183             $region = substr($value, 0, 2);
00184         } else {
00185             $region = new Zend_Locale($this->_locale);
00186             $region = $region->getRegion();
00187         }
00188 
00189         if (!array_key_exists($region, $this->_ibanregex)) {
00190             $this->_setValue($region);
00191             $this->_error(self::NOTSUPPORTED);
00192             return false;
00193         }
00194 
00195         if (!preg_match($this->_ibanregex[$region], $value)) {
00196             $this->_error(self::FALSEFORMAT);
00197             return false;
00198         }
00199 
00200         $format = substr($value, 4) . substr($value, 0, 4);
00201         $format = str_replace(
00202             array('A',  'B',  'C',  'D',  'E',  'F',  'G',  'H',  'I',  'J',  'K',  'L',  'M',
00203                   'N',  'O',  'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',  'X',  'Y',  'Z'),
00204             array('10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22',
00205                   '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'),
00206             $format);
00207 
00208         $temp = intval(substr($format, 0, 1));
00209         $len  = strlen($format);
00210         for ($x = 1; $x < $len; ++$x) {
00211             $temp *= 10;
00212             $temp += intval(substr($format, $x, 1));
00213             $temp %= 97;
00214         }
00215 
00216         if ($temp != 1) {
00217             $this->_error(self::CHECKFAILED);
00218             return false;
00219         }
00220 
00221         return true;
00222     }
00223 }
 All Data Structures Namespaces Files Functions Variables Enumerations