Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PostCode.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
30 #require_once 'Zend/Locale/Format.php';
31 
39 {
40  const INVALID = 'postcodeInvalid';
41  const NO_MATCH = 'postcodeNoMatch';
42 
46  protected $_messageTemplates = array(
47  self::INVALID => "Invalid type given. String or integer expected",
48  self::NO_MATCH => "'%value%' does not appear to be a postal code",
49  );
50 
56  protected $_locale;
57 
63  protected $_format;
64 
74  public function __construct($options = null)
75  {
76  if ($options instanceof Zend_Config) {
77  $options = $options->toArray();
78  }
79 
80  if (empty($options)) {
81  #require_once 'Zend/Registry.php';
82  if (Zend_Registry::isRegistered('Zend_Locale')) {
83  $this->setLocale(Zend_Registry::get('Zend_Locale'));
84  }
85  } elseif (is_array($options)) {
86  // Received
87  if (array_key_exists('locale', $options)) {
88  $this->setLocale($options['locale']);
89  }
90 
91  if (array_key_exists('format', $options)) {
92  $this->setFormat($options['format']);
93  }
94  } elseif ($options instanceof Zend_Locale || is_string($options)) {
95  // Received Locale object or string locale
96  $this->setLocale($options);
97  }
98 
99  $format = $this->getFormat();
100  if (empty($format)) {
101  #require_once 'Zend/Validate/Exception.php';
102  throw new Zend_Validate_Exception("A postcode-format string has to be given for validation");
103  }
104  }
105 
111  public function getLocale()
112  {
113  return $this->_locale;
114  }
115 
124  public function setLocale($locale = null)
125  {
126  #require_once 'Zend/Locale.php';
127  $this->_locale = Zend_Locale::findLocale($locale);
128  $locale = new Zend_Locale($this->_locale);
129  $region = $locale->getRegion();
130  if (empty($region)) {
131  #require_once 'Zend/Validate/Exception.php';
132  throw new Zend_Validate_Exception("Unable to detect a region for the locale '$locale'");
133  }
134 
136  $locale->getRegion(),
137  'postaltoterritory',
139  );
140 
141  if (empty($format)) {
142  #require_once 'Zend/Validate/Exception.php';
143  throw new Zend_Validate_Exception("Unable to detect a postcode format for the region '{$locale->getRegion()}'");
144  }
145 
146  $this->setFormat($format);
147  return $this;
148  }
149 
155  public function getFormat()
156  {
157  return $this->_format;
158  }
159 
167  public function setFormat($format)
168  {
169  if (empty($format) || !is_string($format)) {
170  #require_once 'Zend/Validate/Exception.php';
171  throw new Zend_Validate_Exception("A postcode-format string has to be given for validation");
172  }
173 
174  if ($format[0] !== '/') {
175  $format = '/^' . $format;
176  }
177 
178  if ($format[strlen($format) - 1] !== '/') {
179  $format .= '$/';
180  }
181 
182  $this->_format = $format;
183  return $this;
184  }
185 
194  public function isValid($value)
195  {
196  $this->_setValue($value);
197  if (!is_string($value) && !is_int($value)) {
198  $this->_error(self::INVALID);
199  return false;
200  }
201 
202  $format = $this->getFormat();
203  if (!preg_match($format, $value)) {
204  $this->_error(self::NO_MATCH);
205  return false;
206  }
207 
208  return true;
209  }
210 }
static getTranslation($value=null, $path=null, $locale=null)
Definition: Locale.php:1536
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct($options=null)
Definition: PostCode.php:74
const NO_MATCH
Definition: PostCode.php:41
$_messageTemplates
Definition: PostCode.php:46
Definition: PostCode.php:38
_error($messageKey, $value=null)
Definition: Abstract.php:284
isValid($value)
Definition: PostCode.php:194
static isRegistered($index)
Definition: Registry.php:178
$_locale
Definition: PostCode.php:56
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
$_format
Definition: PostCode.php:63
setFormat($format)
Definition: PostCode.php:167
static findLocale($locale=null)
Definition: Locale.php:1740
getLocale()
Definition: PostCode.php:111
const INVALID
Definition: PostCode.php:40
setLocale($locale=null)
Definition: PostCode.php:124
getFormat()
Definition: PostCode.php:155
static get($index)
Definition: Registry.php:141