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