Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: third_party/protobuf/php/src/Google/Protobuf/Internal/MapField.php

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 * @ignore
58 */
59 public function __construct($container)
60 {
61 $this->container = $container;
62 }
63
64 /**
65 * Reset the status of the iterator
66 *
67 * @return void
68 */
69 public function rewind()
70 {
71 return reset($this->container);
72 }
73
74 /**
75 * Return the element at the current position.
76 *
77 * @return object The element at the current position.
78 */
79 public function current()
80 {
81 return current($this->container);
82 }
83
84 /**
85 * Return the current key.
86 *
87 * @return object The current key.
88 */
89 public function key()
90 {
91 return key($this->container);
92 }
93
94 /**
95 * Move to the next position.
96 *
97 * @return void
98 */
99 public function next()
100 {
101 return next($this->container);
102 }
103
104 /**
105 * Check whether there are more elements to iterate.
106 *
107 * @return bool True if there are more elements to iterate.
108 */
109 public function valid()
110 {
111 return key($this->container) !== null;
112 }
113 }
114
115 /**
116 * @ignore
117 */
118 function checkKey($key_type, &$key)
119 {
120 switch ($key_type) {
121 case GPBType::INT32:
122 GPBUtil::checkInt32($key);
123 break;
124 case GPBType::UINT32:
125 GPBUtil::checkUint32($key);
126 break;
127 case GPBType::INT64:
128 GPBUtil::checkInt64($key);
129 break;
130 case GPBType::UINT64:
131 GPBUtil::checkUint64($key);
132 break;
133 case GPBType::FIXED64:
134 GPBUtil::checkUint64($key);
135 break;
136 case GPBType::FIXED32:
137 GPBUtil::checkUint32($key);
138 break;
139 case GPBType::SFIXED64:
140 GPBUtil::checkInt64($key);
141 break;
142 case GPBType::SFIXED32:
143 GPBUtil::checkInt32($key);
144 break;
145 case GPBType::SINT64:
146 GPBUtil::checkInt64($key);
147 break;
148 case GPBType::SINT32:
149 GPBUtil::checkInt32($key);
150 break;
151 case GPBType::BOOL:
152 GPBUtil::checkBool($key);
153 break;
154 case GPBType::STRING:
155 GPBUtil::checkString($key, true);
156 break;
157 default:
158 var_dump($key_type);
159 trigger_error(
160 "Given type cannot be map key.",
161 E_USER_ERROR);
162 break;
163 }
164 }
165
166 /**
167 * MapField is used by generated protocol message classes to manipulate map
168 * fields. It can be used like native PHP array.
169 */
170 class MapField implements \ArrayAccess, \IteratorAggregate, \Countable
171 {
172 /**
173 * @ignore
174 */
175 private $container;
176 /**
177 * @ignore
178 */
179 private $key_type;
180 /**
181 * @ignore
182 */
183 private $value_type;
184 /**
185 * @ignore
186 */
187 private $value_klass;
188
189 /**
190 * Constructs an instance of MapField.
191 *
192 * @param long $key_type Type of the stored key element.
193 * @param long $value_type Type of the stored value element.
194 * @param string $klass Message/Enum class name of value instance
195 * (message/enum fields only).
196 * @ignore
197 */
198 public function __construct($key_type, $value_type, $klass = null)
199 {
200 $this->container = [];
201 $this->key_type = $key_type;
202 $this->value_type = $value_type;
203 $this->klass = $klass;
204 }
205
206 /**
207 * Return the element at the given key.
208 *
209 * This will also be called for: $ele = $arr[$key]
210 *
211 * @param object $key The key of the element to be fetched.
212 * @return object The stored element at given key.
213 * @throws ErrorException Invalid type for index.
214 * @throws ErrorException Non-existing index.
215 */
216 public function offsetGet($key)
217 {
218 return $this->container[$key];
219 }
220
221 /**
222 * Assign the element at the given key.
223 *
224 * This will also be called for: $arr[$key] = $value
225 *
226 * @param object $key The key of the element to be fetched.
227 * @param object $value The element to be assigned.
228 * @return void
229 * @throws ErrorException Invalid type for key.
230 * @throws ErrorException Invalid type for value.
231 * @throws ErrorException Non-existing key.
232 */
233 public function offsetSet($key, $value)
234 {
235 checkKey($this->key_type, $key);
236
237 switch ($this->value_type) {
238 case GPBType::INT32:
239 GPBUtil::checkInt32($value);
240 break;
241 case GPBType::UINT32:
242 GPBUtil::checkUint32($value);
243 break;
244 case GPBType::INT64:
245 GPBUtil::checkInt64($value);
246 break;
247 case GPBType::UINT64:
248 GPBUtil::checkUint64($value);
249 break;
250 case GPBType::FLOAT:
251 GPBUtil::checkFloat($value);
252 break;
253 case GPBType::DOUBLE:
254 GPBUtil::checkDouble($value);
255 break;
256 case GPBType::BOOL:
257 GPBUtil::checkBool($value);
258 break;
259 case GPBType::STRING:
260 GPBUtil::checkString($value, true);
261 break;
262 case GPBType::MESSAGE:
263 GPBUtil::checkMessage($value, $this->klass);
264 break;
265 default:
266 break;
267 }
268
269 $this->container[$key] = $value;
270 }
271
272 /**
273 * Remove the element at the given key.
274 *
275 * This will also be called for: unset($arr)
276 *
277 * @param object $key The key of the element to be removed.
278 * @return void
279 * @throws ErrorException Invalid type for key.
280 */
281 public function offsetUnset($key)
282 {
283 checkKey($this->key_type, $key);
284 unset($this->container[$key]);
285 }
286
287 /**
288 * Check the existence of the element at the given key.
289 *
290 * This will also be called for: isset($arr)
291 *
292 * @param object $key The key of the element to be removed.
293 * @return bool True if the element at the given key exists.
294 * @throws ErrorException Invalid type for key.
295 */
296 public function offsetExists($key)
297 {
298 checkKey($this->key_type, $key);
299 return isset($this->container[$key]);
300 }
301
302 /**
303 * @ignore
304 */
305 public function getIterator()
306 {
307 return new MapFieldIter($this->container);
308 }
309
310 /**
311 * Return the number of stored elements.
312 *
313 * This will also be called for: count($arr)
314 *
315 * @return integer The number of stored elements.
316 */
317 public function count()
318 {
319 return count($this->container);
320 }
321 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698