From ba37cba6c2513ecf239946e95dac22bc2e5fe1ad Mon Sep 17 00:00:00 2001 From: maryo Date: Sun, 15 Oct 2017 19:41:03 +0200 Subject: [PATCH] Fixed unsetting from loosely equal keys OrderedHashMap --- .../Form/Tests/Util/OrderedHashMapTest.php | 20 +++++++++++++++++++ .../Component/Form/Util/OrderedHashMap.php | 4 ++-- .../Form/Util/OrderedHashMapIterator.php | 8 +++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Form/Tests/Util/OrderedHashMapTest.php b/src/Symfony/Component/Form/Tests/Util/OrderedHashMapTest.php index 89735ea618..fe922e9239 100644 --- a/src/Symfony/Component/Form/Tests/Util/OrderedHashMapTest.php +++ b/src/Symfony/Component/Form/Tests/Util/OrderedHashMapTest.php @@ -56,6 +56,15 @@ class OrderedHashMapTest extends TestCase $this->assertSame(array(0 => 1, 'foo' => 2, 1 => 3), iterator_to_array($map)); } + public function testInsertLooselyEqualKeys() + { + $map = new OrderedHashMap(); + $map['1 as a string'] = '1 as a string'; + $map[1] = 1; + + $this->assertSame(array('1 as a string' => '1 as a string', 1 => 1), iterator_to_array($map)); + } + /** * Updates should not change the position of an element, otherwise we could * turn foreach loops into endless loops if they change the current @@ -111,6 +120,17 @@ class OrderedHashMapTest extends TestCase $this->assertSame(array('second' => 2), iterator_to_array($map)); } + public function testUnsetFromLooselyEqualKeysHashMap() + { + $map = new OrderedHashMap(); + $map['1 as a string'] = '1 as a string'; + $map[1] = 1; + + unset($map[1]); + + $this->assertSame(array('1 as a string' => '1 as a string'), iterator_to_array($map)); + } + public function testUnsetNonExistingSucceeds() { $map = new OrderedHashMap(); diff --git a/src/Symfony/Component/Form/Util/OrderedHashMap.php b/src/Symfony/Component/Form/Util/OrderedHashMap.php index 78687032fe..24ec0d5299 100644 --- a/src/Symfony/Component/Form/Util/OrderedHashMap.php +++ b/src/Symfony/Component/Form/Util/OrderedHashMap.php @@ -133,7 +133,7 @@ class OrderedHashMap implements \ArrayAccess, \IteratorAggregate, \Countable : 1 + (int) max($this->orderedKeys); } - $this->orderedKeys[] = $key; + $this->orderedKeys[] = (string) $key; } $this->elements[$key] = $value; @@ -144,7 +144,7 @@ class OrderedHashMap implements \ArrayAccess, \IteratorAggregate, \Countable */ public function offsetUnset($key) { - if (false !== ($position = array_search($key, $this->orderedKeys))) { + if (false !== ($position = array_search((string) $key, $this->orderedKeys))) { array_splice($this->orderedKeys, $position, 1); unset($this->elements[$key]); diff --git a/src/Symfony/Component/Form/Util/OrderedHashMapIterator.php b/src/Symfony/Component/Form/Util/OrderedHashMapIterator.php index 7305f51640..11562475d8 100644 --- a/src/Symfony/Component/Form/Util/OrderedHashMapIterator.php +++ b/src/Symfony/Component/Form/Util/OrderedHashMapIterator.php @@ -118,7 +118,13 @@ class OrderedHashMapIterator implements \Iterator */ public function key() { - return $this->key; + if (null === $this->key) { + return null; + } + + $array = array($this->key => null); + + return key($array); } /**