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 * MapField and MapFieldIter are used by generated protocol message classes to 35ffe3c632Sopenharmony_ci * manipulate map fields. 36ffe3c632Sopenharmony_ci */ 37ffe3c632Sopenharmony_ci 38ffe3c632Sopenharmony_cinamespace Google\Protobuf\Internal; 39ffe3c632Sopenharmony_ci 40ffe3c632Sopenharmony_ci/** 41ffe3c632Sopenharmony_ci * MapField is used by generated protocol message classes to manipulate map 42ffe3c632Sopenharmony_ci * fields. It can be used like native PHP array. 43ffe3c632Sopenharmony_ci */ 44ffe3c632Sopenharmony_ciclass MapField implements \ArrayAccess, \IteratorAggregate, \Countable 45ffe3c632Sopenharmony_ci{ 46ffe3c632Sopenharmony_ci /** 47ffe3c632Sopenharmony_ci * @ignore 48ffe3c632Sopenharmony_ci */ 49ffe3c632Sopenharmony_ci private $container; 50ffe3c632Sopenharmony_ci /** 51ffe3c632Sopenharmony_ci * @ignore 52ffe3c632Sopenharmony_ci */ 53ffe3c632Sopenharmony_ci private $key_type; 54ffe3c632Sopenharmony_ci /** 55ffe3c632Sopenharmony_ci * @ignore 56ffe3c632Sopenharmony_ci */ 57ffe3c632Sopenharmony_ci private $value_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 MapField. 69ffe3c632Sopenharmony_ci * 70ffe3c632Sopenharmony_ci * @param long $key_type Type of the stored key element. 71ffe3c632Sopenharmony_ci * @param long $value_type Type of the stored value element. 72ffe3c632Sopenharmony_ci * @param string $klass Message/Enum class name of value instance 73ffe3c632Sopenharmony_ci * (message/enum fields only). 74ffe3c632Sopenharmony_ci * @ignore 75ffe3c632Sopenharmony_ci */ 76ffe3c632Sopenharmony_ci public function __construct($key_type, $value_type, $klass = null) 77ffe3c632Sopenharmony_ci { 78ffe3c632Sopenharmony_ci $this->container = []; 79ffe3c632Sopenharmony_ci $this->key_type = $key_type; 80ffe3c632Sopenharmony_ci $this->value_type = $value_type; 81ffe3c632Sopenharmony_ci $this->klass = $klass; 82ffe3c632Sopenharmony_ci 83ffe3c632Sopenharmony_ci if ($this->value_type == GPBType::MESSAGE) { 84ffe3c632Sopenharmony_ci $pool = DescriptorPool::getGeneratedPool(); 85ffe3c632Sopenharmony_ci $desc = $pool->getDescriptorByClassName($klass); 86ffe3c632Sopenharmony_ci if ($desc == NULL) { 87ffe3c632Sopenharmony_ci new $klass; // No msg class instance has been created before. 88ffe3c632Sopenharmony_ci $desc = $pool->getDescriptorByClassName($klass); 89ffe3c632Sopenharmony_ci } 90ffe3c632Sopenharmony_ci $this->klass = $desc->getClass(); 91ffe3c632Sopenharmony_ci $this->legacy_klass = $desc->getLegacyClass(); 92ffe3c632Sopenharmony_ci } 93ffe3c632Sopenharmony_ci } 94ffe3c632Sopenharmony_ci 95ffe3c632Sopenharmony_ci /** 96ffe3c632Sopenharmony_ci * @ignore 97ffe3c632Sopenharmony_ci */ 98ffe3c632Sopenharmony_ci public function getKeyType() 99ffe3c632Sopenharmony_ci { 100ffe3c632Sopenharmony_ci return $this->key_type; 101ffe3c632Sopenharmony_ci } 102ffe3c632Sopenharmony_ci 103ffe3c632Sopenharmony_ci /** 104ffe3c632Sopenharmony_ci * @ignore 105ffe3c632Sopenharmony_ci */ 106ffe3c632Sopenharmony_ci public function getValueType() 107ffe3c632Sopenharmony_ci { 108ffe3c632Sopenharmony_ci return $this->value_type; 109ffe3c632Sopenharmony_ci } 110ffe3c632Sopenharmony_ci 111ffe3c632Sopenharmony_ci /** 112ffe3c632Sopenharmony_ci * @ignore 113ffe3c632Sopenharmony_ci */ 114ffe3c632Sopenharmony_ci public function getValueClass() 115ffe3c632Sopenharmony_ci { 116ffe3c632Sopenharmony_ci return $this->klass; 117ffe3c632Sopenharmony_ci } 118ffe3c632Sopenharmony_ci 119ffe3c632Sopenharmony_ci /** 120ffe3c632Sopenharmony_ci * @ignore 121ffe3c632Sopenharmony_ci */ 122ffe3c632Sopenharmony_ci public function getLegacyValueClass() 123ffe3c632Sopenharmony_ci { 124ffe3c632Sopenharmony_ci return $this->legacy_klass; 125ffe3c632Sopenharmony_ci } 126ffe3c632Sopenharmony_ci 127ffe3c632Sopenharmony_ci /** 128ffe3c632Sopenharmony_ci * Return the element at the given key. 129ffe3c632Sopenharmony_ci * 130ffe3c632Sopenharmony_ci * This will also be called for: $ele = $arr[$key] 131ffe3c632Sopenharmony_ci * 132ffe3c632Sopenharmony_ci * @param object $key The key of the element to be fetched. 133ffe3c632Sopenharmony_ci * @return object The stored element at given key. 134ffe3c632Sopenharmony_ci * @throws \ErrorException Invalid type for index. 135ffe3c632Sopenharmony_ci * @throws \ErrorException Non-existing index. 136ffe3c632Sopenharmony_ci */ 137ffe3c632Sopenharmony_ci public function offsetGet($key) 138ffe3c632Sopenharmony_ci { 139ffe3c632Sopenharmony_ci return $this->container[$key]; 140ffe3c632Sopenharmony_ci } 141ffe3c632Sopenharmony_ci 142ffe3c632Sopenharmony_ci /** 143ffe3c632Sopenharmony_ci * Assign the element at the given key. 144ffe3c632Sopenharmony_ci * 145ffe3c632Sopenharmony_ci * This will also be called for: $arr[$key] = $value 146ffe3c632Sopenharmony_ci * 147ffe3c632Sopenharmony_ci * @param object $key The key of the element to be fetched. 148ffe3c632Sopenharmony_ci * @param object $value The element to be assigned. 149ffe3c632Sopenharmony_ci * @return void 150ffe3c632Sopenharmony_ci * @throws \ErrorException Invalid type for key. 151ffe3c632Sopenharmony_ci * @throws \ErrorException Invalid type for value. 152ffe3c632Sopenharmony_ci * @throws \ErrorException Non-existing key. 153ffe3c632Sopenharmony_ci */ 154ffe3c632Sopenharmony_ci public function offsetSet($key, $value) 155ffe3c632Sopenharmony_ci { 156ffe3c632Sopenharmony_ci $this->checkKey($this->key_type, $key); 157ffe3c632Sopenharmony_ci 158ffe3c632Sopenharmony_ci switch ($this->value_type) { 159ffe3c632Sopenharmony_ci case GPBType::SFIXED32: 160ffe3c632Sopenharmony_ci case GPBType::SINT32: 161ffe3c632Sopenharmony_ci case GPBType::INT32: 162ffe3c632Sopenharmony_ci case GPBType::ENUM: 163ffe3c632Sopenharmony_ci GPBUtil::checkInt32($value); 164ffe3c632Sopenharmony_ci break; 165ffe3c632Sopenharmony_ci case GPBType::FIXED32: 166ffe3c632Sopenharmony_ci case GPBType::UINT32: 167ffe3c632Sopenharmony_ci GPBUtil::checkUint32($value); 168ffe3c632Sopenharmony_ci break; 169ffe3c632Sopenharmony_ci case GPBType::SFIXED64: 170ffe3c632Sopenharmony_ci case GPBType::SINT64: 171ffe3c632Sopenharmony_ci case GPBType::INT64: 172ffe3c632Sopenharmony_ci GPBUtil::checkInt64($value); 173ffe3c632Sopenharmony_ci break; 174ffe3c632Sopenharmony_ci case GPBType::FIXED64: 175ffe3c632Sopenharmony_ci case GPBType::UINT64: 176ffe3c632Sopenharmony_ci GPBUtil::checkUint64($value); 177ffe3c632Sopenharmony_ci break; 178ffe3c632Sopenharmony_ci case GPBType::FLOAT: 179ffe3c632Sopenharmony_ci GPBUtil::checkFloat($value); 180ffe3c632Sopenharmony_ci break; 181ffe3c632Sopenharmony_ci case GPBType::DOUBLE: 182ffe3c632Sopenharmony_ci GPBUtil::checkDouble($value); 183ffe3c632Sopenharmony_ci break; 184ffe3c632Sopenharmony_ci case GPBType::BOOL: 185ffe3c632Sopenharmony_ci GPBUtil::checkBool($value); 186ffe3c632Sopenharmony_ci break; 187ffe3c632Sopenharmony_ci case GPBType::STRING: 188ffe3c632Sopenharmony_ci GPBUtil::checkString($value, true); 189ffe3c632Sopenharmony_ci break; 190ffe3c632Sopenharmony_ci case GPBType::MESSAGE: 191ffe3c632Sopenharmony_ci if (is_null($value)) { 192ffe3c632Sopenharmony_ci trigger_error("Map element cannot be null.", E_USER_ERROR); 193ffe3c632Sopenharmony_ci } 194ffe3c632Sopenharmony_ci GPBUtil::checkMessage($value, $this->klass); 195ffe3c632Sopenharmony_ci break; 196ffe3c632Sopenharmony_ci default: 197ffe3c632Sopenharmony_ci break; 198ffe3c632Sopenharmony_ci } 199ffe3c632Sopenharmony_ci 200ffe3c632Sopenharmony_ci $this->container[$key] = $value; 201ffe3c632Sopenharmony_ci } 202ffe3c632Sopenharmony_ci 203ffe3c632Sopenharmony_ci /** 204ffe3c632Sopenharmony_ci * Remove the element at the given key. 205ffe3c632Sopenharmony_ci * 206ffe3c632Sopenharmony_ci * This will also be called for: unset($arr) 207ffe3c632Sopenharmony_ci * 208ffe3c632Sopenharmony_ci * @param object $key The key of the element to be removed. 209ffe3c632Sopenharmony_ci * @return void 210ffe3c632Sopenharmony_ci * @throws \ErrorException Invalid type for key. 211ffe3c632Sopenharmony_ci */ 212ffe3c632Sopenharmony_ci public function offsetUnset($key) 213ffe3c632Sopenharmony_ci { 214ffe3c632Sopenharmony_ci $this->checkKey($this->key_type, $key); 215ffe3c632Sopenharmony_ci unset($this->container[$key]); 216ffe3c632Sopenharmony_ci } 217ffe3c632Sopenharmony_ci 218ffe3c632Sopenharmony_ci /** 219ffe3c632Sopenharmony_ci * Check the existence of the element at the given key. 220ffe3c632Sopenharmony_ci * 221ffe3c632Sopenharmony_ci * This will also be called for: isset($arr) 222ffe3c632Sopenharmony_ci * 223ffe3c632Sopenharmony_ci * @param object $key The key of the element to be removed. 224ffe3c632Sopenharmony_ci * @return bool True if the element at the given key exists. 225ffe3c632Sopenharmony_ci * @throws \ErrorException Invalid type for key. 226ffe3c632Sopenharmony_ci */ 227ffe3c632Sopenharmony_ci public function offsetExists($key) 228ffe3c632Sopenharmony_ci { 229ffe3c632Sopenharmony_ci $this->checkKey($this->key_type, $key); 230ffe3c632Sopenharmony_ci return isset($this->container[$key]); 231ffe3c632Sopenharmony_ci } 232ffe3c632Sopenharmony_ci 233ffe3c632Sopenharmony_ci /** 234ffe3c632Sopenharmony_ci * @ignore 235ffe3c632Sopenharmony_ci */ 236ffe3c632Sopenharmony_ci public function getIterator() 237ffe3c632Sopenharmony_ci { 238ffe3c632Sopenharmony_ci return new MapFieldIter($this->container, $this->key_type); 239ffe3c632Sopenharmony_ci } 240ffe3c632Sopenharmony_ci 241ffe3c632Sopenharmony_ci /** 242ffe3c632Sopenharmony_ci * Return the number of stored elements. 243ffe3c632Sopenharmony_ci * 244ffe3c632Sopenharmony_ci * This will also be called for: count($arr) 245ffe3c632Sopenharmony_ci * 246ffe3c632Sopenharmony_ci * @return integer The number of stored elements. 247ffe3c632Sopenharmony_ci */ 248ffe3c632Sopenharmony_ci public function count() 249ffe3c632Sopenharmony_ci { 250ffe3c632Sopenharmony_ci return count($this->container); 251ffe3c632Sopenharmony_ci } 252ffe3c632Sopenharmony_ci 253ffe3c632Sopenharmony_ci /** 254ffe3c632Sopenharmony_ci * @ignore 255ffe3c632Sopenharmony_ci */ 256ffe3c632Sopenharmony_ci private function checkKey($key_type, &$key) 257ffe3c632Sopenharmony_ci { 258ffe3c632Sopenharmony_ci switch ($key_type) { 259ffe3c632Sopenharmony_ci case GPBType::SFIXED32: 260ffe3c632Sopenharmony_ci case GPBType::SINT32: 261ffe3c632Sopenharmony_ci case GPBType::INT32: 262ffe3c632Sopenharmony_ci GPBUtil::checkInt32($key); 263ffe3c632Sopenharmony_ci break; 264ffe3c632Sopenharmony_ci case GPBType::FIXED32: 265ffe3c632Sopenharmony_ci case GPBType::UINT32: 266ffe3c632Sopenharmony_ci GPBUtil::checkUint32($key); 267ffe3c632Sopenharmony_ci break; 268ffe3c632Sopenharmony_ci case GPBType::SFIXED64: 269ffe3c632Sopenharmony_ci case GPBType::SINT64: 270ffe3c632Sopenharmony_ci case GPBType::INT64: 271ffe3c632Sopenharmony_ci GPBUtil::checkInt64($key); 272ffe3c632Sopenharmony_ci break; 273ffe3c632Sopenharmony_ci case GPBType::FIXED64: 274ffe3c632Sopenharmony_ci case GPBType::UINT64: 275ffe3c632Sopenharmony_ci GPBUtil::checkUint64($key); 276ffe3c632Sopenharmony_ci break; 277ffe3c632Sopenharmony_ci case GPBType::BOOL: 278ffe3c632Sopenharmony_ci GPBUtil::checkBool($key); 279ffe3c632Sopenharmony_ci break; 280ffe3c632Sopenharmony_ci case GPBType::STRING: 281ffe3c632Sopenharmony_ci GPBUtil::checkString($key, true); 282ffe3c632Sopenharmony_ci break; 283ffe3c632Sopenharmony_ci default: 284ffe3c632Sopenharmony_ci trigger_error( 285ffe3c632Sopenharmony_ci "Given type cannot be map key.", 286ffe3c632Sopenharmony_ci E_USER_ERROR); 287ffe3c632Sopenharmony_ci break; 288ffe3c632Sopenharmony_ci } 289ffe3c632Sopenharmony_ci } 290ffe3c632Sopenharmony_ci} 291