|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Components\CLI\Input; |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the symfony framework. |
| 7 | + * |
| 8 | + * (c) Fabien Potencier <fabien.potencier@symfony-project.com> |
| 9 | + * |
| 10 | + * This source file is subject to the MIT license that is bundled |
| 11 | + * with this source code in the file LICENSE. |
| 12 | + */ |
| 13 | + |
| 14 | +/** |
| 15 | + * Represents a command line argument. |
| 16 | + * |
| 17 | + * @package symfony |
| 18 | + * @subpackage cli |
| 19 | + * @author Fabien Potencier <fabien.potencier@symfony-project.com> |
| 20 | + */ |
| 21 | +class Argument |
| 22 | +{ |
| 23 | + const REQUIRED = 1; |
| 24 | + const OPTIONAL = 2; |
| 25 | + const IS_ARRAY = 4; |
| 26 | + |
| 27 | + protected $name; |
| 28 | + protected $mode; |
| 29 | + protected $default; |
| 30 | + protected $description; |
| 31 | + |
| 32 | + /** |
| 33 | + * Constructor. |
| 34 | + * |
| 35 | + * @param string $name The argument name |
| 36 | + * @param integer $mode The argument mode: self::REQUIRED or self::OPTIONAL |
| 37 | + * @param string $description A description text |
| 38 | + * @param mixed $default The default value (for self::OPTIONAL mode only) |
| 39 | + */ |
| 40 | + public function __construct($name, $mode = null, $description = '', $default = null) |
| 41 | + { |
| 42 | + if (null === $mode) |
| 43 | + { |
| 44 | + $mode = self::OPTIONAL; |
| 45 | + } |
| 46 | + else if (is_string($mode) || $mode > 7) |
| 47 | + { |
| 48 | + throw new \InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode)); |
| 49 | + } |
| 50 | + |
| 51 | + $this->name = $name; |
| 52 | + $this->mode = $mode; |
| 53 | + $this->description = $description; |
| 54 | + |
| 55 | + $this->setDefault($default); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns the argument name. |
| 60 | + * |
| 61 | + * @return string The argument name |
| 62 | + */ |
| 63 | + public function getName() |
| 64 | + { |
| 65 | + return $this->name; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns true if the argument is required. |
| 70 | + * |
| 71 | + * @return Boolean true if parameter mode is self::REQUIRED, false otherwise |
| 72 | + */ |
| 73 | + public function isRequired() |
| 74 | + { |
| 75 | + return self::REQUIRED === (self::REQUIRED & $this->mode); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns true if the argument can take multiple values. |
| 80 | + * |
| 81 | + * @return Boolean true if mode is self::IS_ARRAY, false otherwise |
| 82 | + */ |
| 83 | + public function isArray() |
| 84 | + { |
| 85 | + return self::IS_ARRAY === (self::IS_ARRAY & $this->mode); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Sets the default value. |
| 90 | + * |
| 91 | + * @param mixed $default The default value |
| 92 | + */ |
| 93 | + public function setDefault($default = null) |
| 94 | + { |
| 95 | + if (self::REQUIRED === $this->mode && null !== $default) |
| 96 | + { |
| 97 | + throw new \LogicException('Cannot set a default value except for Parameter::OPTIONAL mode.'); |
| 98 | + } |
| 99 | + |
| 100 | + if ($this->isArray()) |
| 101 | + { |
| 102 | + if (null === $default) |
| 103 | + { |
| 104 | + $default = array(); |
| 105 | + } |
| 106 | + else if (!is_array($default)) |
| 107 | + { |
| 108 | + throw new \LogicException('A default value for an array argument must be an array.'); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + $this->default = $default; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Returns the default value. |
| 117 | + * |
| 118 | + * @return mixed The default value |
| 119 | + */ |
| 120 | + public function getDefault() |
| 121 | + { |
| 122 | + return $this->default; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Returns the description text. |
| 127 | + * |
| 128 | + * @return string The description text |
| 129 | + */ |
| 130 | + public function getDescription() |
| 131 | + { |
| 132 | + return $this->description; |
| 133 | + } |
| 134 | +} |
0 commit comments