xref: /aosp_15_r20/external/protobuf/php/src/Google/Protobuf/Internal/MapFieldIter.php (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
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
38namespace Google\Protobuf\Internal;
39
40/**
41 * MapFieldIter is used to iterate MapField. It is also need for the foreach
42 * syntax.
43 */
44class 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     * @todo need to add return type void (require update php version to 7.1)
71     */
72    #[\ReturnTypeWillChange]
73    public function rewind()
74    {
75        reset($this->container);
76    }
77
78    /**
79     * Return the element at the current position.
80     *
81     * @return object The element at the current position.
82     * @todo need to add return type mixed (require update php version to 8.0)
83     */
84    #[\ReturnTypeWillChange]
85    public function current()
86    {
87        return current($this->container);
88    }
89
90    /**
91     * Return the current key.
92     *
93     * @return object The current key.
94     * @todo need to add return type mixed (require update php version to 8.0)
95     */
96    #[\ReturnTypeWillChange]
97    public function key()
98    {
99        $key = key($this->container);
100        switch ($this->key_type) {
101            case GPBType::INT64:
102            case GPBType::UINT64:
103            case GPBType::FIXED64:
104            case GPBType::SFIXED64:
105            case GPBType::SINT64:
106                if (PHP_INT_SIZE === 8) {
107                    return $key;
108                }
109                // Intentionally fall through
110            case GPBType::STRING:
111                // PHP associative array stores int string as int for key.
112                return strval($key);
113            case GPBType::BOOL:
114                // PHP associative array stores bool as integer for key.
115                return boolval($key);
116            default:
117                return $key;
118        }
119    }
120
121    /**
122     * Move to the next position.
123     *
124     * @return void
125     * @todo need to add return type void (require update php version to 7.1)
126     */
127    #[\ReturnTypeWillChange]
128    public function next()
129    {
130        next($this->container);
131    }
132
133    /**
134     * Check whether there are more elements to iterate.
135     *
136     * @return bool True if there are more elements to iterate.
137     */
138    public function valid(): bool
139    {
140        return key($this->container) !== null;
141    }
142}
143