Validator.php 5.0 KB

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