[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/console/Question/ -> Question.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Console\Question;
  13  
  14  use Symfony\Component\Console\Exception\InvalidArgumentException;
  15  use Symfony\Component\Console\Exception\LogicException;
  16  
  17  /**
  18   * Represents a Question.
  19   *
  20   * @author Fabien Potencier <fabien@symfony.com>
  21   */
  22  class Question
  23  {
  24      private $question;
  25      private $attempts;
  26      private $hidden = false;
  27      private $hiddenFallback = true;
  28      private $autocompleterValues;
  29      private $validator;
  30      private $default;
  31      private $normalizer;
  32  
  33      /**
  34       * @param string $question The question to ask to the user
  35       * @param mixed  $default  The default answer to return if the user enters nothing
  36       */
  37      public function __construct($question, $default = null)
  38      {
  39          $this->question = $question;
  40          $this->default = $default;
  41      }
  42  
  43      /**
  44       * Returns the question.
  45       *
  46       * @return string
  47       */
  48      public function getQuestion()
  49      {
  50          return $this->question;
  51      }
  52  
  53      /**
  54       * Returns the default answer.
  55       *
  56       * @return mixed
  57       */
  58      public function getDefault()
  59      {
  60          return $this->default;
  61      }
  62  
  63      /**
  64       * Returns whether the user response must be hidden.
  65       *
  66       * @return bool
  67       */
  68      public function isHidden()
  69      {
  70          return $this->hidden;
  71      }
  72  
  73      /**
  74       * Sets whether the user response must be hidden or not.
  75       *
  76       * @param bool $hidden
  77       *
  78       * @return $this
  79       *
  80       * @throws LogicException In case the autocompleter is also used
  81       */
  82      public function setHidden($hidden)
  83      {
  84          if ($this->autocompleterValues) {
  85              throw new LogicException('A hidden question cannot use the autocompleter.');
  86          }
  87  
  88          $this->hidden = (bool) $hidden;
  89  
  90          return $this;
  91      }
  92  
  93      /**
  94       * In case the response can not be hidden, whether to fallback on non-hidden question or not.
  95       *
  96       * @return bool
  97       */
  98      public function isHiddenFallback()
  99      {
 100          return $this->hiddenFallback;
 101      }
 102  
 103      /**
 104       * Sets whether to fallback on non-hidden question if the response can not be hidden.
 105       *
 106       * @param bool $fallback
 107       *
 108       * @return $this
 109       */
 110      public function setHiddenFallback($fallback)
 111      {
 112          $this->hiddenFallback = (bool) $fallback;
 113  
 114          return $this;
 115      }
 116  
 117      /**
 118       * Gets values for the autocompleter.
 119       *
 120       * @return iterable|null
 121       */
 122      public function getAutocompleterValues()
 123      {
 124          return $this->autocompleterValues;
 125      }
 126  
 127      /**
 128       * Sets values for the autocompleter.
 129       *
 130       * @param iterable|null $values
 131       *
 132       * @return $this
 133       *
 134       * @throws InvalidArgumentException
 135       * @throws LogicException
 136       */
 137      public function setAutocompleterValues($values)
 138      {
 139          if (\is_array($values)) {
 140              $values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values);
 141          }
 142  
 143          if (null !== $values && !\is_array($values) && !$values instanceof \Traversable) {
 144              throw new InvalidArgumentException('Autocompleter values can be either an array, `null` or a `Traversable` object.');
 145          }
 146  
 147          if ($this->hidden) {
 148              throw new LogicException('A hidden question cannot use the autocompleter.');
 149          }
 150  
 151          $this->autocompleterValues = $values;
 152  
 153          return $this;
 154      }
 155  
 156      /**
 157       * Sets a validator for the question.
 158       *
 159       * @param callable|null $validator
 160       *
 161       * @return $this
 162       */
 163      public function setValidator($validator)
 164      {
 165          $this->validator = $validator;
 166  
 167          return $this;
 168      }
 169  
 170      /**
 171       * Gets the validator for the question.
 172       *
 173       * @return callable|null
 174       */
 175      public function getValidator()
 176      {
 177          return $this->validator;
 178      }
 179  
 180      /**
 181       * Sets the maximum number of attempts.
 182       *
 183       * Null means an unlimited number of attempts.
 184       *
 185       * @param int|null $attempts
 186       *
 187       * @return $this
 188       *
 189       * @throws InvalidArgumentException in case the number of attempts is invalid
 190       */
 191      public function setMaxAttempts($attempts)
 192      {
 193          if (null !== $attempts && $attempts < 1) {
 194              throw new InvalidArgumentException('Maximum number of attempts must be a positive value.');
 195          }
 196  
 197          $this->attempts = $attempts;
 198  
 199          return $this;
 200      }
 201  
 202      /**
 203       * Gets the maximum number of attempts.
 204       *
 205       * Null means an unlimited number of attempts.
 206       *
 207       * @return int|null
 208       */
 209      public function getMaxAttempts()
 210      {
 211          return $this->attempts;
 212      }
 213  
 214      /**
 215       * Sets a normalizer for the response.
 216       *
 217       * The normalizer can be a callable (a string), a closure or a class implementing __invoke.
 218       *
 219       * @param callable $normalizer
 220       *
 221       * @return $this
 222       */
 223      public function setNormalizer($normalizer)
 224      {
 225          $this->normalizer = $normalizer;
 226  
 227          return $this;
 228      }
 229  
 230      /**
 231       * Gets the normalizer for the response.
 232       *
 233       * The normalizer can ba a callable (a string), a closure or a class implementing __invoke.
 234       *
 235       * @return callable
 236       */
 237      public function getNormalizer()
 238      {
 239          return $this->normalizer;
 240      }
 241  
 242      protected function isAssoc($array)
 243      {
 244          return (bool) \count(array_filter(array_keys($array), 'is_string'));
 245      }
 246  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1