## Apache Zeta Components Manual :: File Source for arguments.php

## Source for file arguments.php

Documentation is available at arguments.php

1. <?php
2. /**
3. * File containing the ezcConsoleArguments collection class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @package ConsoleTools
23. * @version //autogentag//
24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
25. * @filesource
26. */
28. /**
29. * Collection class for ezcConsoleArgument objects. Used in {@link ezcConsoleInput}.
30. *
31. * @package ConsoleTools
32. * @version //autogen//
33. */
34. class ezcConsoleArguments implements ArrayAccess, Iterator, Countable
35. {
36. /**
37. * Ordered list of arguments.
38. *
39. * @var array(ezcConsoleArgument)
40. */
41. protected $ordered = array();
43. /**
44. * Named list of arguments.
45. *
46. * @var array(string=>ezcConsoleArgument)
47. */
48. protected $named = array();
50. /**
51. * Returns if the given offset exists.
52. * This method is part of the ArrayAccess interface to allow access to the
53. * data of this object as if it was an array. Valid offsets are integers or
54. * strings. If an integer is used, it refers to the position in the command
55. * line. A string refers to the arguments name property.
56. *
57. * @param mixed $offset The offset to check.
58. * @return bool True when the offset exists, otherwise false.
59. *
60. * @throws ezcBaseValueException
61. * If the provided offset is neither an integer, nor a string.
62. */
63. public function offsetExists ( $offset )
64. {
65. switch ( gettype ( $offset ) )
66. {
67. case "string":
68. return array_key_exists ( $offset, $this-> named );
69. case "integer":
70. return array_key_exists ( $offset, $this-> ordered );
71. default:
72. throw new ezcBaseValueException ( "offset", $offset, "string or int" );
73. }
74. }
76. /**
77. * Returns the element with the given offset.
78. * This method is part of the ArrayAccess interface to allow access to the
79. * data of this object as if it was an array. Valid offsets are integers or
80. * strings. If an integer is used, it refers to the position in the command
81. * line. A string refers to the arguments name property.
82. *
83. * @param string|integer$offset The offset to check.
84. * @return ezcConsoleArgument
85. *
86. * @throws ezcBaseValueException
87. * If the provided offset is neither an integer, nor a string.
88. */
89. public function offsetGet ( $offset )
90. {
91. switch ( gettype ( $offset ) )
92. {
93. case "string":
94. if ( isset( $this[$offset] ) )
95. {
96. return $this-> named [$offset];
97. }
98. break;
99. case "integer":
100. if ( isset( $this[$offset] ) )
101. {
102. return $this-> ordered [$offset];
103. }
104. break;
105. default:
106. throw new ezcBaseValueException ( "offset", $offset, "string or int" );
107. }
108. throw new ezcBasePropertyNotFoundException ( $offset );
109. }
111. /**
112. * Set the element with the given offset.
113. * This method is part of the ArrayAccess interface to allow access to the
114. * data of this object as if it was an array. In contrast to the other
115. * ArrayAccess implementations of this class, this method allows only integer
116. * keys.
117. *
118. * @param int $offset The offset to assign an item to.
119. * @param ezcConsoleArgument $value The argument object to register.
120. * @return void
121. *
122. * @throws ezcBaseValueException
123. * If a non integer offset is provided.
124. * @throws ezcBaseValueException
125. * If the provided value is not of type {@ling ezcConsoleTableRow}.
126. * @throws ezcConsoleArgumentAlreadyRegisteredException
127. * If an argument with the given offset or name is already registered.
128. */
129. public function offsetSet ( $offset, $value )
130. {
131. // Determine key if not set (using $obj[] = ...)
132. if ( $offset === null )
133. {
134. $offset = count ( $this-> ordered ) === 0 ? 0 : max ( array_keys ( $this-> ordered ) ) + 1;
135. }
137. // Set access only allowed with integer values
138. if ( ! is_int ( $offset ) )
139. {
140. throw new ezcBaseValueException ( "offset", $offset, "int" );
141. }
143. switch ( true )
144. {
145. case ( $value instanceof ezcConsoleArgument ):
146. if ( isset( $this-> ordered [$offset] ) )
147. {
148. throw new ezcConsoleArgumentAlreadyRegisteredException ( $offset, ezcConsoleArgumentAlreadyRegisteredException ::ORDERED );
149. }
150. if ( isset( $this-> named [$value->name] ) )
151. {
152. throw new ezcConsoleArgumentAlreadyRegisteredException ( $value->name, ezcConsoleArgumentAlreadyRegisteredException ::NAMED );
153. }
155. $this-> named [$value->name] = $value;
156. $this-> ordered [$offset] = $value;
157. break;
158. case ( $value === null ):
159. // Aliasing unset() with assignement to null
160. unset( $this-> named [$this-> ordered [$offset]->name] );
161. unset( $this-> ordered [$offset] );
162. break;
163. default:
164. throw new ezcBaseValueException ( "value", $value, "ezcConsoleArgument or null" );
165. }
166. }
168. /**
169. * Unset the element with the given offset.
170. * This method is part of the ArrayAccess interface to allow access to the
171. * data of this object as if it was an array. In contrast to the other
172. * ArrayAccess implementations of this class, this method allows only integer
173. * keys.
174. *
175. * @param int $offset The offset to unset the value for.
176. * @return void
177. *
178. * @throws ezcBaseValueException
179. * If a non numeric row offset is used.
180. */
181. public function offsetUnset ( $offset )
182. {
183. // Set access only allowed with integer values
184. if ( is_int ( $offset ) === false )
185. {
186. throw new ezcBaseValueException ( "offset", $offset, "int" );
187. }
189. unset( $this-> named [$this-> ordered [$offset]->name] );
190. unset( $this-> ordered [$offset] );
191. }
193. /**
194. * Returns the currently selected argument from the list.
195. * Used by foreach-Loops.
196. *
197. * @return ezcConsoleArgument
198. */
199. public function current ()
200. {
201. return current ( $this-> ordered );
202. }
204. /**
205. * Returns the key of the currently selected argument from the list.
206. * Used by foreach-Loops. In contrast to the iteration direction, which is
207. * defined by the ordered list of arguments, this is the name of the
208. * argument.
209. *
210. * @return string
211. */
212. public function key ()
213. {
214. return key ( $this-> ordered );
215. }
217. /**
218. * Advances the internal pointer to the next argument and returns it.
219. * Used by foreach-Loops.
220. *
221. * @return ezcConsoleArgument
222. */
223. public function next ()
224. {
225. return next ( $this-> ordered );
226. }
228. /**
229. * Rewinds the internal pointer to the first argument and returns it.
230. * Used by foreach-Loops.
231. *
232. * @return ezcConsoleArgument
233. */
234. public function rewind ()
235. {
236. // Called before foreach
237. ksort ( $this-> ordered );
238. return reset ( $this-> ordered );
239. }
241. /**
242. * Checks if the current position is valid.
243. * Used by foreach-Loops.
244. *
245. * @return bool
246. */
247. public function valid ()
248. {
249. return ( current ( $this-> ordered ) !== false );
250. }
252. /**
253. * Returns the number of registered arguments.
254. *
255. * @return int
256. */
257. public function count ()
258. {
259. return count ( $this-> ordered );
260. }
261. }
262. ?>

Documentation generated by phpDocumentor 1.4.3