123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * Represent a set of command line arguments.
- *
- * @package symfony
- * @subpackage command
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfCommandArgumentSet.class.php 9076 2008-05-19 23:19:44Z Carl.Vondrick $
- */
- class sfCommandArgumentSet
- {
- protected
- $arguments = array(),
- $requiredCount = 0,
- $hasAnArrayArgument = false,
- $hasOptional = false;
- /**
- * Constructor.
- *
- * @param array $arguments An array of sfCommandArgument objects
- */
- public function __construct($arguments = array())
- {
- $this->setArguments($arguments);
- }
- /**
- * Sets the sfCommandArgument objects.
- *
- * @param array $arguments An array of sfCommandArgument objects
- */
- public function setArguments($arguments = array())
- {
- $this->arguments = array();
- $this->requiredCount = 0;
- $this->hasOptional = false;
- $this->addArguments($arguments);
- }
- /**
- * Add an array of sfCommandArgument objects.
- *
- * @param array $arguments An array of sfCommandArgument objects
- */
- public function addArguments($arguments = array())
- {
- if (!is_null($arguments))
- {
- foreach ($arguments as $argument)
- {
- $this->addArgument($argument);
- }
- }
- }
- /**
- * Add a sfCommandArgument objects.
- *
- * @param sfCommandArgument $argument A sfCommandArgument object
- */
- public function addArgument(sfCommandArgument $argument)
- {
- if (isset($this->arguments[$argument->getName()]))
- {
- throw new sfCommandException(sprintf('An argument with name "%s" already exist.', $argument->getName()));
- }
- if ($this->hasAnArrayArgument)
- {
- throw new sfCommandException('Cannot add an argument after an array argument.');
- }
- if ($argument->isRequired() && $this->hasOptional)
- {
- throw new sfCommandException('Cannot add a required argument after an optional one.');
- }
- if ($argument->isArray())
- {
- $this->hasAnArrayArgument = true;
- }
- if ($argument->isRequired())
- {
- ++$this->requiredCount;
- }
- else
- {
- $this->hasOptional = true;
- }
- $this->arguments[$argument->getName()] = $argument;
- }
- /**
- * Returns an argument by name.
- *
- * @param string $name The argument name
- *
- * @return sfCommandArgument A sfCommandArgument object
- */
- public function getArgument($name)
- {
- if (!$this->hasArgument($name))
- {
- throw new sfCommandException(sprintf('The "%s" argument does not exist.', $name));
- }
- return $this->arguments[$name];
- }
- /**
- * Returns true if an argument object exists by name.
- *
- * @param string $name The argument name
- *
- * @return Boolean true if the argument object exists, false otherwise
- */
- public function hasArgument($name)
- {
- return isset($this->arguments[$name]);
- }
- /**
- * Gets the array of sfCommandArgument objects.
- *
- * @return array An array of sfCommandArgument objects
- */
- public function getArguments()
- {
- return $this->arguments;
- }
- /**
- * Returns the number of arguments.
- *
- * @return integer The number of arguments
- */
- public function getArgumentCount()
- {
- return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments);
- }
- /**
- * Returns the number of required arguments.
- *
- * @return integer The number of required arguments
- */
- public function getArgumentRequiredCount()
- {
- return $this->requiredCount;
- }
- /**
- * Gets the default values.
- *
- * @return array An array of default values
- */
- public function getDefaults()
- {
- $values = array();
- foreach ($this->arguments as $argument)
- {
- $values[$argument->getName()] = $argument->getDefault();
- }
- return $values;
- }
- }
|