protected function makeKeyCompatible(&$key, &$maxKey, IOrderedMap $incompatibleKeyMap) { if (is_int($key) || is_string($key)) { //Integer strings like "123" get auto cast to integers when set as array keys $intKey = (int) $key; if ((string) $intKey === (string) $key && $intKey >= $maxKey) { $maxKey = $intKey + 1; } } elseif ($incompatibleKeyMap->contains($key)) { $key = $incompatibleKeyMap->get($key); } else { $originalKey = $key; $key = $maxKey++; $incompatibleKeyMap->set($originalKey, $key); } }
/** * @dataProvider orderedMaps */ public function testThatOrderedMapUsesArrayIdentity(IOrderedMap $orderedMap) { $keys = ['ints' => [1, 2, 3], 'reversedInts' => [3, 2, 1], 'intsWithArray' => [1, 2, [3]], 'intsWithString' => [1, '2', 3], 'instance1' => [1, 2, new \stdClass()], 'instance2' => [1, 2, new \stdClass()], 'resource1' => [fopen('php://memory', 'r+'), 2, 3], 'resource2' => [fopen('php://memory', 'r+'), 2, 3]]; foreach ($keys as $name => $key) { $orderedMap->set($key, $name); } foreach ($keys as $name => $key) { $this->assertSame($name, $orderedMap->get($key)); } $array = $this->orderedMapKeyValues(); $identicalArray = $array; $originalInstance = $array['instance']; $nonIdenticalArray = $array; $nonIdenticalArray['instance'] = new \stdClass(); $anotherIdenticalArray = $nonIdenticalArray; $anotherIdenticalArray['instance'] = $originalInstance; $instance = new \stdClass(); $orderedMap->set($array, $instance); $this->assertTrue($orderedMap->contains($identicalArray)); $this->assertSame($instance, $orderedMap->get($identicalArray)); $this->assertFalse($orderedMap->contains($nonIdenticalArray)); $this->assertNull($orderedMap->get($nonIdenticalArray)); $this->assertTrue($orderedMap->contains($anotherIdenticalArray)); $this->assertSame($instance, $orderedMap->get($anotherIdenticalArray)); }