|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00030 require_once 'Zend/Locale/Format.php'; 00031 00038 class Zend_Validate_PostCode extends Zend_Validate_Abstract 00039 { 00040 const INVALID = 'postcodeInvalid'; 00041 const NO_MATCH = 'postcodeNoMatch'; 00042 00046 protected $_messageTemplates = array( 00047 self::INVALID => "Invalid type given. The value should be a string or a integer", 00048 self::NO_MATCH => "'%value%' does not appear to be a postal code", 00049 ); 00050 00056 protected $_locale; 00057 00063 protected $_format; 00064 00074 public function __construct($options = null) 00075 { 00076 if ($options instanceof Zend_Config) { 00077 $options = $options->toArray(); 00078 } 00079 00080 if (empty($options)) { 00081 require_once 'Zend/Registry.php'; 00082 if (Zend_Registry::isRegistered('Zend_Locale')) { 00083 $this->setLocale(Zend_Registry::get('Zend_Locale')); 00084 } 00085 } elseif (is_array($options)) { 00086 // Received 00087 if (array_key_exists('locale', $options)) { 00088 $this->setLocale($options['locale']); 00089 } 00090 00091 if (array_key_exists('format', $options)) { 00092 $this->setFormat($options['format']); 00093 } 00094 } elseif ($options instanceof Zend_Locale || is_string($options)) { 00095 // Received Locale object or string locale 00096 $this->setLocale($options); 00097 } 00098 00099 $format = $this->getFormat(); 00100 if (empty($format)) { 00101 require_once 'Zend/Validate/Exception.php'; 00102 throw new Zend_Validate_Exception("A postcode-format string has to be given for validation"); 00103 } 00104 } 00105 00111 public function getLocale() 00112 { 00113 return $this->_locale; 00114 } 00115 00124 public function setLocale($locale = null) 00125 { 00126 require_once 'Zend/Locale.php'; 00127 $this->_locale = Zend_Locale::findLocale($locale); 00128 $locale = new Zend_Locale($this->_locale); 00129 $region = $locale->getRegion(); 00130 if (empty($region)) { 00131 require_once 'Zend/Validate/Exception.php'; 00132 throw new Zend_Validate_Exception("Unable to detect a region for the locale '$locale'"); 00133 } 00134 00135 $format = Zend_Locale::getTranslation( 00136 $locale->getRegion(), 00137 'postaltoterritory', 00138 $this->_locale 00139 ); 00140 00141 if (empty($format)) { 00142 require_once 'Zend/Validate/Exception.php'; 00143 throw new Zend_Validate_Exception("Unable to detect a postcode format for the region '{$locale->getRegion()}'"); 00144 } 00145 00146 $this->setFormat($format); 00147 return $this; 00148 } 00149 00155 public function getFormat() 00156 { 00157 return $this->_format; 00158 } 00159 00167 public function setFormat($format) 00168 { 00169 if (empty($format) || !is_string($format)) { 00170 require_once 'Zend/Validate/Exception.php'; 00171 throw new Zend_Validate_Exception("A postcode-format string has to be given for validation"); 00172 } 00173 00174 if ($format[0] !== '/') { 00175 $format = '/^' . $format; 00176 } 00177 00178 if ($format[strlen($format) - 1] !== '/') { 00179 $format .= '$/'; 00180 } 00181 00182 $this->_format = $format; 00183 return $this; 00184 } 00185 00194 public function isValid($value) 00195 { 00196 $this->_setValue($value); 00197 if (!is_string($value) && !is_int($value)) { 00198 $this->_error(self::INVALID); 00199 return false; 00200 } 00201 00202 $format = $this->getFormat(); 00203 if (!preg_match($format, $value)) { 00204 $this->_error(self::NO_MATCH); 00205 return false; 00206 } 00207 00208 return true; 00209 } 00210 }