MultiSelectConfigOption.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\Option;
  7. /**
  8. * Multi-select option in deployment config tool
  9. */
  10. class MultiSelectConfigOption extends AbstractConfigOption
  11. {
  12. /**#@+
  13. * Frontend input types
  14. */
  15. const FRONTEND_WIZARD_CHECKBOX = 'checkbox';
  16. const FRONTEND_WIZARD_MULTISELECT = 'multiselect';
  17. /**#@- */
  18. /**#@- */
  19. private $selectOptions;
  20. /**
  21. * Constructor
  22. *
  23. * @param string $name
  24. * @param string $frontendType
  25. * @param array $selectOptions
  26. * @param string $configPath
  27. * @param string $description
  28. * @param array $defaultValue
  29. * @param string|array|null $shortCut
  30. * @throws \InvalidArgumentException
  31. */
  32. public function __construct(
  33. $name,
  34. $frontendType,
  35. array $selectOptions,
  36. $configPath,
  37. $description = '',
  38. array $defaultValue = [],
  39. $shortCut = null
  40. ) {
  41. if ($frontendType != self::FRONTEND_WIZARD_MULTISELECT && $frontendType != self::FRONTEND_WIZARD_CHECKBOX) {
  42. throw new \InvalidArgumentException(
  43. "Frontend input type has to be 'multiselect', 'textarea' or 'checkbox'."
  44. );
  45. }
  46. if (!$selectOptions) {
  47. throw new \InvalidArgumentException('Select options can\'t be empty.');
  48. }
  49. $this->selectOptions = $selectOptions;
  50. parent::__construct(
  51. $name,
  52. $frontendType,
  53. self::VALUE_REQUIRED | self::VALUE_IS_ARRAY,
  54. $configPath,
  55. $description,
  56. $defaultValue,
  57. $shortCut
  58. );
  59. }
  60. /**
  61. * Get available options
  62. *
  63. * @return array
  64. */
  65. public function getSelectOptions()
  66. {
  67. return $this->selectOptions;
  68. }
  69. /**
  70. * Validates input data
  71. *
  72. * @param mixed $data
  73. * @return void
  74. * @throws \InvalidArgumentException
  75. */
  76. public function validate($data)
  77. {
  78. if (is_array($data)) {
  79. foreach ($data as $value) {
  80. if (!in_array($value, $this->getSelectOptions())) {
  81. throw new \InvalidArgumentException(
  82. "Value specified for '{$this->getName()}' is not supported: '{$value}'"
  83. );
  84. }
  85. }
  86. }
  87. parent::validate($data);
  88. }
  89. }