AbstractCommand.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Model\Menu\Builder;
  7. /**
  8. * Menu builder command
  9. * @api
  10. * @since 100.0.2
  11. */
  12. abstract class AbstractCommand
  13. {
  14. /**
  15. * List of required params
  16. *
  17. * @var string[]
  18. */
  19. protected $_requiredParams = ["id"];
  20. /**
  21. * Command params array
  22. *
  23. * @var array
  24. */
  25. protected $_data = [];
  26. /**
  27. * Next command in the chain
  28. *
  29. * @var \Magento\Backend\Model\Menu\Builder\AbstractCommand
  30. */
  31. protected $_next = null;
  32. /**
  33. * @param array $data
  34. * @throws \InvalidArgumentException
  35. */
  36. public function __construct(array $data = [])
  37. {
  38. foreach ($this->_requiredParams as $param) {
  39. if (!isset($data[$param]) || $data[$param] === null) {
  40. throw new \InvalidArgumentException("Missing required param " . $param);
  41. }
  42. }
  43. $this->_data = $data;
  44. }
  45. /**
  46. * Retrieve id of element to apply command to
  47. *
  48. * @return int
  49. */
  50. public function getId()
  51. {
  52. return $this->_data['id'];
  53. }
  54. /**
  55. * Add command as last in the list of callbacks
  56. *
  57. * @param \Magento\Backend\Model\Menu\Builder\AbstractCommand $command
  58. * @return $this
  59. * @throws \InvalidArgumentException if invalid chaining command is supplied
  60. */
  61. public function chain(\Magento\Backend\Model\Menu\Builder\AbstractCommand $command)
  62. {
  63. if ($this->_next === null) {
  64. $this->_next = $command;
  65. } else {
  66. $this->_next->chain($command);
  67. }
  68. return $this;
  69. }
  70. /**
  71. * Execute command and pass control to chained commands
  72. *
  73. * @param array $itemParams
  74. * @return array
  75. */
  76. public function execute(array $itemParams = [])
  77. {
  78. $itemParams = $this->_execute($itemParams);
  79. if ($this->_next !== null) {
  80. $itemParams = $this->_next->execute($itemParams);
  81. }
  82. return $itemParams;
  83. }
  84. /**
  85. * Execute internal command actions
  86. *
  87. * @param array $itemParams
  88. * @return array
  89. */
  90. abstract protected function _execute(array $itemParams);
  91. }