Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StringLength.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
34 {
35  const INVALID = 'stringLengthInvalid';
36  const TOO_SHORT = 'stringLengthTooShort';
37  const TOO_LONG = 'stringLengthTooLong';
38 
42  protected $_messageTemplates = array(
43  self::INVALID => "Invalid type given. String expected",
44  self::TOO_SHORT => "'%value%' is less than %min% characters long",
45  self::TOO_LONG => "'%value%' is more than %max% characters long",
46  );
47 
51  protected $_messageVariables = array(
52  'min' => '_min',
53  'max' => '_max'
54  );
55 
61  protected $_min;
62 
70  protected $_max;
71 
77  protected $_encoding;
78 
84  public function __construct($options = array())
85  {
86  if ($options instanceof Zend_Config) {
87  $options = $options->toArray();
88  } else if (!is_array($options)) {
89  $options = func_get_args();
90  $temp['min'] = array_shift($options);
91  if (!empty($options)) {
92  $temp['max'] = array_shift($options);
93  }
94 
95  if (!empty($options)) {
96  $temp['encoding'] = array_shift($options);
97  }
98 
99  $options = $temp;
100  }
101 
102  if (!array_key_exists('min', $options)) {
103  $options['min'] = 0;
104  }
105 
106  $this->setMin($options['min']);
107  if (array_key_exists('max', $options)) {
108  $this->setMax($options['max']);
109  }
110 
111  if (array_key_exists('encoding', $options)) {
112  $this->setEncoding($options['encoding']);
113  }
114  }
115 
121  public function getMin()
122  {
123  return $this->_min;
124  }
125 
133  public function setMin($min)
134  {
135  if (null !== $this->_max && $min > $this->_max) {
139  #require_once 'Zend/Validate/Exception.php';
140  throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >"
141  . " $this->_max");
142  }
143  $this->_min = max(0, (integer) $min);
144  return $this;
145  }
146 
152  public function getMax()
153  {
154  return $this->_max;
155  }
156 
164  public function setMax($max)
165  {
166  if (null === $max) {
167  $this->_max = null;
168  } else if ($max < $this->_min) {
172  #require_once 'Zend/Validate/Exception.php';
173  throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but "
174  . "$max < $this->_min");
175  } else {
176  $this->_max = (integer) $max;
177  }
178 
179  return $this;
180  }
181 
187  public function getEncoding()
188  {
189  return $this->_encoding;
190  }
191 
199  public function setEncoding($encoding = null)
200  {
201  if ($encoding !== null) {
202  $orig = PHP_VERSION_ID < 50600
203  ? iconv_get_encoding('internal_encoding')
204  : ini_get('default_charset');
205  if (PHP_VERSION_ID < 50600) {
206  if ($encoding) {
207  $result = iconv_set_encoding('internal_encoding', $encoding);
208  } else {
209  $result = false;
210  }
211  } else {
212  ini_set('default_charset', $encoding);
213  $result = ini_get('default_charset');
214  }
215  if ($result === false) {
216  #require_once 'Zend/Validate/Exception.php';
217  throw new Zend_Validate_Exception('Given encoding not supported on this OS!');
218  }
219 
220  if (PHP_VERSION_ID < 50600) {
221  iconv_set_encoding('internal_encoding', $orig);
222  } else {
223  ini_set('default_charset', $orig);
224  }
225  }
226  $this->_encoding = $encoding;
227  return $this;
228  }
229 
239  public function isValid($value)
240  {
241  if (!is_string($value)) {
242  $this->_error(self::INVALID);
243  return false;
244  }
245 
246  $this->_setValue($value);
247  if ($this->_encoding !== null) {
248  $length = iconv_strlen($value, $this->_encoding);
249  } else {
250  $length = iconv_strlen($value);
251  }
252 
253  if ($length < $this->_min) {
254  $this->_error(self::TOO_SHORT);
255  }
256 
257  if (null !== $this->_max && $this->_max < $length) {
258  $this->_error(self::TOO_LONG);
259  }
260 
261  if (count($this->_messages)) {
262  return false;
263  } else {
264  return true;
265  }
266  }
267 }
ini_set($varName, $newValue)
setEncoding($encoding=null)
_error($messageKey, $value=null)
Definition: Abstract.php:284
__construct($options=array())
$value
Definition: gender.phtml:16