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

Debug toolbar