1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Model\Menu\Item;
/**
* @api
* @since 100.0.2
*/
class Validator
{
/**
* The list of required params
*
* @var string[]
*/
protected $_required = ['id', 'title', 'resource'];
/**
* List of created item ids
*
* @var array
*/
protected $_ids = [];
/**
* The list of primitive validators
*
* @var \Zend_Validate[]
*/
protected $_validators = [];
/**
* Constructor
*/
public function __construct()
{
$idValidator = new \Zend_Validate();
$idValidator->addValidator(new \Zend_Validate_StringLength(['min' => 3]));
$idValidator->addValidator(new \Zend_Validate_Regex('/^[A-Za-z0-9\/:_]+$/'));
$resourceValidator = new \Zend_Validate();
$resourceValidator->addValidator(new \Zend_Validate_StringLength(['min' => 8]));
$resourceValidator->addValidator(
new \Zend_Validate_Regex('/^[A-Z][A-Za-z0-9]+_[A-Z][A-Za-z0-9]+::[A-Za-z_0-9]+$/')
);
$attributeValidator = new \Zend_Validate();
$attributeValidator->addValidator(new \Zend_Validate_StringLength(['min' => 3]));
$attributeValidator->addValidator(new \Zend_Validate_Regex('/^[A-Za-z0-9\/_]+$/'));
$textValidator = new \Zend_Validate_StringLength(['min' => 3, 'max' => 50]);
$titleValidator = $tooltipValidator = $textValidator;
$actionValidator = $moduleDepValidator = $configDepValidator = $attributeValidator;
$this->_validators['id'] = $idValidator;
$this->_validators['title'] = $titleValidator;
$this->_validators['action'] = $actionValidator;
$this->_validators['resource'] = $resourceValidator;
$this->_validators['dependsOnModule'] = $moduleDepValidator;
$this->_validators['dependsOnConfig'] = $configDepValidator;
$this->_validators['toolTip'] = $tooltipValidator;
}
/**
* Validate menu item params
*
* @param array $data
* @return void
* @throws \InvalidArgumentException
* @throws \BadMethodCallException
*/
public function validate($data)
{
if ($this->checkMenuItemIsRemoved($data)) {
return;
}
$this->assertContainsRequiredParameters($data);
$this->assertIdentifierIsNotUsed($data['id']);
foreach ($data as $param => $value) {
$this->validateMenuItemParameter($param, $value);
}
$this->_ids[] = $data['id'];
}
/**
* Check that menu item is not deleted
*
* @param array $data
* @return bool
*/
private function checkMenuItemIsRemoved($data)
{
return isset($data['id'], $data['removed']) && $data['removed'] === true;
}
/**
* Check that menu item contains all required data
* @param array $data
*
* @throws \BadMethodCallException
*/
private function assertContainsRequiredParameters($data)
{
foreach ($this->_required as $param) {
if (!isset($data[$param])) {
throw new \BadMethodCallException('Missing required param ' . $param);
}
}
}
/**
* Check that menu item id is not used
*
* @param string $id
* @throws \InvalidArgumentException
*/
private function assertIdentifierIsNotUsed($id)
{
if (array_search($id, $this->_ids) !== false) {
throw new \InvalidArgumentException('Item with id ' . $id . ' already exists');
}
}
/**
* Validate menu item parameter value
*
* @param string $param
* @param mixed $value
* @throws \InvalidArgumentException
*/
private function validateMenuItemParameter($param, $value)
{
if ($value === null) {
return;
}
if (!isset($this->_validators[$param])) {
return;
}
$validator = $this->_validators[$param];
if ($validator->isValid($value)) {
return;
}
throw new \InvalidArgumentException(
"Param " . $param . " doesn't pass validation: " . implode(
'; ',
$validator->getMessages()
)
);
}
/**
* Validate incoming param
*
* @param string $param
* @param mixed $value
* @return void
* @throws \InvalidArgumentException
*/
public function validateParam($param, $value)
{
if (in_array($param, $this->_required) && $value === null) {
throw new \InvalidArgumentException('Param ' . $param . ' is required');
}
if ($value !== null && isset($this->_validators[$param]) && !$this->_validators[$param]->isValid($value)) {
throw new \InvalidArgumentException(
'Param ' . $param . ' doesn\'t pass validation: ' . implode(
'; ',
$this->_validators[$param]->getMessages()
)
);
}
}
}