Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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