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
 * Represents a command line argument.
13
 *
14
 * @package    symfony
15
 * @subpackage command
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfCommandArgument.class.php 21908 2009-09-11 12:06:21Z fabien $
18
 */
19
class sfCommandArgument
20
{
21
  const REQUIRED = 1;
22
  const OPTIONAL = 2;
23
 
24
  const IS_ARRAY = 4;
25
 
26
  protected
27
    $name    = null,
28
    $mode    = null,
29
    $default = null,
30
    $help    = '';
31
 
32
  /**
33
   * Constructor.
34
   *
35
   * @param string  $name    The argument name
36
   * @param integer $mode    The argument mode: self::REQUIRED or self::OPTIONAL
37
   * @param string  $help    A help text
38
   * @param mixed   $default The default value (for self::OPTIONAL mode only)
39
   */
40
  public function __construct($name, $mode = null, $help = '', $default = null)
41
  {
42
    if (null === $mode)
43
    {
44
      $mode = self::OPTIONAL;
45
    }
46
    else if (is_string($mode) || $mode > 7)
47
    {
48
      throw new sfCommandException(sprintf('Argument mode "%s" is not valid.', $mode));
49
    }
50
 
51
    $this->name = $name;
52
    $this->mode = $mode;
53
    $this->help = $help;
54
 
55
    $this->setDefault($default);
56
  }
57
 
58
  /**
59
   * Returns the argument name.
60
   *
61
   * @return string The argument name
62
   */
63
  public function getName()
64
  {
65
    return $this->name;
66
  }
67
 
68
  /**
69
   * Returns true if the argument is required.
70
   *
71
   * @return Boolean true if parameter mode is self::REQUIRED, false otherwise
72
   */
73
  public function isRequired()
74
  {
75
    return self::REQUIRED === (self::REQUIRED & $this->mode);
76
  }
77
 
78
  /**
79
   * Returns true if the argument can take multiple values.
80
   *
81
   * @return Boolean true if mode is self::IS_ARRAY, false otherwise
82
   */
83
  public function isArray()
84
  {
85
    return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
86
  }
87
 
88
  /**
89
   * Sets the default value.
90
   *
91
   * @param mixed $default The default value
92
   */
93
  public function setDefault($default = null)
94
  {
95
    if (self::REQUIRED === $this->mode && null !== $default)
96
    {
97
      throw new sfCommandException('Cannot set a default value except for sfCommandParameter::OPTIONAL mode.');
98
    }
99
 
100
    if ($this->isArray())
101
    {
102
      if (null === $default)
103
      {
104
        $default = array();
105
      }
106
      else if (!is_array($default))
107
      {
108
        throw new sfCommandException('A default value for an array argument must be an array.');
109
      }
110
    }
111
 
112
    $this->default = $default;
113
  }
114
 
115
  /**
116
   * Returns the default value.
117
   *
118
   * @return mixed The default value
119
   */
120
  public function getDefault()
121
  {
122
    return $this->default;
123
  }
124
 
125
  /**
126
   * Returns the help text.
127
   *
128
   * @return string The help text
129
   */
130
  public function getHelp()
131
  {
132
    return $this->help;
133
  }
134
}