SelectConfigOption.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * Select option in deployment config tool
  9. */
  10. class SelectConfigOption extends AbstractConfigOption
  11. {
  12. /**#@+
  13. * Frontend input types
  14. */
  15. const FRONTEND_WIZARD_RADIO = 'radio';
  16. const FRONTEND_WIZARD_SELECT = 'select';
  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 string|null $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. $defaultValue = null,
  39. $shortCut = null
  40. ) {
  41. if ($frontendType != self::FRONTEND_WIZARD_SELECT && $frontendType != self::FRONTEND_WIZARD_RADIO) {
  42. throw new \InvalidArgumentException("Frontend input type has to be 'select' or 'radio'.");
  43. }
  44. if (!$selectOptions) {
  45. throw new \InvalidArgumentException('Select options can\'t be empty.');
  46. }
  47. $this->selectOptions = $selectOptions;
  48. parent::__construct(
  49. $name,
  50. $frontendType,
  51. self::VALUE_REQUIRED,
  52. $configPath,
  53. $description,
  54. $defaultValue,
  55. $shortCut
  56. );
  57. }
  58. /**
  59. * Get available options
  60. *
  61. * @return array
  62. */
  63. public function getSelectOptions()
  64. {
  65. return $this->selectOptions;
  66. }
  67. /**
  68. * Validates input data
  69. *
  70. * @param mixed $data
  71. * @return void
  72. * @throws \InvalidArgumentException
  73. */
  74. public function validate($data)
  75. {
  76. if (!in_array($data, $this->getSelectOptions())) {
  77. throw new \InvalidArgumentException("Value specified for '{$this->getName()}' is not supported: '{$data}'");
  78. }
  79. parent::validate($data);
  80. }
  81. }