MapFieldIter.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. /**
  32. * MapField and MapFieldIter are used by generated protocol message classes to
  33. * manipulate map fields.
  34. */
  35. namespace Google\Protobuf\Internal;
  36. /**
  37. * MapFieldIter is used to iterate MapField. It is also need for the foreach
  38. * syntax.
  39. */
  40. class MapFieldIter implements \Iterator
  41. {
  42. /**
  43. * @ignore
  44. */
  45. private $container;
  46. /**
  47. * Create iterator instance for MapField.
  48. *
  49. * @param MapField The MapField instance for which this iterator is
  50. * created.
  51. * @param GPBType Map key type.
  52. * @ignore
  53. */
  54. public function __construct($container, $key_type)
  55. {
  56. $this->container = $container;
  57. $this->key_type = $key_type;
  58. }
  59. /**
  60. * Reset the status of the iterator
  61. *
  62. * @return void
  63. */
  64. public function rewind()
  65. {
  66. return reset($this->container);
  67. }
  68. /**
  69. * Return the element at the current position.
  70. *
  71. * @return object The element at the current position.
  72. */
  73. public function current()
  74. {
  75. return current($this->container);
  76. }
  77. /**
  78. * Return the current key.
  79. *
  80. * @return object The current key.
  81. */
  82. public function key()
  83. {
  84. $key = key($this->container);
  85. if ($this->key_type === GPBType::BOOL) {
  86. // PHP associative array stores bool as integer for key.
  87. return boolval($key);
  88. } elseif ($this->key_type === GPBType::STRING) {
  89. // PHP associative array stores int string as int for key.
  90. return strval($key);
  91. } else {
  92. return $key;
  93. }
  94. }
  95. /**
  96. * Move to the next position.
  97. *
  98. * @return void
  99. */
  100. public function next()
  101. {
  102. return next($this->container);
  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. }