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 * RepeatedField and RepeatedFieldIter are used by generated protocol message
35 * classes to manipulate repeated fields.
36 */
37
38namespace Google\Protobuf\Internal;
39
40/**
41 * RepeatedFieldIter is used to iterate RepeatedField. It is also need for the
42 * foreach syntax.
43 */
44class RepeatedFieldIter implements \Iterator
45{
46
47    /**
48     * @ignore
49     */
50    private $position;
51    /**
52     * @ignore
53     */
54    private $container;
55
56    /**
57     * Create iterator instance for RepeatedField.
58     *
59     * @param RepeatedField The RepeatedField instance for which this iterator
60     * is created.
61     * @ignore
62     */
63    public function __construct($container)
64    {
65        $this->position = 0;
66        $this->container = $container;
67    }
68
69    /**
70     * Reset the status of the iterator
71     *
72     * @return void
73     */
74    public function rewind()
75    {
76        $this->position = 0;
77    }
78
79    /**
80     * Return the element at the current position.
81     *
82     * @return object The element at the current position.
83     */
84    public function current()
85    {
86        return $this->container[$this->position];
87    }
88
89    /**
90     * Return the current position.
91     *
92     * @return integer The current position.
93     */
94    public function key()
95    {
96        return $this->position;
97    }
98
99    /**
100     * Move to the next position.
101     *
102     * @return void
103     */
104    public function next()
105    {
106        ++$this->position;
107    }
108
109    /**
110     * Check whether there are more elements to iterate.
111     *
112     * @return bool True if there are more elements to iterate.
113     */
114    public function valid()
115    {
116        return isset($this->container[$this->position]);
117    }
118}
119