1<?php
2/**
3 * File containing the ezcConsoleArgument class.
4 *
5 * @package ConsoleTools
6 * @version 1.6.1
7 * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
8 * @license http://ez.no/licenses/new_bsd New BSD License
9 * @filesource
10 */
11
12/**
13 * The ezcConsoleArgument class represents an argument on the console.
14 * This class is the container to store information about an argument submitted
15 * to a shell application. It is used to define the appearance of an argument
16 * before parsing the parameter string and contains the received value
17 * afterwards. Argument objects are stored in an instance of the set class
18 * {@link ezcConsoleArguments} which is stored in
19 * {ezcConsoleInput::$argumentDefinition}.
20 *
21 * @property string $name      The name for the argument. Must be unique.
22 * @property int $type         The value type.
23 * @property string $shorthelp A short help text.
24 * @property string $longhelp  A long help text-
25 * @property bool $mandatory   Whether the argument is mandatory.
26 * @property mixed $default    A default value, if not mandatory.
27 * @property bool $multiple    Whether the argument accepts multiple values.
28 * @property-read mixed $value The value parsed from the parameter string, using
29 *                             {@link ezcConsoleInput::process()}.
30 * @package ConsoleTools
31 * @version 1.6.1
32 */
33class ezcConsoleArgument
34{
35    /**
36     * Properties
37     *
38     * @var array
39     */
40    protected $properties = array(
41        "name"      => null,
42        "type"      => ezcConsoleInput::TYPE_STRING,
43        "shorthelp" => "No help available.",
44        "longhelp"  => "There is no help for this argument available.",
45        "mandatory" => true,
46        "multiple"  => false,
47        "default"   => null,
48        "value"     => null,
49    );
50
51    /**
52     * Creates a new console argument object.
53     * Creates a new console argument object, which represents a single
54     * argument on the shell. Arguments are stored insiede
55     * {@link ezcConsoleArguments} which is used with {@link ezcConsoleInput}.
56     *
57     * For the type property see {@link ezcConsoleInput::TYPE_STRING} and
58     * {@link ezcConsoleInput::TYPE_INT}. If 1 argument is defined as optional
59     * ($mandatory = false), all following arguments are autolamtically
60     * considered optional, too.
61     *
62     * @param string $name      The name for the argument. Must be unique.
63     * @param int $type         The value type.
64     * @param string $shorthelp A short help text.
65     * @param string $longhelp  A long help text-
66     * @param bool $mandatory   Whether the argument is mandatory.
67     * @param bool $multiple    Whether the argument accepts multiple values.
68     * @param mixed $default    A default value, if not mandatory.
69     * @return void
70     */
71    public function __construct(
72        $name      = null,
73        $type      = ezcConsoleInput::TYPE_STRING,
74        $shorthelp = "No help available.",
75        $longhelp  = "There is no help for this argument available.",
76        $mandatory = true,
77        $multiple  = false,
78        $default   = null
79    )
80    {
81        if ( !is_string( $name ) || strlen( $name ) < 1 )
82        {
83            throw new ezcBaseValueException( "name", $name, "string, length > 0" );
84        }
85        $this->properties["name"] = $name;
86
87        $this->type               = $type;
88        $this->shorthelp          = $shorthelp;
89        $this->longhelp           = $longhelp;
90        $this->mandatory          = $mandatory;
91        $this->multiple           = $multiple;
92        $this->default            = $default;
93    }
94
95    /**
96     * Property set access.
97     *
98     * @param string $propertyName  Name of the property.
99     * @param string $propertyValue Valze for the property.
100     * @return void
101     * @ignore
102     */
103    public function __set( $propertyName, $propertyValue )
104    {
105        switch ( $propertyName )
106        {
107            case "name":
108                throw new ezcBasePropertyPermissionException( $propertyName, ezcBasePropertyPermissionException::READ );
109                break;
110            case "type":
111                if ( $propertyValue !== ezcConsoleInput::TYPE_INT && $propertyValue !== ezcConsoleInput::TYPE_STRING )
112                {
113                    throw new ezcBaseValueException( $propertyName, $propertyValue, "string, length > 0" );
114                }
115                break;
116            case "shorthelp":
117            case "longhelp":
118                if ( is_string( $propertyValue ) === false )
119                {
120                    throw new ezcBaseValueException( $propertyName, $propertyValue, "string" );
121                }
122                break;
123            case "mandatory":
124            case "multiple":
125                if ( is_bool( $propertyValue ) === false )
126                {
127                    throw new ezcBaseValueException( $propertyName, $propertyValue, "bool" );
128                }
129                break;
130            case "default":
131                if ( is_scalar( $propertyValue ) === false && is_array( $propertyValue ) === false && $propertyValue !== null )
132                {
133                    throw new ezcBaseValueException( $propertyName, $propertyValue, "array, scalar or null" );
134                }
135                break;
136            case "value":
137                if ( is_scalar( $propertyValue ) === false && is_array( $propertyValue ) === false && $propertyValue !== null )
138                {
139                    throw new ezcBaseValueException( $propertyName, $propertyValue, "string or null" );
140                }
141                break;
142            default:
143                throw new ezcBasePropertyNotFoundException( $propertyName );
144        }
145        $this->properties[$propertyName] = $propertyValue;
146    }
147
148    /**
149     * Property read access.
150     *
151     * @throws ezcBasePropertyNotFoundException
152     *         If the the desired property is not found.
153     *
154     * @param string $propertyName Name of the property.
155     * @return mixed Value of the property or null.
156     * @ignore
157     */
158    public function __get( $propertyName )
159    {
160        if ( isset( $this->$propertyName ) )
161        {
162            return $this->properties[$propertyName];
163        }
164        throw new ezcBasePropertyNotFoundException( $propertyName );
165    }
166
167    /**
168     * Property isset access.
169     *
170     * @param string $propertyName Name of the property.
171     * @return bool True is the property is set, otherwise false.
172     * @ignore
173     */
174    public function __isset( $propertyName )
175    {
176        return array_key_exists( $propertyName, $this->properties );
177    }
178}
179
180?>
181