1<?php 2 3// Protocol Buffers - Google's data interchange format 4// Copyright 2008 Google Inc. All rights reserved. 5// 6// Use of this source code is governed by a BSD-style 7// license that can be found in the LICENSE file or at 8// https://developers.google.com/open-source/licenses/bsd 9 10/** 11 * MapField and MapFieldIter are used by generated protocol message classes to 12 * manipulate map fields. 13 */ 14 15namespace Google\Protobuf\Internal; 16 17/** 18 * MapFieldIter is used to iterate MapField. It is also need for the foreach 19 * syntax. 20 */ 21class MapFieldIter implements \Iterator 22{ 23 24 /** 25 * @ignore 26 */ 27 private $container; 28 29 /** 30 * @ignore 31 */ 32 private $key_type; 33 34 /** 35 * Create iterator instance for MapField. 36 * 37 * @param array $container 38 * @param GPBType $key_type Map key type. 39 * @ignore 40 */ 41 public function __construct($container, $key_type) 42 { 43 $this->container = $container; 44 $this->key_type = $key_type; 45 } 46 47 /** 48 * Reset the status of the iterator 49 * 50 * @return void 51 * @todo need to add return type void (require update php version to 7.1) 52 */ 53 #[\ReturnTypeWillChange] 54 public function rewind() 55 { 56 reset($this->container); 57 } 58 59 /** 60 * Return the element at the current position. 61 * 62 * @return object The element at the current position. 63 * @todo need to add return type mixed (require update php version to 8.0) 64 */ 65 #[\ReturnTypeWillChange] 66 public function current() 67 { 68 return current($this->container); 69 } 70 71 /** 72 * Return the current key. 73 * 74 * @return object The current key. 75 * @todo need to add return type mixed (require update php version to 8.0) 76 */ 77 #[\ReturnTypeWillChange] 78 public function key() 79 { 80 $key = key($this->container); 81 switch ($this->key_type) { 82 case GPBType::INT64: 83 case GPBType::UINT64: 84 case GPBType::FIXED64: 85 case GPBType::SFIXED64: 86 case GPBType::SINT64: 87 if (PHP_INT_SIZE === 8) { 88 return $key; 89 } 90 // Intentionally fall through 91 case GPBType::STRING: 92 // PHP associative array stores int string as int for key. 93 return strval($key); 94 case GPBType::BOOL: 95 // PHP associative array stores bool as integer for key. 96 return boolval($key); 97 default: 98 return $key; 99 } 100 } 101 102 /** 103 * Move to the next position. 104 * 105 * @return void 106 * @todo need to add return type void (require update php version to 7.1) 107 */ 108 #[\ReturnTypeWillChange] 109 public function next() 110 { 111 next($this->container); 112 } 113 114 /** 115 * Check whether there are more elements to iterate. 116 * 117 * @return bool True if there are more elements to iterate. 118 */ 119 public function valid(): bool 120 { 121 return key($this->container) !== null; 122 } 123} 124