InputArgument.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. /**
  12. * Represents a command line argument.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @api
  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. * @api
  38. */
  39. public function __construct($name, $mode = null, $description = '', $default = null)
  40. {
  41. if (null === $mode) {
  42. $mode = self::OPTIONAL;
  43. } elseif (!is_int($mode) || $mode > 7 || $mode < 1) {
  44. throw new \InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
  45. }
  46. $this->name = $name;
  47. $this->mode = $mode;
  48. $this->description = $description;
  49. $this->setDefault($default);
  50. }
  51. /**
  52. * Returns the argument name.
  53. *
  54. * @return string The argument name
  55. */
  56. public function getName()
  57. {
  58. return $this->name;
  59. }
  60. /**
  61. * Returns true if the argument is required.
  62. *
  63. * @return bool true if parameter mode is self::REQUIRED, false otherwise
  64. */
  65. public function isRequired()
  66. {
  67. return self::REQUIRED === (self::REQUIRED & $this->mode);
  68. }
  69. /**
  70. * Returns true if the argument can take multiple values.
  71. *
  72. * @return bool true if mode is self::IS_ARRAY, false otherwise
  73. */
  74. public function isArray()
  75. {
  76. return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
  77. }
  78. /**
  79. * Sets the default value.
  80. *
  81. * @param mixed $default The default value
  82. *
  83. * @throws \LogicException When incorrect default value is given
  84. */
  85. public function setDefault($default = null)
  86. {
  87. if (self::REQUIRED === $this->mode && null !== $default) {
  88. throw new \LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
  89. }
  90. if ($this->isArray()) {
  91. if (null === $default) {
  92. $default = array();
  93. } elseif (!is_array($default)) {
  94. throw new \LogicException('A default value for an array argument must be an array.');
  95. }
  96. }
  97. $this->default = $default;
  98. }
  99. /**
  100. * Returns the default value.
  101. *
  102. * @return mixed The default value
  103. */
  104. public function getDefault()
  105. {
  106. return $this->default;
  107. }
  108. /**
  109. * Returns the description text.
  110. *
  111. * @return string The description text
  112. */
  113. public function getDescription()
  114. {
  115. return $this->description;
  116. }
  117. }