OLD | NEW |
(Empty) | |
| 1 <?php |
| 2 |
| 3 // Protocol Buffers - Google's data interchange format |
| 4 // Copyright 2008 Google Inc. All rights reserved. |
| 5 // https://developers.google.com/protocol-buffers/ |
| 6 // |
| 7 // Redistribution and use in source and binary forms, with or without |
| 8 // modification, are permitted provided that the following conditions are |
| 9 // met: |
| 10 // |
| 11 // * Redistributions of source code must retain the above copyright |
| 12 // notice, this list of conditions and the following disclaimer. |
| 13 // * Redistributions in binary form must reproduce the above |
| 14 // copyright notice, this list of conditions and the following disclaimer |
| 15 // in the documentation and/or other materials provided with the |
| 16 // distribution. |
| 17 // * Neither the name of Google Inc. nor the names of its |
| 18 // contributors may be used to endorse or promote products derived from |
| 19 // this software without specific prior written permission. |
| 20 // |
| 21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
| 33 /** |
| 34 * RepeatedField and RepeatedFieldIter are used by generated protocol message |
| 35 * classes to manipulate repeated fields. |
| 36 */ |
| 37 |
| 38 namespace Google\Protobuf\Internal; |
| 39 |
| 40 use Google\Protobuf\Internal\GPBType; |
| 41 use Google\Protobuf\Internal\GPBUtil; |
| 42 |
| 43 /** |
| 44 * RepeatedFieldIter is used to iterate RepeatedField. It is also need for the |
| 45 * foreach syntax. |
| 46 */ |
| 47 class RepeatedFieldIter implements \Iterator |
| 48 { |
| 49 |
| 50 /** |
| 51 * @ignore |
| 52 */ |
| 53 private $position; |
| 54 /** |
| 55 * @ignore |
| 56 */ |
| 57 private $container; |
| 58 |
| 59 /** |
| 60 * Create iterator instance for RepeatedField. |
| 61 * |
| 62 * @param RepeatedField The RepeatedField instance for which this iterator |
| 63 * is created. |
| 64 * @ignore |
| 65 */ |
| 66 public function __construct($container) |
| 67 { |
| 68 $this->position = 0; |
| 69 $this->container = $container; |
| 70 } |
| 71 |
| 72 /** |
| 73 * Reset the status of the iterator |
| 74 * |
| 75 * @return void |
| 76 */ |
| 77 public function rewind() |
| 78 { |
| 79 $this->position = 0; |
| 80 } |
| 81 |
| 82 /** |
| 83 * Return the element at the current position. |
| 84 * |
| 85 * @return object The element at the current position. |
| 86 */ |
| 87 public function current() |
| 88 { |
| 89 return $this->container[$this->position]; |
| 90 } |
| 91 |
| 92 /** |
| 93 * Return the current position. |
| 94 * |
| 95 * @return integer The current position. |
| 96 */ |
| 97 public function key() |
| 98 { |
| 99 return $this->position; |
| 100 } |
| 101 |
| 102 /** |
| 103 * Move to the next position. |
| 104 * |
| 105 * @return void |
| 106 */ |
| 107 public function next() |
| 108 { |
| 109 ++$this->position; |
| 110 } |
| 111 |
| 112 /** |
| 113 * Check whether there are more elements to iterate. |
| 114 * |
| 115 * @return bool True if there are more elements to iterate. |
| 116 */ |
| 117 public function valid() |
| 118 { |
| 119 return isset($this->container[$this->position]); |
| 120 } |
| 121 } |
| 122 |
| 123 /** |
| 124 * RepeatedField is used by generated protocol message classes to manipulate |
| 125 * repeated fields. It can be used like native PHP array. |
| 126 */ |
| 127 class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable |
| 128 { |
| 129 |
| 130 /** |
| 131 * @ignore |
| 132 */ |
| 133 private $container; |
| 134 /** |
| 135 * @ignore |
| 136 */ |
| 137 private $type; |
| 138 /** |
| 139 * @ignore |
| 140 */ |
| 141 private $klass; |
| 142 |
| 143 /** |
| 144 * Constructs an instance of RepeatedField. |
| 145 * |
| 146 * @param long $type Type of the stored element. |
| 147 * @param string $klass Message/Enum class name (message/enum fields only). |
| 148 * @ignore |
| 149 */ |
| 150 public function __construct($type, $klass = null) |
| 151 { |
| 152 $this->container = []; |
| 153 $this->type = $type; |
| 154 $this->klass = $klass; |
| 155 } |
| 156 |
| 157 /** |
| 158 * @ignore |
| 159 */ |
| 160 public function getType() |
| 161 { |
| 162 return $this->type; |
| 163 } |
| 164 |
| 165 /** |
| 166 * @ignore |
| 167 */ |
| 168 public function getClass() |
| 169 { |
| 170 return $this->klass; |
| 171 } |
| 172 |
| 173 /** |
| 174 * Return the element at the given index. |
| 175 * |
| 176 * This will also be called for: $ele = $arr[0] |
| 177 * |
| 178 * @param long $offset The index of the element to be fetched. |
| 179 * @return object The stored element at given index. |
| 180 * @throws ErrorException Invalid type for index. |
| 181 * @throws ErrorException Non-existing index. |
| 182 */ |
| 183 public function offsetGet($offset) |
| 184 { |
| 185 return $this->container[$offset]; |
| 186 } |
| 187 |
| 188 /** |
| 189 * Assign the element at the given index. |
| 190 * |
| 191 * This will also be called for: $arr []= $ele and $arr[0] = ele |
| 192 * |
| 193 * @param long $offset The index of the element to be assigned. |
| 194 * @param object $value The element to be assigned. |
| 195 * @return void |
| 196 * @throws ErrorException Invalid type for index. |
| 197 * @throws ErrorException Non-existing index. |
| 198 * @throws ErrorException Incorrect type of the element. |
| 199 */ |
| 200 public function offsetSet($offset, $value) |
| 201 { |
| 202 switch ($this->type) { |
| 203 case GPBType::INT32: |
| 204 GPBUtil::checkInt32($value); |
| 205 break; |
| 206 case GPBType::UINT32: |
| 207 GPBUtil::checkUint32($value); |
| 208 break; |
| 209 case GPBType::INT64: |
| 210 GPBUtil::checkInt64($value); |
| 211 break; |
| 212 case GPBType::UINT64: |
| 213 GPBUtil::checkUint64($value); |
| 214 break; |
| 215 case GPBType::FLOAT: |
| 216 GPBUtil::checkFloat($value); |
| 217 break; |
| 218 case GPBType::DOUBLE: |
| 219 GPBUtil::checkDouble($value); |
| 220 break; |
| 221 case GPBType::BOOL: |
| 222 GPBUtil::checkBool($value); |
| 223 break; |
| 224 case GPBType::STRING: |
| 225 GPBUtil::checkString($value, true); |
| 226 break; |
| 227 case GPBType::MESSAGE: |
| 228 GPBUtil::checkMessage($value, $this->klass); |
| 229 break; |
| 230 default: |
| 231 break; |
| 232 } |
| 233 if (is_null($offset)) { |
| 234 $this->container[] = $value; |
| 235 } else { |
| 236 $count = count($this->container); |
| 237 if (!is_numeric($offset) || $offset < 0 || $offset >= $count) { |
| 238 trigger_error( |
| 239 "Cannot modify element at the given index", |
| 240 E_USER_ERROR); |
| 241 return; |
| 242 } |
| 243 $this->container[$offset] = $value; |
| 244 } |
| 245 } |
| 246 |
| 247 /** |
| 248 * Remove the element at the given index. |
| 249 * |
| 250 * This will also be called for: unset($arr) |
| 251 * |
| 252 * @param long $offset The index of the element to be removed. |
| 253 * @return void |
| 254 * @throws ErrorException Invalid type for index. |
| 255 * @throws ErrorException The element to be removed is not at the end of the |
| 256 * RepeatedField. |
| 257 */ |
| 258 public function offsetUnset($offset) |
| 259 { |
| 260 $count = count($this->container); |
| 261 if (!is_numeric($offset) || $count === 0 || $offset !== $count - 1) { |
| 262 trigger_error( |
| 263 "Cannot remove element at the given index", |
| 264 E_USER_ERROR); |
| 265 return; |
| 266 } |
| 267 array_pop($this->container); |
| 268 } |
| 269 |
| 270 /** |
| 271 * Check the existence of the element at the given index. |
| 272 * |
| 273 * This will also be called for: isset($arr) |
| 274 * |
| 275 * @param long $offset The index of the element to be removed. |
| 276 * @return bool True if the element at the given offset exists. |
| 277 * @throws ErrorException Invalid type for index. |
| 278 */ |
| 279 public function offsetExists($offset) |
| 280 { |
| 281 return isset($this->container[$offset]); |
| 282 } |
| 283 |
| 284 /** |
| 285 * @ignore |
| 286 */ |
| 287 public function getIterator() |
| 288 { |
| 289 return new RepeatedFieldIter($this->container); |
| 290 } |
| 291 |
| 292 /** |
| 293 * Return the number of stored elements. |
| 294 * |
| 295 * This will also be called for: count($arr) |
| 296 * |
| 297 * @return integer The number of stored elements. |
| 298 */ |
| 299 public function count() |
| 300 { |
| 301 return count($this->container); |
| 302 } |
| 303 } |
OLD | NEW |