1<?php 2/** 3 * File containing the ezcConsoleArguments collection 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 * Collection class for ezcConsoleArgument objects. Used in {@link ezcConsoleInput}. 14 * 15 * @package ConsoleTools 16 * @version 1.6.1 17 */ 18class ezcConsoleArguments implements ArrayAccess, Iterator, Countable 19{ 20 /** 21 * Ordered list of arguments. 22 * 23 * @var array(ezcConsoleArgument) 24 */ 25 protected $ordered = array(); 26 27 /** 28 * Named list of arguments. 29 * 30 * @var array(string=>ezcConsoleArgument) 31 */ 32 protected $named = array(); 33 34 /** 35 * Returns if the given offset exists. 36 * This method is part of the ArrayAccess interface to allow access to the 37 * data of this object as if it was an array. Valid offsets are integers or 38 * strings. If an integer is used, it refers to the position in the command 39 * line. A string refers to the arguments name property. 40 * 41 * @param mixed $offset The offset to check. 42 * @return bool True when the offset exists, otherwise false. 43 * 44 * @throws ezcBaseValueException 45 * If the provided offset is neither an integer, nor a string. 46 */ 47 public function offsetExists( $offset ) 48 { 49 switch ( gettype( $offset ) ) 50 { 51 case "string": 52 return array_key_exists( $offset, $this->named ); 53 case "integer": 54 return array_key_exists( $offset, $this->ordered ); 55 default: 56 throw new ezcBaseValueException( "offset", $offset, "string or int" ); 57 } 58 } 59 60 /** 61 * Returns the element with the given offset. 62 * This method is part of the ArrayAccess interface to allow access to the 63 * data of this object as if it was an array. Valid offsets are integers or 64 * strings. If an integer is used, it refers to the position in the command 65 * line. A string refers to the arguments name property. 66 * 67 * @param string|integer $offset The offset to check. 68 * @return ezcConsoleArgument 69 * 70 * @throws ezcBaseValueException 71 * If the provided offset is neither an integer, nor a string. 72 */ 73 public function offsetGet( $offset ) 74 { 75 switch ( gettype( $offset ) ) 76 { 77 case "string": 78 if ( isset( $this[$offset] ) ) 79 { 80 return $this->named[$offset]; 81 } 82 break; 83 case "integer": 84 if ( isset( $this[$offset] ) ) 85 { 86 return $this->ordered[$offset]; 87 } 88 break; 89 default: 90 throw new ezcBaseValueException( "offset", $offset, "string or int" ); 91 } 92 throw new ezcBasePropertyNotFoundException( $offset ); 93 } 94 95 /** 96 * Set the element with the given offset. 97 * This method is part of the ArrayAccess interface to allow access to the 98 * data of this object as if it was an array. In contrast to the other 99 * ArrayAccess implementations of this class, this method allows only integer 100 * keys. 101 * 102 * @param int $offset The offset to assign an item to. 103 * @param ezcConsoleArgument $value The argument object to register. 104 * @return void 105 * 106 * @throws ezcBaseValueException 107 * If a non integer offset is provided. 108 * @throws ezcBaseValueException 109 * If the provided value is not of type {@ling ezcConsoleTableRow}. 110 * @throws ezcConsoleArgumentAlreadyRegisteredException 111 * If an argument with the given offset or name is already registered. 112 */ 113 public function offsetSet( $offset, $value ) 114 { 115 // Determine key if not set (using $obj[] = ...) 116 if ( $offset === null ) 117 { 118 $offset = count( $this->ordered ) === 0 ? 0 : max( array_keys( $this->ordered ) ) + 1; 119 } 120 121 // Set access only allowed with integer values 122 if ( !is_int( $offset ) ) 123 { 124 throw new ezcBaseValueException( "offset", $offset, "int" ); 125 } 126 127 switch ( true ) 128 { 129 case ( $value instanceof ezcConsoleArgument ): 130 if ( isset( $this->ordered[$offset] ) ) 131 { 132 throw new ezcConsoleArgumentAlreadyRegisteredException( $offset, ezcConsoleArgumentAlreadyRegisteredException::ORDERED ); 133 } 134 if ( isset( $this->named[$value->name] ) ) 135 { 136 throw new ezcConsoleArgumentAlreadyRegisteredException( $value->name, ezcConsoleArgumentAlreadyRegisteredException::NAMED ); 137 } 138 139 $this->named[$value->name] = $value; 140 $this->ordered[$offset] = $value; 141 break; 142 case ( $value === null ): 143 // Aliasing unset() with assignement to null 144 unset( $this->named[$this->ordered[$offset]->name] ); 145 unset( $this->ordered[$offset] ); 146 break; 147 default: 148 throw new ezcBaseValueException( "value", $value, "ezcConsoleArgument or null" ); 149 } 150 } 151 152 /** 153 * Unset the element with the given offset. 154 * This method is part of the ArrayAccess interface to allow access to the 155 * data of this object as if it was an array. In contrast to the other 156 * ArrayAccess implementations of this class, this method allows only integer 157 * keys. 158 * 159 * @param int $offset The offset to unset the value for. 160 * @return void 161 * 162 * @throws ezcBaseValueException 163 * If a non numeric row offset is used. 164 */ 165 public function offsetUnset( $offset ) 166 { 167 // Set access only allowed with integer values 168 if ( is_int( $offset ) === false ) 169 { 170 throw new ezcBaseValueException( "offset", $offset, "int" ); 171 } 172 173 unset( $this->named[$this->ordered[$offset]->name] ); 174 unset( $this->ordered[$offset] ); 175 } 176 177 /** 178 * Returns the currently selected argument from the list. 179 * Used by foreach-Loops. 180 * 181 * @return ezcConsoleArgument 182 */ 183 public function current() 184 { 185 return current( $this->ordered ); 186 } 187 188 /** 189 * Returns the key of the currently selected argument from the list. 190 * Used by foreach-Loops. In contrast to the iteration direction, which is 191 * defined by the ordered list of arguments, this is the name of the 192 * argument. 193 * 194 * @return string 195 */ 196 public function key() 197 { 198 return key( $this->ordered ); 199 } 200 201 /** 202 * Advances the internal pointer to the next argument and returns it. 203 * Used by foreach-Loops. 204 * 205 * @return ezcConsoleArgument 206 */ 207 public function next() 208 { 209 return next( $this->ordered ); 210 } 211 212 /** 213 * Rewinds the internal pointer to the first argument and returns it. 214 * Used by foreach-Loops. 215 * 216 * @return ezcConsoleArgument 217 */ 218 public function rewind() 219 { 220 // Called before foreach 221 ksort( $this->ordered ); 222 return reset( $this->ordered ); 223 } 224 225 /** 226 * Checks if the current position is valid. 227 * Used by foreach-Loops. 228 * 229 * @return bool 230 */ 231 public function valid() 232 { 233 return ( current( $this->ordered ) !== false ); 234 } 235 236 /** 237 * Returns the number of registered arguments. 238 * 239 * @return int 240 */ 241 public function count() 242 { 243 return count( $this->ordered ); 244 } 245} 246?> 247