Completed
Push — master ( 84ee08...7db43c )
by Todd
02:25
created

Args   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 98.53%

Importance

Changes 6
Bugs 0 Features 4
Metric Value
wmc 24
c 6
b 0
f 4
lcom 1
cbo 1
dl 0
loc 239
ccs 67
cts 68
cp 0.9853
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getArgs() 0 3 1
A setArgs() 0 4 1
A getCommand() 0 3 1
A setCommand() 0 4 1
A getMeta() 0 3 1
A setMeta() 0 4 1
A getOpts() 0 3 1
A setOpts() 0 4 1
A getOpt() 0 3 1
A setOpt() 0 8 2
A jsonSerialize() 0 8 1
A offsetExists() 0 3 1
A offsetGet() 0 3 1
A offsetSet() 0 3 1
A offsetUnset() 0 3 1
A addArg() 0 8 2
A getArg() 0 11 4
A setArg() 0 4 1
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2014 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Cli;
9
10
/**
11
 * This class represents the parsed and validated argument list.
12
 */
13
class Args implements \JsonSerializable, \ArrayAccess {
14
    protected $command;
15
    protected $opts;
16
    protected $args;
17
    protected $meta;
18
19
    /**
20
     * Initialize the {@link Args} instance.
21
     *
22
     * @param string $command The name of the command.
23
     * @param array $opts An array of command line options.
24
     * @param array $args A numeric array of command line args.
25
     */
26 36
    public function __construct($command = '', $opts = [], $args = []) {
27 36
        $this->command = $command;
28 36
        $this->opts = $opts;
29 36
        $this->args = $args;
30 36
        $this->meta = [];
31 36
    }
32
33
    /**
34
     * Add an argument to the args array.
35
     *
36
     * @param string $value The argument to add.
37
     * @param string|null $index The index to add the arg at.
38
     * @return Args Returns $this for fluent calls.
39
     */
40 4
    public function addArg($value, $index = null) {
41 4
        if ($index !== null) {
42 3
            $this->args[$index] = $value;
43 3
        } else {
44 1
            $this->args[] = $value;
45
        }
46 4
        return $this;
47
    }
48
49
    /**
50
     * Get an argument at a given index.
51
     *
52
     * Arguments can be accessed by name or index.
53
     *
54
     * @param string|int $index
55
     * @param mixed $default The default value to return if the argument is not found.
56
     * @return mixed Returns the argument at {@link $index} or {@link $default}.
57
     */
58 3
    public function getArg($index, $default = null) {
59 3
        if (array_key_exists($index, $this->args)) {
60 3
            return $this->args[$index];
61 2
        } elseif (is_int($index)) {
62 2
            $values = array_values($this->args);
63 2
            if (array_key_exists($index, $values)) {
64 2
                return $values[$index];
65
            }
66
        }
67 1
        return $default;
68
    }
69
70
    /**
71
     * Set an argument in the args array.
72
     *
73
     * @param string|int $index The index to set at.
74
     * @param mixed $value The value of the arg.
75
     * @return Args $this Returns $this for fluent calls.
76
     */
77 1
    public function setArg($index, $value) {
78 1
        $this->args[$index] = $value;
79 1
        return $this;
80
    }
81
82
    /**
83
     * Get the args array.
84
     *
85
     * @return array Returns the args array.
86
     */
87 28
    public function getArgs() {
88 28
        return $this->args;
89
    }
90
91
    /**
92
     * Set the args array.
93
     *
94
     * @param array $args The new args array.
95
     * @return Args Returns $this for fluent calls.
96
     */
97 28
    public function setArgs(array $args) {
98 28
        $this->args = $args;
99 28
        return $this;
100
    }
101
102
    /**
103
     * Get the name of the command associated with the args.
104
     *
105
     * @return string Returns the name of the command.
106
     */
107 33
    public function getCommand() {
108 33
        return $this->command;
109
    }
110
111
    /**
112
     * Set the name of the command associated with the args.
113
     *
114
     * @param string $command The new command.
115
     * @return Args Returns $this for fluent calls.
116
     */
117 4
    public function setCommand($command) {
118 4
        $this->command = $command;
119 4
        return $this;
120
    }
121
122
    /**
123
     * Get a meta value.
124
     *
125
     * @param string $name The name of the meta value.
126
     * @param mixed $default The default value to return if {@link $name} is not found.
127
     * @return mixed Returns the meta value or {@link $default} if it doesn't exist.
128
     */
129 7
    public function getMeta($name, $default = null) {
130 7
        return Cli::val($name, $this->meta, $default);
131
    }
132
133
    /**
134
     * Set a meta value.
135
     *
136
     * @param string $name The name of the meta value.
137
     * @param mixed $value The new meta value.
138
     * @return Args Returns $this for fluent setting.
139
     */
140 34
    public function setMeta($name, $value) {
141 34
        $this->meta[$name] = $value;
142 34
        return $this;
143
    }
144
145
    /**
146
     * Gets the entire options array.
147
     *
148
     * @return array Returns the current options array.
149
     */
150 27
    public function getOpts() {
151 27
        return $this->opts;
152
    }
153
154
    /**
155
     * Sets the entire options array.
156
     *
157
     * @param array $value Pass an array to set a new options array.
158
     * @return Args Returns $this for fluent calls.
159
     */
160 1
    public function setOpts(array $value) {
161 1
        $this->opts = $value;
162 1
        return $this;
163
    }
164
165
    /**
166
     * Get the value of a passed option.
167
     *
168
     * @param string $option The name of the option to get.
169
     * @param mixed $default The default value if the option does not exist.
170
     * @return mixed Returns the option or {@link $default} if it does not exist.
171
     */
172 31
    public function getOpt($option, $default = null) {
173 31
        return Cli::val($option, $this->opts, $default);
174
    }
175
176
    /**
177
     * Set an option.
178
     *
179
     * @param string $option The name of the option.
180
     * @param mixed $value The value of the option.
181
     * @return Args Returns $this for fluent calls.
182
     */
183 33
    public function setOpt($option, $value) {
184 33
        if ($value === null) {
185 1
            unset($this->opts[$option]);
186 1
        } else {
187 33
            $this->opts[$option] = $value;
188
        }
189 33
        return $this;
190
    }
191
192
    /**
193
     * Return the json serializable data for the args.
194
     *
195
     * @return array Returns an array of data that can be used to serialize the args to json.
196
     */
197 1
    public function jsonSerialize() {
198
        return [
199 1
            'command' => $this->command,
200 1
            'opts' => $this->opts,
201 1
            'args' => $this->args,
202 1
            'meta' => $this->meta
203 1
        ];
204
    }
205
206
    /**
207
     * Whether a offset exists.
208
     *
209
     * @param mixed $offset An offset to check for.
210
     * @return boolean true on success or false on failure.
211
     * The return value will be casted to boolean if non-boolean was returned.
212
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
213
     */
214 1
    public function offsetExists($offset) {
215 1
        return isset($this->opts[$offset]);
216
    }
217
218
    /**
219
     * Offset to retrieve.
220
     *
221
     * @param mixed $offset The offset to retrieve.
222
     * @return mixed Can return all value types.
223
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
224
     */
225 2
    public function offsetGet($offset) {
226 2
        return $this->getOpt($offset, null);
227
    }
228
229
    /**
230
     * Offset to set.
231
     *
232
     * @param mixed $offset The offset to assign the value to.
233
     * @param mixed $value The value to set.
234
     * @return void
235
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
236
     */
237 1
    public function offsetSet($offset, $value) {
238 1
        $this->setOpt($offset, $value);
239 1
    }
240
241
    /**
242
     * Offset to unset.
243
     *
244
     * @param mixed $offset The offset to unset.
245
     * @return void
246
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
247
     */
248 1
    public function offsetUnset($offset) {
249 1
        unset($this->opts[$offset]);
250 1
    }
251
}
252