vendor/symfony/form/Util/OrderedHashMapIterator.php line 101

Open in your IDE?
  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->cursorId1);
  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 = [$this->key => null];
  107.         return key($array);
  108.     }
  109.     /**
  110.      * {@inheritdoc}
  111.      */
  112.     public function valid(): bool
  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. }