1 | <?php |
||
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 = []) { |
|
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() { |
|
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) { |
|
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() { |
|
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) { |
|
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) { |
|
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) { |
|
144 | |||
145 | /** |
||
146 | * Gets the entire options array. |
||
147 | * |
||
148 | * @return array Returns the current options array. |
||
149 | */ |
||
150 | 27 | public function getOpts() { |
|
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) { |
|
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) { |
|
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) { |
|
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() { |
|
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) { |
|
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) { |
|
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) { |
|
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) { |
|
251 | } |
||
252 |