Verzeichnisstruktur phpBB-3.3.16


Veröffentlicht
27.04.2026

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

ChoiceQuestion.php

Zuletzt modifiziert: 01.05.2026, 11:27 - Dateigröße: 4.84 KiB


 001  <?php
 002   
 003  /*
 004   * This file is part of the Symfony package.
 005   *
 006   * (c) Fabien Potencier <fabien@symfony.com>
 007   *
 008   * For the full copyright and license information, please view the LICENSE
 009   * file that was distributed with this source code.
 010   */
 011   
 012  namespace Symfony\Component\Console\Question;
 013   
 014  use Symfony\Component\Console\Exception\InvalidArgumentException;
 015   
 016  /**
 017   * Represents a choice question.
 018   *
 019   * @author Fabien Potencier <fabien@symfony.com>
 020   */
 021  class ChoiceQuestion extends Question
 022  {
 023      private $choices;
 024      private $multiselect = false;
 025      private $prompt = ' > ';
 026      private $errorMessage = 'Value "%s" is invalid';
 027   
 028      /**
 029       * @param string $question The question to ask to the user
 030       * @param array  $choices  The list of available choices
 031       * @param mixed  $default  The default answer to return
 032       */
 033      public function __construct($question, array $choices, $default = null)
 034      {
 035          if (!$choices) {
 036              throw new \LogicException('Choice question must have at least 1 choice available.');
 037          }
 038   
 039          parent::__construct($question, $default);
 040   
 041          $this->choices = $choices;
 042          $this->setValidator($this->getDefaultValidator());
 043          $this->setAutocompleterValues($choices);
 044      }
 045   
 046      /**
 047       * Returns available choices.
 048       *
 049       * @return array
 050       */
 051      public function getChoices()
 052      {
 053          return $this->choices;
 054      }
 055   
 056      /**
 057       * Sets multiselect option.
 058       *
 059       * When multiselect is set to true, multiple choices can be answered.
 060       *
 061       * @param bool $multiselect
 062       *
 063       * @return $this
 064       */
 065      public function setMultiselect($multiselect)
 066      {
 067          $this->multiselect = $multiselect;
 068          $this->setValidator($this->getDefaultValidator());
 069   
 070          return $this;
 071      }
 072   
 073      /**
 074       * Returns whether the choices are multiselect.
 075       *
 076       * @return bool
 077       */
 078      public function isMultiselect()
 079      {
 080          return $this->multiselect;
 081      }
 082   
 083      /**
 084       * Gets the prompt for choices.
 085       *
 086       * @return string
 087       */
 088      public function getPrompt()
 089      {
 090          return $this->prompt;
 091      }
 092   
 093      /**
 094       * Sets the prompt for choices.
 095       *
 096       * @param string $prompt
 097       *
 098       * @return $this
 099       */
 100      public function setPrompt($prompt)
 101      {
 102          $this->prompt = $prompt;
 103   
 104          return $this;
 105      }
 106   
 107      /**
 108       * Sets the error message for invalid values.
 109       *
 110       * The error message has a string placeholder (%s) for the invalid value.
 111       *
 112       * @param string $errorMessage
 113       *
 114       * @return $this
 115       */
 116      public function setErrorMessage($errorMessage)
 117      {
 118          $this->errorMessage = $errorMessage;
 119          $this->setValidator($this->getDefaultValidator());
 120   
 121          return $this;
 122      }
 123   
 124      /**
 125       * Returns the default answer validator.
 126       *
 127       * @return callable
 128       */
 129      private function getDefaultValidator()
 130      {
 131          $choices = $this->choices;
 132          $errorMessage = $this->errorMessage;
 133          $multiselect = $this->multiselect;
 134          $isAssoc = $this->isAssoc($choices);
 135   
 136          return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
 137              if ($multiselect) {
 138                  // Check for a separated comma values
 139                  if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selected, $matches)) {
 140                      throw new InvalidArgumentException(sprintf($errorMessage, $selected));
 141                  }
 142   
 143                  $selectedChoices = array_map('trim', explode(',', $selected));
 144              } else {
 145                  $selectedChoices = [trim($selected)];
 146              }
 147   
 148              $multiselectChoices = [];
 149              foreach ($selectedChoices as $value) {
 150                  $results = [];
 151                  foreach ($choices as $key => $choice) {
 152                      if ($choice === $value) {
 153                          $results[] = $key;
 154                      }
 155                  }
 156   
 157                  if (\count($results) > 1) {
 158                      throw new InvalidArgumentException(sprintf('The provided answer is ambiguous. Value should be one of "%s".', implode('" or "', $results)));
 159                  }
 160   
 161                  $result = array_search($value, $choices);
 162   
 163                  if (!$isAssoc) {
 164                      if (false !== $result) {
 165                          $result = $choices[$result];
 166                      } elseif (isset($choices[$value])) {
 167                          $result = $choices[$value];
 168                      }
 169                  } elseif (false === $result && isset($choices[$value])) {
 170                      $result = $value;
 171                  }
 172   
 173                  if (false === $result) {
 174                      throw new InvalidArgumentException(sprintf($errorMessage, $value));
 175                  }
 176   
 177                  $multiselectChoices[] = (string) $result;
 178              }
 179   
 180              if ($multiselect) {
 181                  return $multiselectChoices;
 182              }
 183   
 184              return current($multiselectChoices);
 185          };
 186      }
 187  }
 188