Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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