public function choose(array $items, $text = 'Enter a number to choose an item:', $default = null, $skipOnOne = true)
    {
        if (count($items) === 1 && $skipOnOne) {
            return key($items);
        }
        $itemList = array_values($items);
        $defaultKey = $default !== null ? array_search($default, $itemList, true) : null;
        $question = new ChoiceQuestion($text, $itemList, $defaultKey);
        $question->setMaxAttempts(5);

        if (!$this->input->isInteractive()) {
            if (!isset($defaultKey)) {
                return null;
            }
            $choice = $itemList[$defaultKey];
            $choiceKey = array_search($choice, $items, true);
            if ($choiceKey === false) {
                throw new \RuntimeException('Invalid default');
            }

            return $choiceKey;
        }

        $choice = $this->ask($this->input, $this->output, $question);
        $choiceKey = array_search($choice, $items, true);
        if ($choiceKey === false) {
            throw new \RuntimeException("Invalid value: $choice");
        }

        $this->output->writeln('');

        return $choiceKey;
    }
public function choose(array $items, $text = 'Enter a number to choose an item:', $default = null, $skipOnOne = true)
    {
        if (count($items) === 1 && $skipOnOne) {
            return key($items);
        }
        $itemList = array_values($items);
        $defaultKey = $default !== null ? array_search($default, $itemList, true) : null;
        $question = new ChoiceQuestion($text, $itemList, $defaultKey);
        $question->setMaxAttempts(5);

        if (!$this->input->isInteractive()) {
            if (!isset($defaultKey)) {
                return null;
            }
            $choice = $itemList[$defaultKey];
            $choiceKey = array_search($choice, $items, true);
            if ($choiceKey === false) {
                throw new \RuntimeException('Invalid default');
            }

            return $choiceKey;
        }

        $choice = $this->ask($this->input, $this->output, $question);
        $choiceKey = array_search($choice, $items, true);
        if ($choiceKey === false) {
            throw new \RuntimeException("Invalid value: $choice");
        }

        $this->output->writeln('');

        return $choiceKey;
    }
public function choose ( array $ items , $ text = 'Enter a number to choose an item:' , $ default = null , $ skipOnOne = true ) { if ( count ( $ items ) === 1 && $ skipOnOne ) { return key ( $ items ) ; } $ itemList = array_values ( $ items ) ; $ defaultKey = $ default !== null ? array_search ( $ default , $ itemList , true ) : null ; $ question = new ChoiceQuestion ( $ text , $ itemList , $ defaultKey ) ; $ question -> setMaxAttempts ( 5 ) ; if ( ! $ this -> input -> isInteractive ( ) ) { if ( ! isset ( $ defaultKey ) ) { return null ; } $ choice = $ itemList [ $ defaultKey ] ; $ choiceKey = array_search ( $ choice , $ items , true ) ; if ( $ choiceKey === false ) { throw new \ RuntimeException ( 'Invalid default' ) ; } return $ choiceKey ; } $ choice = $ this -> ask ( $ this -> input , $ this -> output , $ question ) ; $ choiceKey = array_search ( $ choice , $ items , true ) ; if ( $ choiceKey === false ) { throw new \ RuntimeException ( "Invalid value: $choice" ) ; } $ this -> output -> writeln ( '' ) ; return $ choiceKey ; }
Provides an interactive choice question.

@param array  $items     An associative array of choices.
@param string $text      Some text to precede the choices.
@param mixed  $default   A default (as a key in $items).
@param bool   $skipOnOne Whether to skip the choice if there is only one
item.

@throws \RuntimeException on failure

@return mixed
The chosen item (as a key in $items).
Provides an interactive choice question .