File indexing completed on 2024-12-22 05:37:12
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Validate 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * @see Zend_Validate_Abstract 0024 */ 0025 // require_once 'Zend/Validate/Abstract.php'; 0026 0027 /** 0028 * @see Zend_Locale_Format 0029 */ 0030 // require_once 'Zend/Locale/Format.php'; 0031 0032 /** 0033 * @category Zend 0034 * @package Zend_Validate 0035 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0036 * @license http://framework.zend.com/license/new-bsd New BSD License 0037 */ 0038 class Zend_Validate_PostCode extends Zend_Validate_Abstract 0039 { 0040 const INVALID = 'postcodeInvalid'; 0041 const NO_MATCH = 'postcodeNoMatch'; 0042 0043 /** 0044 * @var array 0045 */ 0046 protected $_messageTemplates = array( 0047 self::INVALID => "Invalid type given. String or integer expected", 0048 self::NO_MATCH => "'%value%' does not appear to be a postal code", 0049 ); 0050 0051 /** 0052 * Locale to use 0053 * 0054 * @var string 0055 */ 0056 protected $_locale; 0057 0058 /** 0059 * Manual postal code format 0060 * 0061 * @var unknown_type 0062 */ 0063 protected $_format; 0064 0065 /** 0066 * Constructor for the integer validator 0067 * 0068 * Accepts either a string locale, a Zend_Locale object, or an array or 0069 * Zend_Config object containing the keys "locale" and/or "format". 0070 * 0071 * @param string|Zend_Locale|array|Zend_Config $options 0072 * @throws Zend_Validate_Exception On empty format 0073 */ 0074 public function __construct($options = null) 0075 { 0076 if ($options instanceof Zend_Config) { 0077 $options = $options->toArray(); 0078 } 0079 0080 if (empty($options)) { 0081 // require_once 'Zend/Registry.php'; 0082 if (Zend_Registry::isRegistered('Zend_Locale')) { 0083 $this->setLocale(Zend_Registry::get('Zend_Locale')); 0084 } 0085 } elseif (is_array($options)) { 0086 // Received 0087 if (array_key_exists('locale', $options)) { 0088 $this->setLocale($options['locale']); 0089 } 0090 0091 if (array_key_exists('format', $options)) { 0092 $this->setFormat($options['format']); 0093 } 0094 } elseif ($options instanceof Zend_Locale || is_string($options)) { 0095 // Received Locale object or string locale 0096 $this->setLocale($options); 0097 } 0098 0099 $format = $this->getFormat(); 0100 if (empty($format)) { 0101 // require_once 'Zend/Validate/Exception.php'; 0102 throw new Zend_Validate_Exception("A postcode-format string has to be given for validation"); 0103 } 0104 } 0105 0106 /** 0107 * Returns the set locale 0108 * 0109 * @return string|Zend_Locale The set locale 0110 */ 0111 public function getLocale() 0112 { 0113 return $this->_locale; 0114 } 0115 0116 /** 0117 * Sets the locale to use 0118 * 0119 * @param string|Zend_Locale $locale 0120 * @throws Zend_Validate_Exception On unrecognised region 0121 * @throws Zend_Validate_Exception On not detected format 0122 * @return Zend_Validate_PostCode Provides a fluent interface 0123 */ 0124 public function setLocale($locale = null) 0125 { 0126 // require_once 'Zend/Locale.php'; 0127 $this->_locale = Zend_Locale::findLocale($locale); 0128 $locale = new Zend_Locale($this->_locale); 0129 $region = $locale->getRegion(); 0130 if (empty($region)) { 0131 // require_once 'Zend/Validate/Exception.php'; 0132 throw new Zend_Validate_Exception("Unable to detect a region for the locale '$locale'"); 0133 } 0134 0135 $format = Zend_Locale::getTranslation( 0136 $locale->getRegion(), 0137 'postaltoterritory', 0138 $this->_locale 0139 ); 0140 0141 if (empty($format)) { 0142 // require_once 'Zend/Validate/Exception.php'; 0143 throw new Zend_Validate_Exception("Unable to detect a postcode format for the region '{$locale->getRegion()}'"); 0144 } 0145 0146 $this->setFormat($format); 0147 return $this; 0148 } 0149 0150 /** 0151 * Returns the set postal code format 0152 * 0153 * @return string 0154 */ 0155 public function getFormat() 0156 { 0157 return $this->_format; 0158 } 0159 0160 /** 0161 * Sets a self defined postal format as regex 0162 * 0163 * @param string $format 0164 * @throws Zend_Validate_Exception On empty format 0165 * @return Zend_Validate_PostCode Provides a fluent interface 0166 */ 0167 public function setFormat($format) 0168 { 0169 if (empty($format) || !is_string($format)) { 0170 // require_once 'Zend/Validate/Exception.php'; 0171 throw new Zend_Validate_Exception("A postcode-format string has to be given for validation"); 0172 } 0173 0174 if ($format[0] !== '/') { 0175 $format = '/^' . $format; 0176 } 0177 0178 if ($format[strlen($format) - 1] !== '/') { 0179 $format .= '$/'; 0180 } 0181 0182 $this->_format = $format; 0183 return $this; 0184 } 0185 0186 /** 0187 * Defined by Zend_Validate_Interface 0188 * 0189 * Returns true if and only if $value is a valid postalcode 0190 * 0191 * @param string $value 0192 * @return boolean 0193 */ 0194 public function isValid($value) 0195 { 0196 $this->_setValue($value); 0197 if (!is_string($value) && !is_int($value)) { 0198 $this->_error(self::INVALID); 0199 return false; 0200 } 0201 0202 $format = $this->getFormat(); 0203 if (!preg_match($format, $value)) { 0204 $this->_error(self::NO_MATCH); 0205 return false; 0206 } 0207 0208 return true; 0209 } 0210 }