Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

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

InputOption.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 5.91 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\Input;
013   
014  use Symfony\Component\Console\Exception\InvalidArgumentException;
015  use Symfony\Component\Console\Exception\LogicException;
016   
017  /**
018   * Represents a command line option.
019   *
020   * @author Fabien Potencier <fabien@symfony.com>
021   */
022  class InputOption
023  {
024      const VALUE_NONE = 1;
025      const VALUE_REQUIRED = 2;
026      const VALUE_OPTIONAL = 4;
027      const VALUE_IS_ARRAY = 8;
028   
029      private $name;
030      private $shortcut;
031      private $mode;
032      private $default;
033      private $description;
034   
035      /**
036       * @param string                        $name        The option name
037       * @param string|array|null             $shortcut    The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
038       * @param int|null                      $mode        The option mode: One of the VALUE_* constants
039       * @param string                        $description A description text
040       * @param string|string[]|int|bool|null $default     The default value (must be null for self::VALUE_NONE)
041       *
042       * @throws InvalidArgumentException If option mode is invalid or incompatible
043       */
044      public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
045      {
046          if (0 === strpos($name, '--')) {
047              $name = substr($name, 2);
048          }
049   
050          if (empty($name)) {
051              throw new InvalidArgumentException('An option name cannot be empty.');
052          }
053   
054          if (empty($shortcut)) {
055              $shortcut = null;
056          }
057   
058          if (null !== $shortcut) {
059              if (\is_array($shortcut)) {
060                  $shortcut = implode('|', $shortcut);
061              }
062              $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
063              $shortcuts = array_filter($shortcuts);
064              $shortcut = implode('|', $shortcuts);
065   
066              if (empty($shortcut)) {
067                  throw new InvalidArgumentException('An option shortcut cannot be empty.');
068              }
069          }
070   
071          if (null === $mode) {
072              $mode = self::VALUE_NONE;
073          } elseif (!\is_int($mode) || $mode > 15 || $mode < 1) {
074              throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
075          }
076   
077          $this->name = $name;
078          $this->shortcut = $shortcut;
079          $this->mode = $mode;
080          $this->description = $description;
081   
082          if ($this->isArray() && !$this->acceptValue()) {
083              throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
084          }
085   
086          $this->setDefault($default);
087      }
088   
089      /**
090       * Returns the option shortcut.
091       *
092       * @return string|null The shortcut
093       */
094      public function getShortcut()
095      {
096          return $this->shortcut;
097      }
098   
099      /**
100       * Returns the option name.
101       *
102       * @return string The name
103       */
104      public function getName()
105      {
106          return $this->name;
107      }
108   
109      /**
110       * Returns true if the option accepts a value.
111       *
112       * @return bool true if value mode is not self::VALUE_NONE, false otherwise
113       */
114      public function acceptValue()
115      {
116          return $this->isValueRequired() || $this->isValueOptional();
117      }
118   
119      /**
120       * Returns true if the option requires a value.
121       *
122       * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise
123       */
124      public function isValueRequired()
125      {
126          return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
127      }
128   
129      /**
130       * Returns true if the option takes an optional value.
131       *
132       * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise
133       */
134      public function isValueOptional()
135      {
136          return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
137      }
138   
139      /**
140       * Returns true if the option can take multiple values.
141       *
142       * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise
143       */
144      public function isArray()
145      {
146          return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
147      }
148   
149      /**
150       * Sets the default value.
151       *
152       * @param string|string[]|int|bool|null $default The default value
153       *
154       * @throws LogicException When incorrect default value is given
155       */
156      public function setDefault($default = null)
157      {
158          if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
159              throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
160          }
161   
162          if ($this->isArray()) {
163              if (null === $default) {
164                  $default = [];
165              } elseif (!\is_array($default)) {
166                  throw new LogicException('A default value for an array option must be an array.');
167              }
168          }
169   
170          $this->default = $this->acceptValue() ? $default : false;
171      }
172   
173      /**
174       * Returns the default value.
175       *
176       * @return string|string[]|int|bool|null The default value
177       */
178      public function getDefault()
179      {
180          return $this->default;
181      }
182   
183      /**
184       * Returns the description text.
185       *
186       * @return string The description text
187       */
188      public function getDescription()
189      {
190          return $this->description;
191      }
192   
193      /**
194       * Checks whether the given option equals this one.
195       *
196       * @return bool
197       */
198      public function equals(self $option)
199      {
200          return $option->getName() === $this->getName()
201              && $option->getShortcut() === $this->getShortcut()
202              && $option->getDefault() === $this->getDefault()
203              && $option->isArray() === $this->isArray()
204              && $option->isValueRequired() === $this->isValueRequired()
205              && $option->isValueOptional() === $this->isValueOptional()
206          ;
207      }
208  }
209