PostCode.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @see Zend_Locale_Format
  27. */
  28. require_once 'Zend/Locale/Format.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Validate
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Validate_PostCode extends Zend_Validate_Abstract
  36. {
  37. const INVALID = 'postcodeInvalid';
  38. const NO_MATCH = 'postcodeNoMatch';
  39. /**
  40. * @var array
  41. */
  42. protected $_messageTemplates = array(
  43. self::INVALID => "Invalid type given, value should be a string or a integer",
  44. self::NO_MATCH => "'%value%' does not appear to be an postal code"
  45. );
  46. /**
  47. * Locale to use
  48. *
  49. * @var string
  50. */
  51. protected $_locale;
  52. /**
  53. * Manual postal code format
  54. *
  55. * @var unknown_type
  56. */
  57. protected $_format;
  58. /**
  59. * Constructor for the integer validator
  60. *
  61. * Accepts either a string locale, a Zend_Locale object, or an array or
  62. * Zend_Config object containing the keys "locale" and/or "format".
  63. *
  64. * @param string|Zend_Locale|array|Zend_Config $options
  65. */
  66. public function __construct($options)
  67. {
  68. if ($options instanceof Zend_Config) {
  69. $options = $options->toArray();
  70. }
  71. if (null === $options) {
  72. require_once 'Zend/Registry.php';
  73. if (Zend_Registry::isRegistered('Zend_Locale')) {
  74. $this->setLocale(Zend_Registry::get('Zend_Locale'));
  75. }
  76. } elseif (is_array($options)) {
  77. // Received
  78. if (array_key_exists('locale', $options)) {
  79. $this->setLocale($options['locale']);
  80. }
  81. if (array_key_exists('format', $options)) {
  82. $this->setFormat($options['format']);
  83. }
  84. } elseif ($options instanceof Zend_Locale || is_string($options)) {
  85. // Received Locale object or string locale
  86. $this->setLocale($options);
  87. }
  88. $format = $this->getFormat();
  89. if (empty($format)) {
  90. require_once 'Zend/Validate/Exception.php';
  91. throw new Zend_Validate_Exception("Format has to be a not empty string");
  92. }
  93. }
  94. /**
  95. * Returns the set locale
  96. */
  97. public function getLocale()
  98. {
  99. return $this->_locale;
  100. }
  101. /**
  102. * Sets the locale to use
  103. *
  104. * @param string|Zend_Locale $locale
  105. */
  106. public function setLocale($locale = null)
  107. {
  108. require_once 'Zend/Locale.php';
  109. $this->_locale = Zend_Locale::findLocale($locale);
  110. $locale = new Zend_Locale($this->_locale);
  111. $region = $locale->getRegion();
  112. if (empty($region)) {
  113. require_once 'Zend/Validate/Exception.php';
  114. throw new Zend_Validate_Exception("Unable to detect a region from the locale '$locale'");
  115. }
  116. $format = Zend_Locale::getTranslation(
  117. $locale->getRegion(),
  118. 'postaltoterritory',
  119. $this->_locale
  120. );
  121. if (empty($format)) {
  122. require_once 'Zend/Validate/Exception.php';
  123. throw new Zend_Validate_Exception("Unable to detect a format from the region '{$locale->getRegion()}'");
  124. }
  125. $this->setFormat($format);
  126. return $this;
  127. }
  128. /**
  129. * Returns the set postal code format
  130. *
  131. * @return string
  132. */
  133. public function getFormat()
  134. {
  135. return $this->_format;
  136. }
  137. /**
  138. * Sets a self defined postal format as regex
  139. *
  140. * @param string $format
  141. */
  142. public function setFormat($format)
  143. {
  144. if (empty($format) || !is_string($format)) {
  145. require_once 'Zend/Validate/Exception.php';
  146. throw new Zend_Validate_Exception("Format has to be a not empty string");
  147. }
  148. if ($format[0] !== '/') {
  149. $format = '/^' . $format;
  150. }
  151. if ($format[strlen($format) - 1] !== '/') {
  152. $format .= '$/';
  153. }
  154. $this->_format = $format;
  155. }
  156. /**
  157. * Defined by Zend_Validate_Interface
  158. *
  159. * Returns true if and only if $value is a valid postalcode
  160. *
  161. * @param string $value
  162. * @return boolean
  163. */
  164. public function isValid($value)
  165. {
  166. if (!is_string($value) && !is_int($value)) {
  167. $this->_error(self::INVALID);
  168. return false;
  169. }
  170. $format = $this->getFormat();
  171. if (!preg_match($format, $value)) {
  172. $this->_error(self::NO_MATCH);
  173. return false;
  174. }
  175. return true;
  176. }
  177. }