Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Abstract.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Interface.php';
26 
34 {
40  protected $_value;
41 
47  protected $_messageVariables = array();
48 
54  protected $_messageTemplates = array();
55 
61  protected $_messages = array();
62 
68  protected $_obscureValue = false;
69 
76  protected $_errors = array();
77 
82  protected $_translator;
83 
88  protected static $_defaultTranslator;
89 
94  protected $_translatorDisabled = false;
95 
101  protected static $_messageLength = -1;
102 
108  public function getMessages()
109  {
110  return $this->_messages;
111  }
112 
118  public function getMessageVariables()
119  {
120  return array_keys($this->_messageVariables);
121  }
122 
128  public function getMessageTemplates()
129  {
131  }
132 
141  public function setMessage($messageString, $messageKey = null)
142  {
143  if ($messageKey === null) {
144  $keys = array_keys($this->_messageTemplates);
145  foreach($keys as $key) {
146  $this->setMessage($messageString, $key);
147  }
148  return $this;
149  }
150 
151  if (!isset($this->_messageTemplates[$messageKey])) {
152  #require_once 'Zend/Validate/Exception.php';
153  throw new Zend_Validate_Exception("No message template exists for key '$messageKey'");
154  }
155 
156  $this->_messageTemplates[$messageKey] = $messageString;
157  return $this;
158  }
159 
167  public function setMessages(array $messages)
168  {
169  foreach ($messages as $key => $message) {
170  $this->setMessage($message, $key);
171  }
172  return $this;
173  }
174 
183  public function __get($property)
184  {
185  if ($property == 'value') {
186  return $this->_value;
187  }
188  if (array_key_exists($property, $this->_messageVariables)) {
189  return $this->{$this->_messageVariables[$property]};
190  }
194  #require_once 'Zend/Validate/Exception.php';
195  throw new Zend_Validate_Exception("No property exists by the name '$property'");
196  }
197 
210  protected function _createMessage($messageKey, $value)
211  {
212  if (!isset($this->_messageTemplates[$messageKey])) {
213  return null;
214  }
215 
216  $message = $this->_messageTemplates[$messageKey];
217 
218  if (null !== ($translator = $this->getTranslator())) {
219  if ($translator->isTranslated($messageKey)) {
220  $message = $translator->translate($messageKey);
221  } else {
222  $message = $translator->translate($message);
223  }
224  }
225 
226  if (is_object($value)) {
227  if (!in_array('__toString', get_class_methods($value))) {
228  $value = get_class($value) . ' object';
229  } else {
230  $value = $value->__toString();
231  }
232  } elseif (is_array($value)) {
233  $value = $this->_implodeRecursive($value);
234  } else {
235  $value = implode((array) $value);
236  }
237 
238  if ($this->getObscureValue()) {
239  $value = str_repeat('*', strlen($value));
240  }
241 
242  $message = str_replace('%value%', $value, $message);
243  foreach ($this->_messageVariables as $ident => $property) {
244  $message = str_replace(
245  "%$ident%",
246  implode(' ', (array) $this->$property),
247  $message
248  );
249  }
250 
251  $length = self::getMessageLength();
252  if (($length > -1) && (strlen($message) > $length)) {
253  $message = substr($message, 0, (self::getMessageLength() - 3)) . '...';
254  }
255 
256  return $message;
257  }
258 
265  protected function _implodeRecursive(array $pieces)
266  {
267  $values = array();
268  foreach ($pieces as $item) {
269  if (is_array($item)) {
270  $values[] = $this->_implodeRecursive($item);
271  } else {
272  $values[] = $item;
273  }
274  }
275 
276  return implode(', ', $values);
277  }
278 
284  protected function _error($messageKey, $value = null)
285  {
286  if ($messageKey === null) {
287  $keys = array_keys($this->_messageTemplates);
288  $messageKey = current($keys);
289  }
290  if ($value === null) {
292  }
293  $this->_errors[] = $messageKey;
294  $this->_messages[$messageKey] = $this->_createMessage($messageKey, $value);
295  }
296 
303  protected function _setValue($value)
304  {
305  $this->_value = $value;
306  $this->_messages = array();
307  $this->_errors = array();
308  }
309 
316  public function getErrors()
317  {
318  return $this->_errors;
319  }
320 
327  public function setObscureValue($flag)
328  {
329  $this->_obscureValue = (bool) $flag;
330  return $this;
331  }
332 
339  public function getObscureValue()
340  {
341  return $this->_obscureValue;
342  }
343 
351  public function setTranslator($translator = null)
352  {
353  if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
354  $this->_translator = $translator;
355  } elseif ($translator instanceof Zend_Translate) {
356  $this->_translator = $translator->getAdapter();
357  } else {
358  #require_once 'Zend/Validate/Exception.php';
359  throw new Zend_Validate_Exception('Invalid translator specified');
360  }
361  return $this;
362  }
363 
369  public function getTranslator()
370  {
371  if ($this->translatorIsDisabled()) {
372  return null;
373  }
374 
375  if (null === $this->_translator) {
377  }
378 
379  return $this->_translator;
380  }
381 
387  public function hasTranslator()
388  {
389  return (bool)$this->_translator;
390  }
391 
398  public static function setDefaultTranslator($translator = null)
399  {
400  if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
401  self::$_defaultTranslator = $translator;
402  } elseif ($translator instanceof Zend_Translate) {
403  self::$_defaultTranslator = $translator->getAdapter();
404  } else {
405  #require_once 'Zend/Validate/Exception.php';
406  throw new Zend_Validate_Exception('Invalid translator specified');
407  }
408  }
409 
415  public static function getDefaultTranslator()
416  {
417  if (null === self::$_defaultTranslator) {
418  #require_once 'Zend/Registry.php';
419  if (Zend_Registry::isRegistered('Zend_Translate')) {
420  $translator = Zend_Registry::get('Zend_Translate');
421  if ($translator instanceof Zend_Translate_Adapter) {
422  return $translator;
423  } elseif ($translator instanceof Zend_Translate) {
424  return $translator->getAdapter();
425  }
426  }
427  }
428 
430  }
431 
437  public static function hasDefaultTranslator()
438  {
439  return (bool)self::$_defaultTranslator;
440  }
441 
448  public function setDisableTranslator($flag)
449  {
450  $this->_translatorDisabled = (bool) $flag;
451  return $this;
452  }
453 
459  public function translatorIsDisabled()
460  {
462  }
463 
469  public static function getMessageLength()
470  {
471  return self::$_messageLength;
472  }
473 
479  public static function setMessageLength($length = -1)
480  {
481  self::$_messageLength = $length;
482  }
483 }
_implodeRecursive(array $pieces)
Definition: Abstract.php:265
static setDefaultTranslator($translator=null)
Definition: Abstract.php:398
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$values
Definition: options.phtml:88
static setMessageLength($length=-1)
Definition: Abstract.php:479
$message
static hasDefaultTranslator()
Definition: Abstract.php:437
_error($messageKey, $value=null)
Definition: Abstract.php:284
static getMessageLength()
Definition: Abstract.php:469
static isRegistered($index)
Definition: Registry.php:178
static getDefaultTranslator()
Definition: Abstract.php:415
$value
Definition: gender.phtml:16
setTranslator($translator=null)
Definition: Abstract.php:351
setMessages(array $messages)
Definition: Abstract.php:167
setMessage($messageString, $messageKey=null)
Definition: Abstract.php:141
static $_defaultTranslator
Definition: Abstract.php:88
_createMessage($messageKey, $value)
Definition: Abstract.php:210
static get($index)
Definition: Registry.php:141