Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Callback.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
34 {
38  const INVALID_CALLBACK = 'callbackInvalid';
39 
43  const INVALID_VALUE = 'callbackValue';
44 
50  protected $_messageTemplates = array(
51  self::INVALID_VALUE => "'%value%' is not valid",
52  self::INVALID_CALLBACK => "An exception has been raised within the callback",
53  );
54 
60  protected $_callback = null;
61 
67  protected $_options = array();
68 
75  public function __construct($callback = null)
76  {
77  if (is_callable($callback)) {
78  $this->setCallback($callback);
79  } elseif (is_array($callback)) {
80  if (isset($callback['callback'])) {
81  $this->setCallback($callback['callback']);
82  }
83  if (isset($callback['options'])) {
84  $this->setOptions($callback['options']);
85  }
86  }
87 
88  if (null === ($initializedCallack = $this->getCallback())) {
89  #require_once 'Zend/Validate/Exception.php';
90  throw new Zend_Validate_Exception('No callback registered');
91  }
92  }
93 
99  public function getCallback()
100  {
101  return $this->_callback;
102  }
103 
111  public function setCallback($callback)
112  {
113  if (!is_callable($callback)) {
114  #require_once 'Zend/Validate/Exception.php';
115  throw new Zend_Validate_Exception('Invalid callback given');
116  }
117  $this->_callback = $callback;
118  return $this;
119  }
120 
126  public function getOptions()
127  {
128  return $this->_options;
129  }
130 
137  public function setOptions($options)
138  {
139  $this->_options = (array) $options;
140  return $this;
141  }
142 
152  public function isValid($value)
153  {
154  $this->_setValue($value);
155 
156  $options = $this->getOptions();
157  $callback = $this->getCallback();
158  $args = func_get_args();
159  $options = array_merge($args, $options);
160 
161  try {
162  if (!call_user_func_array($callback, $options)) {
163  $this->_error(self::INVALID_VALUE);
164  return false;
165  }
166  } catch (Exception $e) {
167  $this->_error(self::INVALID_CALLBACK);
168  return false;
169  }
170 
171  return true;
172  }
173 }
__construct($callback=null)
Definition: Callback.php:75
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setCallback($callback)
Definition: Callback.php:111
_error($messageKey, $value=null)
Definition: Abstract.php:284
$value
Definition: gender.phtml:16