|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WsdlToPhp\PackageBase; |
| 4 | + |
| 5 | +abstract class AbstractStructArrayBase extends AbstractStructBase implements \ArrayAccess, \Iterator, \Countable |
| 6 | +{ |
| 7 | + /** |
| 8 | + * Array that contains values when only one parameter is set when calling __construct method |
| 9 | + * @var array |
| 10 | + */ |
| 11 | + protected $internArray; |
| 12 | + /** |
| 13 | + * Bool that tells if array is set or not |
| 14 | + * @var bool |
| 15 | + */ |
| 16 | + protected $internArrayIsArray; |
| 17 | + /** |
| 18 | + * Items index browser |
| 19 | + * @var int |
| 20 | + */ |
| 21 | + protected $internArrayOffset; |
| 22 | + /** |
| 23 | + * Method returning alone attribute name when class is *array* type |
| 24 | + * This method has been overridden in real-array struct class |
| 25 | + * @return string |
| 26 | + */ |
| 27 | + abstract public function getAttributeName(); |
| 28 | + /** |
| 29 | + * Method alias to count |
| 30 | + * @uses ApiWsdlClass::count() |
| 31 | + * @return int |
| 32 | + */ |
| 33 | + public function length() |
| 34 | + { |
| 35 | + return $this->count(); |
| 36 | + } |
| 37 | + /** |
| 38 | + * Method returning item length, alias to length |
| 39 | + * @uses ApiWsdlClass::getInternArray() |
| 40 | + * @uses ApiWsdlClass::getInternArrayIsArray() |
| 41 | + * @return int |
| 42 | + */ |
| 43 | + public function count() |
| 44 | + { |
| 45 | + return $this->getInternArrayIsArray() ? count($this->getInternArray()) : -1; |
| 46 | + } |
| 47 | + /** |
| 48 | + * Method returning the current element |
| 49 | + * @uses ApiWsdlClass::offsetGet() |
| 50 | + * @return mixed |
| 51 | + */ |
| 52 | + public function current() |
| 53 | + { |
| 54 | + return $this->offsetGet($this->internArrayOffset); |
| 55 | + } |
| 56 | + /** |
| 57 | + * Method moving the current position to the next element |
| 58 | + * @uses ApiWsdlClass::getInternArrayOffset() |
| 59 | + * @uses ApiWsdlClass::setInternArrayOffset() |
| 60 | + * @return AbstractStructArrayBase |
| 61 | + */ |
| 62 | + public function next() |
| 63 | + { |
| 64 | + return $this->setInternArrayOffset($this->getInternArrayOffset() + 1); |
| 65 | + } |
| 66 | + /** |
| 67 | + * Method resetting itemOffset |
| 68 | + * @uses ApiWsdlClass::setInternArrayOffset() |
| 69 | + * @return int |
| 70 | + */ |
| 71 | + public function rewind() |
| 72 | + { |
| 73 | + return $this->setInternArrayOffset(0); |
| 74 | + } |
| 75 | + /** |
| 76 | + * Method checking if current itemOffset points to an existing item |
| 77 | + * @uses ApiWsdlClass::getInternArrayOffset() |
| 78 | + * @uses ApiWsdlClass::offsetExists() |
| 79 | + * @return bool |
| 80 | + */ |
| 81 | + public function valid() |
| 82 | + { |
| 83 | + return $this->offsetExists($this->getInternArrayOffset()); |
| 84 | + } |
| 85 | + /** |
| 86 | + * Method returning current itemOffset value, alias to getInternArrayOffset |
| 87 | + * @uses ApiWsdlClass::getInternArrayOffset() |
| 88 | + * @return int |
| 89 | + */ |
| 90 | + public function key() |
| 91 | + { |
| 92 | + return $this->getInternArrayOffset(); |
| 93 | + } |
| 94 | + /** |
| 95 | + * Method alias to offsetGet |
| 96 | + * @see ApiWsdlClass::offsetGet() |
| 97 | + * @uses ApiWsdlClass::offsetGet() |
| 98 | + * @param int $index |
| 99 | + * @return mixed |
| 100 | + */ |
| 101 | + public function item($index) |
| 102 | + { |
| 103 | + return $this->offsetGet($index); |
| 104 | + } |
| 105 | + /** |
| 106 | + * Default method adding item to array |
| 107 | + * @uses ApiWsdlClass::getAttributeName() |
| 108 | + * @uses ApiWsdlClass::__toString() |
| 109 | + * @uses ApiWsdlClass::_set() |
| 110 | + * @uses ApiWsdlClass::_get() |
| 111 | + * @uses ApiWsdlClass::setInternArray() |
| 112 | + * @uses ApiWsdlClass::setInternArrayIsArray() |
| 113 | + * @uses ApiWsdlClass::setInternArrayOffset() |
| 114 | + * @param mixed $item value |
| 115 | + * @return AbstractStructArrayBase |
| 116 | + */ |
| 117 | + public function add($item) |
| 118 | + { |
| 119 | + if (stripos(get_called_class(), 'array') !== false) { |
| 120 | + /** |
| 121 | + * init array |
| 122 | + */ |
| 123 | + if (!is_array($this->_get($this->getAttributeName()))) { |
| 124 | + $this->_set($this->getAttributeName(), array()); |
| 125 | + } |
| 126 | + /** |
| 127 | + * current array |
| 128 | + */ |
| 129 | + $currentArray = $this->_get($this->getAttributeName()); |
| 130 | + array_push($currentArray, $item); |
| 131 | + $this |
| 132 | + ->_set($this->getAttributeName(), $currentArray) |
| 133 | + ->setInternArray($currentArray) |
| 134 | + ->setInternArrayIsArray(true) |
| 135 | + ->setInternArrayOffset(0); |
| 136 | + } |
| 137 | + return $this; |
| 138 | + } |
| 139 | + /** |
| 140 | + * Method returning the first item |
| 141 | + * @uses ApiWsdlClass::item() |
| 142 | + * @return mixed |
| 143 | + */ |
| 144 | + public function first() |
| 145 | + { |
| 146 | + return $this->item(0); |
| 147 | + } |
| 148 | + /** |
| 149 | + * Method returning the last item |
| 150 | + * @uses ApiWsdlClass::item() |
| 151 | + * @uses ApiWsdlClass::length() |
| 152 | + * @return mixed |
| 153 | + */ |
| 154 | + public function last() |
| 155 | + { |
| 156 | + return $this->item($this->length() - 1); |
| 157 | + } |
| 158 | + /** |
| 159 | + * Method testing index in item |
| 160 | + * @uses ApiWsdlClass::getInternArrayIsArray() |
| 161 | + * @uses ApiWsdlClass::getInternArray() |
| 162 | + * @param int $offset |
| 163 | + * @return bool |
| 164 | + */ |
| 165 | + public function offsetExists($offset) |
| 166 | + { |
| 167 | + return ($this->getInternArrayIsArray() && array_key_exists($offset, $this->getInternArray())); |
| 168 | + } |
| 169 | + /** |
| 170 | + * Method returning the item at "index" value |
| 171 | + * @uses ApiWsdlClass::offsetExists() |
| 172 | + * @param int $offset |
| 173 | + * @return mixed |
| 174 | + */ |
| 175 | + public function offsetGet($offset) |
| 176 | + { |
| 177 | + return $this->offsetExists($offset) ? $this->internArray[$offset] : null; |
| 178 | + } |
| 179 | + /** |
| 180 | + * Method setting value at offset |
| 181 | + * @param mixed $offset |
| 182 | + * @param mixed $value |
| 183 | + * @return AbstractStructArrayBase |
| 184 | + */ |
| 185 | + public function offsetSet($offset, $value) |
| 186 | + { |
| 187 | + $this->internArray[$offset] = $value; |
| 188 | + return $this->_set($this->getAttributeName(), $this->internArray); |
| 189 | + } |
| 190 | + /** |
| 191 | + * Method unsetting value at offset |
| 192 | + * @param mixed $offset |
| 193 | + * @return AbstractStructArrayBase |
| 194 | + */ |
| 195 | + public function offsetUnset($offset) |
| 196 | + { |
| 197 | + if ($this->offsetExists($offset)) { |
| 198 | + unset($this->internArray[$offset]); |
| 199 | + $this->_set($this->getAttributeName(), $this->internArray); |
| 200 | + } |
| 201 | + return $this; |
| 202 | + } |
| 203 | + /** |
| 204 | + * Method returning intern array to iterate trough |
| 205 | + * @return array |
| 206 | + */ |
| 207 | + public function getInternArray() |
| 208 | + { |
| 209 | + return $this->internArray; |
| 210 | + } |
| 211 | + /** |
| 212 | + * Method setting intern array to iterate trough |
| 213 | + * @param array $internArray |
| 214 | + * @return AbstractStructArrayBase |
| 215 | + */ |
| 216 | + public function setInternArray($internArray) |
| 217 | + { |
| 218 | + $this->internArray = $internArray; |
| 219 | + return $this; |
| 220 | + } |
| 221 | + /** |
| 222 | + * Method returnint intern array index when iterating trough |
| 223 | + * @return int |
| 224 | + */ |
| 225 | + public function getInternArrayOffset() |
| 226 | + { |
| 227 | + return $this->internArrayOffset; |
| 228 | + } |
| 229 | + /** |
| 230 | + * Method initiating internArray |
| 231 | + * @uses ApiWsdlClass::setInternArray() |
| 232 | + * @uses ApiWsdlClass::setInternArrayOffset() |
| 233 | + * @uses ApiWsdlClass::setInternArrayIsArray() |
| 234 | + * @uses ApiWsdlClass::getAttributeName() |
| 235 | + * @uses ApiWsdlClass::initInternArray() |
| 236 | + * @uses ApiWsdlClass::__toString() |
| 237 | + * @param array $array the array to iterate trough |
| 238 | + * @param bool $internCall indicates that methods is calling itself |
| 239 | + * @return AbstractStructArrayBase |
| 240 | + */ |
| 241 | + public function initInternArray($array = array(), $internCall = false) |
| 242 | + { |
| 243 | + if (stripos(get_called_class(), 'array') !== false) { |
| 244 | + if (is_array($array) && count($array) > 0) { |
| 245 | + $this |
| 246 | + ->setInternArray($array) |
| 247 | + ->setInternArrayOffset(0) |
| 248 | + ->setInternArrayIsArray(true); |
| 249 | + } elseif (!$internCall && property_exists($this, $this->getAttributeName())) { |
| 250 | + $this->initInternArray($this->_get($this->getAttributeName()), true); |
| 251 | + } |
| 252 | + } |
| 253 | + return $this; |
| 254 | + } |
| 255 | + /** |
| 256 | + * Method setting intern array offset when iterating trough |
| 257 | + * @param int $internArrayOffset |
| 258 | + * @return AbstractStructArrayBase |
| 259 | + */ |
| 260 | + public function setInternArrayOffset($internArrayOffset) |
| 261 | + { |
| 262 | + $this->internArrayOffset = $internArrayOffset; |
| 263 | + return $this; |
| 264 | + } |
| 265 | + /** |
| 266 | + * Method returning true if intern array is an actual array |
| 267 | + * @return bool |
| 268 | + */ |
| 269 | + public function getInternArrayIsArray() |
| 270 | + { |
| 271 | + return $this->internArrayIsArray; |
| 272 | + } |
| 273 | + /** |
| 274 | + * Method setting if intern array is an actual array |
| 275 | + * @param bool $internArrayIsArray |
| 276 | + * @return AbstractStructArrayBase |
| 277 | + */ |
| 278 | + public function setInternArrayIsArray($internArrayIsArray = false) |
| 279 | + { |
| 280 | + $this->internArrayIsArray = $internArrayIsArray; |
| 281 | + return $this; |
| 282 | + } |
| 283 | +} |
0 commit comments