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 
12 class Validator
13 {
19  protected $_required = ['id', 'title', 'resource'];
20 
26  protected $_ids = [];
27 
33  protected $_validators = [];
34 
38  public function __construct()
39  {
40  $idValidator = new \Zend_Validate();
41  $idValidator->addValidator(new \Zend_Validate_StringLength(['min' => 3]));
42  $idValidator->addValidator(new \Zend_Validate_Regex('/^[A-Za-z0-9\/:_]+$/'));
43 
44  $resourceValidator = new \Zend_Validate();
45  $resourceValidator->addValidator(new \Zend_Validate_StringLength(['min' => 8]));
46  $resourceValidator->addValidator(
47  new \Zend_Validate_Regex('/^[A-Z][A-Za-z0-9]+_[A-Z][A-Za-z0-9]+::[A-Za-z_0-9]+$/')
48  );
49 
50  $attributeValidator = new \Zend_Validate();
51  $attributeValidator->addValidator(new \Zend_Validate_StringLength(['min' => 3]));
52  $attributeValidator->addValidator(new \Zend_Validate_Regex('/^[A-Za-z0-9\/_]+$/'));
53 
54  $textValidator = new \Zend_Validate_StringLength(['min' => 3, 'max' => 50]);
55 
56  $titleValidator = $tooltipValidator = $textValidator;
57  $actionValidator = $moduleDepValidator = $configDepValidator = $attributeValidator;
58 
59  $this->_validators['id'] = $idValidator;
60  $this->_validators['title'] = $titleValidator;
61  $this->_validators['action'] = $actionValidator;
62  $this->_validators['resource'] = $resourceValidator;
63  $this->_validators['dependsOnModule'] = $moduleDepValidator;
64  $this->_validators['dependsOnConfig'] = $configDepValidator;
65  $this->_validators['toolTip'] = $tooltipValidator;
66  }
67 
76  public function validate($data)
77  {
78  if ($this->checkMenuItemIsRemoved($data)) {
79  return;
80  }
81 
82  $this->assertContainsRequiredParameters($data);
83  $this->assertIdentifierIsNotUsed($data['id']);
84 
85  foreach ($data as $param => $value) {
86  $this->validateMenuItemParameter($param, $value);
87  }
88  $this->_ids[] = $data['id'];
89  }
90 
97  private function checkMenuItemIsRemoved($data)
98  {
99  return isset($data['id'], $data['removed']) && $data['removed'] === true;
100  }
101 
108  private function assertContainsRequiredParameters($data)
109  {
110  foreach ($this->_required as $param) {
111  if (!isset($data[$param])) {
112  throw new \BadMethodCallException('Missing required param ' . $param);
113  }
114  }
115  }
116 
123  private function assertIdentifierIsNotUsed($id)
124  {
125  if (array_search($id, $this->_ids) !== false) {
126  throw new \InvalidArgumentException('Item with id ' . $id . ' already exists');
127  }
128  }
129 
137  private function validateMenuItemParameter($param, $value)
138  {
139  if ($value === null) {
140  return;
141  }
142  if (!isset($this->_validators[$param])) {
143  return;
144  }
145 
146  $validator = $this->_validators[$param];
147  if ($validator->isValid($value)) {
148  return;
149  }
150 
151  throw new \InvalidArgumentException(
152  "Param " . $param . " doesn't pass validation: " . implode(
153  '; ',
154  $validator->getMessages()
155  )
156  );
157  }
158 
167  public function validateParam($param, $value)
168  {
169  if (in_array($param, $this->_required) && $value === null) {
170  throw new \InvalidArgumentException('Param ' . $param . ' is required');
171  }
172 
173  if ($value !== null && isset($this->_validators[$param]) && !$this->_validators[$param]->isValid($value)) {
174  throw new \InvalidArgumentException(
175  'Param ' . $param . ' doesn\'t pass validation: ' . implode(
176  '; ',
177  $this->_validators[$param]->getMessages()
178  )
179  );
180  }
181  }
182 }
$id
Definition: fieldset.phtml:14
$value
Definition: gender.phtml:16