| avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame^] | 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 | * MapField and MapFieldIter are used by generated protocol message classes to |
| 35 | * manipulate map fields. |
| 36 | */ |
| 37 | |
| 38 | namespace Google\Protobuf\Internal; |
| 39 | |
| 40 | /** |
| 41 | * MapFieldIter is used to iterate MapField. It is also need for the foreach |
| 42 | * syntax. |
| 43 | */ |
| 44 | class MapFieldIter implements \Iterator |
| 45 | { |
| 46 | |
| 47 | /** |
| 48 | * @ignore |
| 49 | */ |
| 50 | private $container; |
| 51 | |
| 52 | /** |
| 53 | * Create iterator instance for MapField. |
| 54 | * |
| 55 | * @param MapField The MapField instance for which this iterator is |
| 56 | * created. |
| 57 | * @param GPBType Map key type. |
| 58 | * @ignore |
| 59 | */ |
| 60 | public function __construct($container, $key_type) |
| 61 | { |
| 62 | $this->container = $container; |
| 63 | $this->key_type = $key_type; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Reset the status of the iterator |
| 68 | * |
| 69 | * @return void |
| 70 | */ |
| 71 | public function rewind() |
| 72 | { |
| 73 | return reset($this->container); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Return the element at the current position. |
| 78 | * |
| 79 | * @return object The element at the current position. |
| 80 | */ |
| 81 | public function current() |
| 82 | { |
| 83 | return current($this->container); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Return the current key. |
| 88 | * |
| 89 | * @return object The current key. |
| 90 | */ |
| 91 | public function key() |
| 92 | { |
| 93 | $key = key($this->container); |
| 94 | switch ($this->key_type) { |
| 95 | case GPBType::INT64: |
| 96 | case GPBType::UINT64: |
| 97 | case GPBType::FIXED64: |
| 98 | case GPBType::SFIXED64: |
| 99 | case GPBType::SINT64: |
| 100 | if (PHP_INT_SIZE === 8) { |
| 101 | return $key; |
| 102 | } |
| 103 | // Intentionally fall through |
| 104 | case GPBType::STRING: |
| 105 | // PHP associative array stores int string as int for key. |
| 106 | return strval($key); |
| 107 | case GPBType::BOOL: |
| 108 | // PHP associative array stores bool as integer for key. |
| 109 | return boolval($key); |
| 110 | default: |
| 111 | return $key; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Move to the next position. |
| 117 | * |
| 118 | * @return void |
| 119 | */ |
| 120 | public function next() |
| 121 | { |
| 122 | return next($this->container); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Check whether there are more elements to iterate. |
| 127 | * |
| 128 | * @return bool True if there are more elements to iterate. |
| 129 | */ |
| 130 | public function valid() |
| 131 | { |
| 132 | return key($this->container) !== null; |
| 133 | } |
| 134 | } |