/vendor/zendframework/zend-console/src/Prompt/Checkbox.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 183 lines · 83 code · 29 blank · 71 comment · 16 complexity · d9e8a4f0d36bade80489fc25b9645899 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Console\Prompt;
  10. use Zend\Console\Exception;
  11. use Zend\Stdlib\ArrayUtils;
  12. final class Checkbox extends AbstractPrompt
  13. {
  14. /**
  15. * @var string
  16. */
  17. private $promptText;
  18. /**
  19. * @var bool
  20. */
  21. private $ignoreCase;
  22. /**
  23. * @var array|Transversable
  24. */
  25. private $options;
  26. /**
  27. * Checked options
  28. * @var array
  29. */
  30. private $checkedOptions = array();
  31. /**
  32. * If the response should be echoed to the console or not
  33. * @var bool
  34. */
  35. private $echo;
  36. /**
  37. * Ask the user to select any number of pre-defined options
  38. *
  39. * @param string $promptText The prompt text to display in console
  40. * @param array|Transversable $options Allowed options
  41. * @param bool $echo True to display selected option?
  42. */
  43. public function __construct($promptText = 'Please select one option (Enter to finish) ', $options = array(), $ignoreCase = true, $echo = false)
  44. {
  45. $this->promptText = (string) $promptText;
  46. $this->setOptions($options);
  47. $this->echo = (bool) $echo;
  48. $this->ignoreCase = (bool) $ignoreCase;
  49. }
  50. /**
  51. * Show a list of options and prompt the user to select any number of them.
  52. *
  53. * @return array Checked options
  54. */
  55. public function show()
  56. {
  57. $this->checkedOptions = array();
  58. $mask = $this->prepareMask();
  59. do {
  60. $this->showAvailableOptions();
  61. $response = $this->readOption($mask);
  62. if ($this->echo) {
  63. $this->showResponse($response);
  64. }
  65. $this->checkOrUncheckOption($response);
  66. } while ($response != "\r" && $response != "\n");
  67. $this->lastResponse = $this->checkedOptions;
  68. return $this->checkedOptions;
  69. }
  70. /**
  71. * Shows the selected option to the screen
  72. * @param string $response
  73. */
  74. private function showResponse($response)
  75. {
  76. $console = $this->getConsole();
  77. if (isset($this->options[$response])) {
  78. $console->writeLine($this->options[$response]);
  79. } else {
  80. $console->writeLine();
  81. }
  82. }
  83. /**
  84. * Check or uncheck an option
  85. *
  86. * @param string $response
  87. */
  88. private function checkOrUncheckOption($response)
  89. {
  90. if ($response != "\r" && $response != "\n" && isset($this->options[$response])) {
  91. $pos = array_search($this->options[$response], $this->checkedOptions);
  92. if ($pos === false) {
  93. $this->checkedOptions[] = $this->options[$response];
  94. } else {
  95. array_splice($this->checkedOptions, $pos, 1);
  96. }
  97. }
  98. }
  99. /**
  100. * Generates a mask to to be used by the readChar method.
  101. *
  102. * @return string
  103. */
  104. private function prepareMask()
  105. {
  106. $mask = implode("", array_keys($this->options)) . "\r\n";
  107. /**
  108. * Normalize the mask if case is irrelevant
  109. */
  110. if (!$this->ignoreCase) {
  111. return $mask;
  112. }
  113. $mask = implode("", array_unique(str_split(strtolower($mask) . strtoupper($mask))));
  114. return $mask;
  115. }
  116. /**
  117. * Reads a char from console.
  118. *
  119. * @param string $mask
  120. * @return string
  121. */
  122. private function readOption($mask)
  123. {
  124. /**
  125. * Read char from console
  126. */
  127. return $this->getConsole()->readChar($mask);
  128. }
  129. /**
  130. * Shows the available options with checked and unchecked states
  131. */
  132. private function showAvailableOptions()
  133. {
  134. $console = $this->getConsole();
  135. $console->writeLine($this->promptText);
  136. foreach ($this->options as $k => $v) {
  137. $console->writeLine(' ' . $k . ') ' . (in_array($v, $this->checkedOptions) ? '[X] ' : '[ ] ') . $v);
  138. }
  139. }
  140. /**
  141. * Set allowed options
  142. *
  143. * @param array|\Traversable $options
  144. * @throws Exception\InvalidArgumentException
  145. */
  146. private function setOptions($options)
  147. {
  148. $options = ArrayUtils::iteratorToArray($options);
  149. if (empty($options)) {
  150. throw new Exception\InvalidArgumentException('Please, specify at least one option');
  151. }
  152. $this->options = $options;
  153. }
  154. }