OrderedHashMapIterator.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form\Util;
  11. /**
  12. * Iterator for {@link OrderedHashMap} objects.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. *
  16. * @internal
  17. */
  18. class OrderedHashMapIterator implements \Iterator
  19. {
  20. /**
  21. * @var array
  22. */
  23. private $elements;
  24. /**
  25. * @var array
  26. */
  27. private $orderedKeys;
  28. /**
  29. * @var int
  30. */
  31. private $cursor;
  32. /**
  33. * @var int
  34. */
  35. private $cursorId;
  36. /**
  37. * @var array
  38. */
  39. private $managedCursors;
  40. /**
  41. * @var string|int|null
  42. */
  43. private $key;
  44. /**
  45. * @var mixed
  46. */
  47. private $current;
  48. /**
  49. * @param array $elements The elements of the map, indexed by their
  50. * keys
  51. * @param array $orderedKeys The keys of the map in the order in which
  52. * they should be iterated
  53. * @param array $managedCursors An array from which to reference the
  54. * iterator's cursor as long as it is alive.
  55. * This array is managed by the corresponding
  56. * {@link OrderedHashMap} instance to support
  57. * recognizing the deletion of elements.
  58. */
  59. public function __construct(array &$elements, array &$orderedKeys, array &$managedCursors)
  60. {
  61. $this->elements = &$elements;
  62. $this->orderedKeys = &$orderedKeys;
  63. $this->managedCursors = &$managedCursors;
  64. $this->cursorId = \count($managedCursors);
  65. $this->managedCursors[$this->cursorId] = &$this->cursor;
  66. }
  67. /**
  68. * Removes the iterator's cursors from the managed cursors of the
  69. * corresponding {@link OrderedHashMap} instance.
  70. */
  71. public function __destruct()
  72. {
  73. // Use array_splice() instead of unset() to prevent holes in the
  74. // array indices, which would break the initialization of $cursorId
  75. array_splice($this->managedCursors, $this->cursorId, 1);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function current()
  81. {
  82. return $this->current;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function next()
  88. {
  89. ++$this->cursor;
  90. if (isset($this->orderedKeys[$this->cursor])) {
  91. $this->key = $this->orderedKeys[$this->cursor];
  92. $this->current = $this->elements[$this->key];
  93. } else {
  94. $this->key = null;
  95. $this->current = null;
  96. }
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function key()
  102. {
  103. if (null === $this->key) {
  104. return null;
  105. }
  106. $array = array($this->key => null);
  107. return key($array);
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function valid()
  113. {
  114. return null !== $this->key;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function rewind()
  120. {
  121. $this->cursor = 0;
  122. if (isset($this->orderedKeys[0])) {
  123. $this->key = $this->orderedKeys[0];
  124. $this->current = $this->elements[$this->key];
  125. } else {
  126. $this->key = null;
  127. $this->current = null;
  128. }
  129. }
  130. }