Platformsh\Cli\Helper\QuestionHelper::choose PHP Метод

choose() публичный Метод

public choose ( array $items, string $text = 'Enter a number to choose an item:', mixed $default = null ) : mixed
$items array An associative array of choices.
$text string Some text to precede the choices.
$default mixed A default (as a key in $items).
Результат mixed The chosen item (as a key in $items).
    public function choose(array $items, $text = 'Enter a number to choose an item:', $default = null)
    {
        if (count($items) === 1) {
            return key($items);
        }
        $itemList = array_values($items);
        $defaultKey = $default !== null ? array_search($default, $itemList) : null;
        $question = new ChoiceQuestion($text, $itemList, $defaultKey);
        $question->setMaxAttempts(5);
        // Unfortunately the default autocompletion can cause '2' to be
        // completed to '20', etc.
        $question->setAutocompleterValues(null);
        $choice = $this->ask($this->input, $this->output, $question);
        $choiceKey = array_search($choice, $items);
        if ($choiceKey === false) {
            throw new \RuntimeException("Invalid value: {$choice}");
        }
        return $choiceKey;
    }

Usage Example

 /**
  * @param string          $sshUrl
  * @param InputInterface  $input
  *
  * @return array|false
  */
 public function chooseDatabase($sshUrl, InputInterface $input)
 {
     $relationships = $this->getRelationships($sshUrl);
     // Filter to find database (mysql and pgsql) relationships.
     $relationships = array_filter($relationships, function (array $relationship) {
         foreach ($relationship as $key => $service) {
             if ($service['scheme'] === 'mysql' || $service['scheme'] === 'pgsql') {
                 return true;
             }
         }
         return false;
     });
     if (empty($relationships)) {
         $this->output->writeln('No databases found');
         return false;
     }
     $questionHelper = new QuestionHelper($input, $this->output);
     $choices = [];
     $separator = '.';
     foreach ($relationships as $name => $relationship) {
         $serviceCount = count($relationship);
         foreach ($relationship as $key => $service) {
             $choices[$name . $separator . $key] = $name . ($serviceCount > 1 ? '.' . $key : '');
         }
     }
     $choice = $questionHelper->choose($choices, 'Enter a number to choose a database:');
     list($name, $key) = explode($separator, $choice, 2);
     $database = $relationships[$name][$key];
     // Add metadata about the database.
     $database['_relationship_name'] = $name;
     $database['_relationship_key'] = $key;
     return $database;
 }