Source of file Select.php
Size: 3,864 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Console/Prompt/Select.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Console\Prompt; use Zend\Console\Exception; class Select extends Char { /** * @var string */ protected $promptText = 'Please select an option'; /** * @var bool */ protected $ignoreCase = true; /** * @var array */ protected $options = array(); /** * Ask the user to select one of pre-defined options * * @param string $promptText The prompt text to display in console * @param array $options Allowed options * @param bool $allowEmpty Allow empty (no) selection? * @param bool $echo True to display selected option? * @throws Exception\BadMethodCallException if no options available */ public function __construct( $promptText = 'Please select one option', $options = array(), $allowEmpty = false, $echo = false ) { if ($promptText !== null) { $this->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; } } |