Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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: 09.10.2024, 12:56 - Dateigröße: 4.94 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       * Constructor.
030       *
031       * @param string $question The question to ask to the user
032       * @param array  $choices  The list of available choices
033       * @param mixed  $default  The default answer to return
034       */
035      public function __construct($question, array $choices, $default = null)
036      {
037          parent::__construct($question, $default);
038   
039          $this->choices = $choices;
040          $this->setValidator($this->getDefaultValidator());
041          $this->setAutocompleterValues($choices);
042      }
043   
044      /**
045       * Returns available choices.
046       *
047       * @return array
048       */
049      public function getChoices()
050      {
051          return $this->choices;
052      }
053   
054      /**
055       * Sets multiselect option.
056       *
057       * When multiselect is set to true, multiple choices can be answered.
058       *
059       * @param bool $multiselect
060       *
061       * @return ChoiceQuestion The current instance
062       */
063      public function setMultiselect($multiselect)
064      {
065          $this->multiselect = $multiselect;
066          $this->setValidator($this->getDefaultValidator());
067   
068          return $this;
069      }
070   
071      /**
072       * Returns whether the choices are multiselect.
073       *
074       * @return bool
075       */
076      public function isMultiselect()
077      {
078          return $this->multiselect;
079      }
080   
081      /**
082       * Gets the prompt for choices.
083       *
084       * @return string
085       */
086      public function getPrompt()
087      {
088          return $this->prompt;
089      }
090   
091      /**
092       * Sets the prompt for choices.
093       *
094       * @param string $prompt
095       *
096       * @return ChoiceQuestion The current instance
097       */
098      public function setPrompt($prompt)
099      {
100          $this->prompt = $prompt;
101   
102          return $this;
103      }
104   
105      /**
106       * Sets the error message for invalid values.
107       *
108       * The error message has a string placeholder (%s) for the invalid value.
109       *
110       * @param string $errorMessage
111       *
112       * @return ChoiceQuestion The current instance
113       */
114      public function setErrorMessage($errorMessage)
115      {
116          $this->errorMessage = $errorMessage;
117          $this->setValidator($this->getDefaultValidator());
118   
119          return $this;
120      }
121   
122      /**
123       * Returns the default answer validator.
124       *
125       * @return callable
126       */
127      private function getDefaultValidator()
128      {
129          $choices = $this->choices;
130          $errorMessage = $this->errorMessage;
131          $multiselect = $this->multiselect;
132          $isAssoc = $this->isAssoc($choices);
133   
134          return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
135              // Collapse all spaces.
136              $selectedChoices = str_replace(' ', '', $selected);
137   
138              if ($multiselect) {
139                  // Check for a separated comma values
140                  if (!preg_match('/^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$/', $selectedChoices, $matches)) {
141                      throw new InvalidArgumentException(sprintf($errorMessage, $selected));
142                  }
143                  $selectedChoices = explode(',', $selectedChoices);
144              } else {
145                  $selectedChoices = array($selected);
146              }
147   
148              $multiselectChoices = array();
149              foreach ($selectedChoices as $value) {
150                  $results = array();
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