123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- namespace Symfony\Component\Form\Util;
- class OrderedHashMap implements \ArrayAccess, \IteratorAggregate, \Countable
- {
-
- private $elements = array();
-
- private $orderedKeys = array();
-
- private $managedCursors = array();
-
- public function __construct(array $elements = array())
- {
- $this->elements = $elements;
- $this->orderedKeys = array_keys($elements);
- }
-
- public function offsetExists($key)
- {
- return isset($this->elements[$key]);
- }
-
- public function offsetGet($key)
- {
- if (!isset($this->elements[$key])) {
- throw new \OutOfBoundsException('The offset "'.$key.'" does not exist.');
- }
- return $this->elements[$key];
- }
-
- public function offsetSet($key, $value)
- {
- if (null === $key || !isset($this->elements[$key])) {
- if (null === $key) {
- $key = array() === $this->orderedKeys
-
- ? 0
-
-
- : 1 + (int) max($this->orderedKeys);
- }
- $this->orderedKeys[] = (string) $key;
- }
- $this->elements[$key] = $value;
- }
-
- public function offsetUnset($key)
- {
- if (false !== ($position = array_search((string) $key, $this->orderedKeys))) {
- array_splice($this->orderedKeys, $position, 1);
- unset($this->elements[$key]);
- foreach ($this->managedCursors as $i => $cursor) {
- if ($cursor >= $position) {
- --$this->managedCursors[$i];
- }
- }
- }
- }
-
- public function getIterator()
- {
- return new OrderedHashMapIterator($this->elements, $this->orderedKeys, $this->managedCursors);
- }
-
- public function count()
- {
- return \count($this->elements);
- }
- }
|