sfCommandArgumentSet.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Represent a set of command line arguments.
  11. *
  12. * @package symfony
  13. * @subpackage command
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfCommandArgumentSet.class.php 17858 2009-05-01 21:22:50Z FabianLange $
  16. */
  17. class sfCommandArgumentSet
  18. {
  19. protected
  20. $arguments = array(),
  21. $requiredCount = 0,
  22. $hasAnArrayArgument = false,
  23. $hasOptional = false;
  24. /**
  25. * Constructor.
  26. *
  27. * @param array $arguments An array of sfCommandArgument objects
  28. */
  29. public function __construct($arguments = array())
  30. {
  31. $this->setArguments($arguments);
  32. }
  33. /**
  34. * Sets the sfCommandArgument objects.
  35. *
  36. * @param array $arguments An array of sfCommandArgument objects
  37. */
  38. public function setArguments($arguments = array())
  39. {
  40. $this->arguments = array();
  41. $this->requiredCount = 0;
  42. $this->hasOptional = false;
  43. $this->addArguments($arguments);
  44. }
  45. /**
  46. * Add an array of sfCommandArgument objects.
  47. *
  48. * @param array $arguments An array of sfCommandArgument objects
  49. */
  50. public function addArguments($arguments = array())
  51. {
  52. if (!is_null($arguments))
  53. {
  54. foreach ($arguments as $argument)
  55. {
  56. $this->addArgument($argument);
  57. }
  58. }
  59. }
  60. /**
  61. * Add a sfCommandArgument objects.
  62. *
  63. * @param sfCommandArgument $argument A sfCommandArgument object
  64. */
  65. public function addArgument(sfCommandArgument $argument)
  66. {
  67. if (isset($this->arguments[$argument->getName()]))
  68. {
  69. throw new sfCommandException(sprintf('An argument with name "%s" already exist.', $argument->getName()));
  70. }
  71. if ($this->hasAnArrayArgument)
  72. {
  73. throw new sfCommandException('Cannot add an argument after an array argument.');
  74. }
  75. if ($argument->isRequired() && $this->hasOptional)
  76. {
  77. throw new sfCommandException('Cannot add a required argument after an optional one.');
  78. }
  79. if ($argument->isArray())
  80. {
  81. $this->hasAnArrayArgument = true;
  82. }
  83. if ($argument->isRequired())
  84. {
  85. ++$this->requiredCount;
  86. }
  87. else
  88. {
  89. $this->hasOptional = true;
  90. }
  91. $this->arguments[$argument->getName()] = $argument;
  92. }
  93. /**
  94. * Returns an argument by name.
  95. *
  96. * @param string $name The argument name
  97. *
  98. * @return sfCommandArgument A sfCommandArgument object
  99. */
  100. public function getArgument($name)
  101. {
  102. if (!$this->hasArgument($name))
  103. {
  104. throw new sfCommandException(sprintf('The "%s" argument does not exist.', $name));
  105. }
  106. return $this->arguments[$name];
  107. }
  108. /**
  109. * Returns true if an argument object exists by name.
  110. *
  111. * @param string $name The argument name
  112. *
  113. * @return Boolean true if the argument object exists, false otherwise
  114. */
  115. public function hasArgument($name)
  116. {
  117. return isset($this->arguments[$name]);
  118. }
  119. /**
  120. * Gets the array of sfCommandArgument objects.
  121. *
  122. * @return array An array of sfCommandArgument objects
  123. */
  124. public function getArguments()
  125. {
  126. return $this->arguments;
  127. }
  128. /**
  129. * Returns the number of arguments.
  130. *
  131. * @return integer The number of arguments
  132. */
  133. public function getArgumentCount()
  134. {
  135. return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments);
  136. }
  137. /**
  138. * Returns the number of required arguments.
  139. *
  140. * @return integer The number of required arguments
  141. */
  142. public function getArgumentRequiredCount()
  143. {
  144. return $this->requiredCount;
  145. }
  146. /**
  147. * Gets the default values.
  148. *
  149. * @return array An array of default values
  150. */
  151. public function getDefaults()
  152. {
  153. $values = array();
  154. foreach ($this->arguments as $argument)
  155. {
  156. $values[$argument->getName()] = $argument->getDefault();
  157. }
  158. return $values;
  159. }
  160. }