-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Validator.php
127 lines (113 loc) · 3.9 KB
/
Validator.php
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
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Model\Menu\Item;
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)
{
foreach ($this->_required as $param) {
if (!isset($data[$param])) {
throw new \BadMethodCallException('Missing required param ' . $param);
}
}
if (array_search($data['id'], $this->_ids) !== false) {
throw new \InvalidArgumentException('Item with id ' . $data['id'] . ' already exists');
}
foreach ($data as $param => $value) {
if (
$data[$param] !== null
&& isset(
$this->_validators[$param]
) && !$this->_validators[$param]->isValid(
$value
)
) {
throw new \InvalidArgumentException(
"Param " . $param . " doesn't pass validation: " . implode(
'; ',
$this->_validators[$param]->getMessages()
)
);
}
}
$this->_ids[] = $data['id'];
}
/**
* 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()
)
);
}
}
}