setPromptText($promptText); } if (!count($options)) { throw new Exception\BadMethodCallException( 'Cannot construct a "select" prompt without any options' ); } $this->setOptions($options); if ($allowEmpty !== null) { $this->setAllowEmpty($allowEmpty); } if ($echo !== null) { $this->setEcho($echo); } } /** * Show a list of options and prompt the user to select one of them. * * @return string Selected option */ public function show() { // Show prompt text and available options $console = $this->getConsole(); $console->writeLine($this->promptText); foreach ($this->options as $k => $v) { $console->writeLine(' ' . $k . ') ' . $v); } // Prepare mask $mask = implode("", array_keys($this->options)); if ($this->allowEmpty) { $mask .= "\r\n"; } // Prepare other params for parent class $this->setAllowedChars($mask); $oldPrompt = $this->promptText; $oldEcho = $this->echo; $this->echo = false; $this->promptText = null; // Retrieve a single character $response = parent::show(); // Restore old params $this->promptText = $oldPrompt; $this->echo = $oldEcho; // Display selected option if echo is enabled if ($this->echo) { if (isset($this->options[$response])) { $console->writeLine($this->options[$response]); } else { $console->writeLine(); } } $this->lastResponse = $response; return $response; } /** * Set allowed options * * @param array|\Traversable $options * @throws Exception\BadMethodCallException */ public function setOptions($options) { if (!is_array($options) && !$options instanceof \Traversable) { throw new Exception\BadMethodCallException( 'Please specify an array or Traversable object as options' ); } if (!is_array($options)) { $this->options = array(); foreach ($options as $k => $v) { $this->options[$k] = $v; } } else { $this->options = $options; } } /** * @return array */ public function getOptions() { return $this->options; } }