InputArgument.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Input;
  11. use Symfony\Component\Console\Exception\InvalidArgumentException;
  12. use Symfony\Component\Console\Exception\LogicException;
  13. /**
  14. * Represents a command line argument.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class InputArgument
  19. {
  20. const REQUIRED = 1;
  21. const OPTIONAL = 2;
  22. const IS_ARRAY = 4;
  23. private $name;
  24. private $mode;
  25. private $default;
  26. private $description;
  27. /**
  28. * Constructor.
  29. *
  30. * @param string $name The argument name
  31. * @param int $mode The argument mode: self::REQUIRED or self::OPTIONAL
  32. * @param string $description A description text
  33. * @param mixed $default The default value (for self::OPTIONAL mode only)
  34. *
  35. * @throws InvalidArgumentException When argument mode is not valid
  36. */
  37. public function __construct($name, $mode = null, $description = '', $default = null)
  38. {
  39. if (null === $mode) {
  40. $mode = self::OPTIONAL;
  41. } elseif (!is_int($mode) || $mode > 7 || $mode < 1) {
  42. throw new InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
  43. }
  44. $this->name = $name;
  45. $this->mode = $mode;
  46. $this->description = $description;
  47. $this->setDefault($default);
  48. }
  49. /**
  50. * Returns the argument name.
  51. *
  52. * @return string The argument name
  53. */
  54. public function getName()
  55. {
  56. return $this->name;
  57. }
  58. /**
  59. * Returns true if the argument is required.
  60. *
  61. * @return bool true if parameter mode is self::REQUIRED, false otherwise
  62. */
  63. public function isRequired()
  64. {
  65. return self::REQUIRED === (self::REQUIRED & $this->mode);
  66. }
  67. /**
  68. * Returns true if the argument can take multiple values.
  69. *
  70. * @return bool true if mode is self::IS_ARRAY, false otherwise
  71. */
  72. public function isArray()
  73. {
  74. return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
  75. }
  76. /**
  77. * Sets the default value.
  78. *
  79. * @param mixed $default The default value
  80. *
  81. * @throws LogicException When incorrect default value is given
  82. */
  83. public function setDefault($default = null)
  84. {
  85. if (self::REQUIRED === $this->mode && null !== $default) {
  86. throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
  87. }
  88. if ($this->isArray()) {
  89. if (null === $default) {
  90. $default = array();
  91. } elseif (!is_array($default)) {
  92. throw new LogicException('A default value for an array argument must be an array.');
  93. }
  94. }
  95. $this->default = $default;
  96. }
  97. /**
  98. * Returns the default value.
  99. *
  100. * @return mixed The default value
  101. */
  102. public function getDefault()
  103. {
  104. return $this->default;
  105. }
  106. /**
  107. * Returns the description text.
  108. *
  109. * @return string The description text
  110. */
  111. public function getDescription()
  112. {
  113. return $this->description;
  114. }
  115. }