• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     */
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        if ($this->key_type === GPBType::BOOL) {
95            // PHP associative array stores bool as integer for key.
96            return boolval($key);
97        } elseif ($this->key_type === GPBType::STRING) {
98            // PHP associative array stores int string as int for key.
99            return strval($key);
100        } else {
101            return $key;
102        }
103    }
104
105    /**
106     * Move to the next position.
107     *
108     * @return void
109     */
110    public function next()
111    {
112        return next($this->container);
113    }
114
115    /**
116     * Check whether there are more elements to iterate.
117     *
118     * @return bool True if there are more elements to iterate.
119     */
120    public function valid()
121    {
122        return key($this->container) !== null;
123    }
124}
125