Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Validate.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Interface.php';
26 
34 {
40  protected $_validators = array();
41 
47  protected $_messages = array();
48 
54  protected static $_defaultNamespaces = array();
55 
62  protected $_errors = array();
63 
74  public function addValidator(Zend_Validate_Interface $validator, $breakChainOnFailure = false)
75  {
76  $this->_validators[] = array(
77  'instance' => $validator,
78  'breakChainOnFailure' => (boolean) $breakChainOnFailure
79  );
80  return $this;
81  }
82 
91  public function isValid($value)
92  {
93  $this->_messages = array();
94  $this->_errors = array();
95  $result = true;
96  foreach ($this->_validators as $element) {
97  $validator = $element['instance'];
98  if ($validator->isValid($value)) {
99  continue;
100  }
101  $result = false;
102  $messages = $validator->getMessages();
103  $this->_messages = array_merge($this->_messages, $messages);
104  $this->_errors = array_merge($this->_errors, array_keys($messages));
105  if ($element['breakChainOnFailure']) {
106  break;
107  }
108  }
109  return $result;
110  }
111 
119  public function getMessages()
120  {
121  return $this->_messages;
122  }
123 
132  public function getErrors()
133  {
134  return $this->_errors;
135  }
136 
142  public static function getDefaultNamespaces()
143  {
145  }
146 
153  public static function setDefaultNamespaces($namespace)
154  {
155  if (!is_array($namespace)) {
156  $namespace = array((string) $namespace);
157  }
158 
159  self::$_defaultNamespaces = $namespace;
160  }
161 
168  public static function addDefaultNamespaces($namespace)
169  {
170  if (!is_array($namespace)) {
171  $namespace = array((string) $namespace);
172  }
173 
174  self::$_defaultNamespaces = array_unique(array_merge(self::$_defaultNamespaces, $namespace));
175  }
176 
182  public static function hasDefaultNamespaces()
183  {
184  return (!empty(self::$_defaultNamespaces));
185  }
186 
195  public static function is($value, $classBaseName, array $args = array(), $namespaces = array())
196  {
197  $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Validate'));
198  $className = ucfirst($classBaseName);
199  try {
200  if (!class_exists($className, false)) {
201  #require_once 'Zend/Loader.php';
202  foreach($namespaces as $namespace) {
203  $class = $namespace . '_' . $className;
204  $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
205  if (Zend_Loader::isReadable($file)) {
207  $className = $class;
208  break;
209  }
210  }
211  }
212 
213  $class = new ReflectionClass($className);
214  if ($class->implementsInterface('Zend_Validate_Interface')) {
215  if ($class->hasMethod('__construct')) {
216  $keys = array_keys($args);
217  $numeric = false;
218  foreach($keys as $key) {
219  if (is_numeric($key)) {
220  $numeric = true;
221  break;
222  }
223  }
224 
225  if ($numeric) {
226  $object = $class->newInstanceArgs($args);
227  } else {
228  $object = $class->newInstance($args);
229  }
230  } else {
231  $object = $class->newInstance();
232  }
233 
234  return $object->isValid($value);
235  }
236  } catch (Zend_Validate_Exception $ze) {
237  // if there is an exception while validating throw it
238  throw $ze;
239  } catch (Exception $e) {
240  // fallthrough and continue for missing validation classes
241  }
242 
243  #require_once 'Zend/Validate/Exception.php';
244  throw new Zend_Validate_Exception("Validate class not found from basename '$classBaseName'");
245  }
246 
252  public static function getMessageLength()
253  {
254  #require_once 'Zend/Validate/Abstract.php';
256  }
257 
263  public static function setMessageLength($length = -1)
264  {
265  #require_once 'Zend/Validate/Abstract.php';
267  }
268 
274  public static function getDefaultTranslator($translator = null)
275  {
276  #require_once 'Zend/Validate/Abstract.php';
278  }
279 
285  public static function setDefaultTranslator($translator = null)
286  {
287  #require_once 'Zend/Validate/Abstract.php';
289  }
290 }
static setDefaultTranslator($translator=null)
Definition: Abstract.php:398
static loadClass($class, $dirs=null)
Definition: Loader.php:52
static $_defaultNamespaces
Definition: Validate.php:54
static getDefaultTranslator($translator=null)
Definition: Validate.php:274
isValid($value)
Definition: Validate.php:91
static setMessageLength($length=-1)
Definition: Abstract.php:479
static hasDefaultNamespaces()
Definition: Validate.php:182
static isReadable($filename)
Definition: Loader.php:162
static setMessageLength($length=-1)
Definition: Validate.php:263
static getMessageLength()
Definition: Abstract.php:469
static getDefaultTranslator()
Definition: Abstract.php:415
static setDefaultTranslator($translator=null)
Definition: Validate.php:285
static getDefaultNamespaces()
Definition: Validate.php:142
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
static setDefaultNamespaces($namespace)
Definition: Validate.php:153
static getMessageLength()
Definition: Validate.php:252
addValidator(Zend_Validate_Interface $validator, $breakChainOnFailure=false)
Definition: Validate.php:74
static is($value, $classBaseName, array $args=array(), $namespaces=array())
Definition: Validate.php:195
static addDefaultNamespaces($namespace)
Definition: Validate.php:168
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
$element
Definition: element.phtml:12