PageRenderTime 56ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Validate/PostCode.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 210 lines | 91 code | 23 blank | 96 comment | 20 complexity | 3baac72e7780629c108dcaae76e460e9 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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: PostCode.php 22668 2010-07-25 14:50:46Z thomas $
  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. String or integer expected",
  44. self::NO_MATCH => "'%value%' does not appear to be a 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. * @throws Zend_Validate_Exception On empty format
  66. */
  67. public function __construct($options = null)
  68. {
  69. if ($options instanceof Zend_Config) {
  70. $options = $options->toArray();
  71. }
  72. if (empty($options)) {
  73. #require_once 'Zend/Registry.php';
  74. if (Zend_Registry::isRegistered('Zend_Locale')) {
  75. $this->setLocale(Zend_Registry::get('Zend_Locale'));
  76. }
  77. } elseif (is_array($options)) {
  78. // Received
  79. if (array_key_exists('locale', $options)) {
  80. $this->setLocale($options['locale']);
  81. }
  82. if (array_key_exists('format', $options)) {
  83. $this->setFormat($options['format']);
  84. }
  85. } elseif ($options instanceof Zend_Locale || is_string($options)) {
  86. // Received Locale object or string locale
  87. $this->setLocale($options);
  88. }
  89. $format = $this->getFormat();
  90. if (empty($format)) {
  91. #require_once 'Zend/Validate/Exception.php';
  92. throw new Zend_Validate_Exception("A postcode-format string has to be given for validation");
  93. }
  94. }
  95. /**
  96. * Returns the set locale
  97. *
  98. * @return string|Zend_Locale The set locale
  99. */
  100. public function getLocale()
  101. {
  102. return $this->_locale;
  103. }
  104. /**
  105. * Sets the locale to use
  106. *
  107. * @param string|Zend_Locale $locale
  108. * @throws Zend_Validate_Exception On unrecognised region
  109. * @throws Zend_Validate_Exception On not detected format
  110. * @return Zend_Validate_PostCode Provides fluid interface
  111. */
  112. public function setLocale($locale = null)
  113. {
  114. #require_once 'Zend/Locale.php';
  115. $this->_locale = Zend_Locale::findLocale($locale);
  116. $locale = new Zend_Locale($this->_locale);
  117. $region = $locale->getRegion();
  118. if (empty($region)) {
  119. #require_once 'Zend/Validate/Exception.php';
  120. throw new Zend_Validate_Exception("Unable to detect a region for the locale '$locale'");
  121. }
  122. $format = Zend_Locale::getTranslation(
  123. $locale->getRegion(),
  124. 'postaltoterritory',
  125. $this->_locale
  126. );
  127. if (empty($format)) {
  128. #require_once 'Zend/Validate/Exception.php';
  129. throw new Zend_Validate_Exception("Unable to detect a postcode format for the region '{$locale->getRegion()}'");
  130. }
  131. $this->setFormat($format);
  132. return $this;
  133. }
  134. /**
  135. * Returns the set postal code format
  136. *
  137. * @return string
  138. */
  139. public function getFormat()
  140. {
  141. return $this->_format;
  142. }
  143. /**
  144. * Sets a self defined postal format as regex
  145. *
  146. * @param string $format
  147. * @throws Zend_Validate_Exception On empty format
  148. * @return Zend_Validate_PostCode Provides fluid interface
  149. */
  150. public function setFormat($format)
  151. {
  152. if (empty($format) || !is_string($format)) {
  153. #require_once 'Zend/Validate/Exception.php';
  154. throw new Zend_Validate_Exception("A postcode-format string has to be given for validation");
  155. }
  156. if ($format[0] !== '/') {
  157. $format = '/^' . $format;
  158. }
  159. if ($format[strlen($format) - 1] !== '/') {
  160. $format .= '$/';
  161. }
  162. $this->_format = $format;
  163. return $this;
  164. }
  165. /**
  166. * Defined by Zend_Validate_Interface
  167. *
  168. * Returns true if and only if $value is a valid postalcode
  169. *
  170. * @param string $value
  171. * @return boolean
  172. */
  173. public function isValid($value)
  174. {
  175. $this->_setValue($value);
  176. if (!is_string($value) && !is_int($value)) {
  177. $this->_error(self::INVALID);
  178. return false;
  179. }
  180. $format = $this->getFormat();
  181. if (!preg_match($format, $value)) {
  182. $this->_error(self::NO_MATCH);
  183. return false;
  184. }
  185. return true;
  186. }
  187. }