Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Validator.php
Go to the documentation of this file.
1 <?php
7 
11 class Validator
12 {
19  protected $_dataValidators = [];
20 
27  protected $_errorMessages;
28 
32  public function __construct()
33  {
34  $this->_setTypeValidators();
35  $this->_setTitleValidators();
36  }
37 
43  protected function _setTitleValidators()
44  {
45  $titleValidators = [
46  [
47  'name' => 'not_empty',
48  'class' => 'Zend_Validate_NotEmpty',
49  'break' => true,
50  'options' => [],
51  'message' => (string)new \Magento\Framework\Phrase('Field title can\'t be empty'),
52  ],
53  ];
54 
55  $this->addDataValidators('theme_title', $titleValidators);
56  return $this;
57  }
58 
64  protected function _setTypeValidators()
65  {
66  $typeValidators = [
67  [
68  'name' => 'not_empty',
69  'class' => 'Zend_Validate_NotEmpty',
70  'break' => true,
71  'options' => [],
72  'message' => (string)new \Magento\Framework\Phrase('Theme type can\'t be empty'),
73  ],
74  [
75  'name' => 'available',
76  'class' => 'Zend_Validate_InArray',
77  'break' => true,
78  'options' => [
79  'haystack' => [
83  ],
84  ],
85  'message' => (string)new \Magento\Framework\Phrase('Theme type is invalid')
86  ],
87  ];
88 
89  $this->addDataValidators('type', $typeValidators);
90 
91  return $this;
92  }
93 
101  public function addDataValidators($dataKey, $validators)
102  {
103  if (!isset($this->_dataValidators[$dataKey])) {
104  $this->_dataValidators[$dataKey] = [];
105  }
106  foreach ($validators as $validator) {
107  $this->_dataValidators[$dataKey][$validator['name']] = $validator;
108  }
109  return $this;
110  }
111 
118  public function getErrorMessages($dataKey = null)
119  {
120  if ($dataKey) {
121  return $this->_errorMessages[$dataKey] ?? [];
122  }
123  return $this->_errorMessages;
124  }
125 
132  protected function _instantiateValidators(array &$validators)
133  {
134  foreach ($validators as &$validator) {
135  if (is_string($validator['class'])) {
136  $validator['class'] = new $validator['class']($validator['options']);
137  $validator['class']->setDisableTranslator(true);
138  }
139  }
140  return $this;
141  }
142 
151  protected function _validateDataItem($validator, $dataKey, $dataValue)
152  {
153  if ($validator['class'] instanceof \Zend_Validate_NotEmpty && !$validator['class']->isValid(
154  $dataValue
155  ) || !empty($dataValue) && !$validator['class']->isValid(
156  $dataValue
157  )
158  ) {
159  $this->_errorMessages[$dataKey][] = $validator['message'];
160  if ($validator['break']) {
161  return false;
162  }
163  }
164  return true;
165  }
166 
173  public function validate(\Magento\Framework\DataObject $data)
174  {
175  $this->_errorMessages = [];
176  foreach ($this->_dataValidators as $dataKey => $validators) {
177  if (!isset($data[$dataKey])) {
178  continue;
179  }
180 
181  $this->_instantiateValidators($validators);
182  foreach ($validators as $validator) {
183  if (!$this->_validateDataItem($validator, $dataKey, $data[$dataKey])) {
184  break;
185  }
186  }
187  }
188  return empty($this->_errorMessages);
189  }
190 }
validate(\Magento\Framework\DataObject $data)
Definition: Validator.php:173
_validateDataItem($validator, $dataKey, $dataValue)
Definition: Validator.php:151