Skip to content

Commit c9bdced

Browse files
VincentLangletfabpot
authored andcommitted
[Form] Provide string keys when iterating on a form
1 parent 8c120f3 commit c9bdced

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
lines changed

Form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterfac
7777
/**
7878
* A map of FormInterface instances.
7979
*
80-
* @var OrderedHashMap<string, FormInterface>
80+
* @var OrderedHashMap<FormInterface>
8181
*/
8282
private OrderedHashMap $children;
8383

Tests/CompoundFormTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,16 @@ public function testIterator()
333333
$this->assertSame($this->form->all(), iterator_to_array($this->form));
334334
}
335335

336+
public function testIteratorKeys()
337+
{
338+
$this->form->add($this->getBuilder('0')->getForm());
339+
$this->form->add($this->getBuilder('1')->getForm());
340+
341+
foreach ($this->form as $key => $value) {
342+
$this->assertIsString($key);
343+
}
344+
}
345+
336346
public function testAddMapsViewDataToFormIfInitialized()
337347
{
338348
$form = $this->getBuilder()

Tests/Util/OrderedHashMapTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testInsertNullKeys()
5151
$map['foo'] = 2;
5252
$map[] = 3;
5353

54-
$this->assertSame([0 => 1, 'foo' => 2, 1 => 3], iterator_to_array($map));
54+
$this->assertSame(['0' => 1, 'foo' => 2, '1' => 3], iterator_to_array($map));
5555
}
5656

5757
public function testInsertLooselyEqualKeys()
@@ -496,12 +496,30 @@ public function testParallelIteration()
496496
$this->assertNull($it1->current());
497497
}
498498

499+
public function testKeysAreString()
500+
{
501+
$map = new OrderedHashMap(['1' => 1]);
502+
$map['2'] = 2;
503+
504+
$it = $map->getIterator();
505+
506+
$it->rewind();
507+
$this->assertTrue($it->valid());
508+
$this->assertSame('1', $it->key());
509+
$this->assertSame(1, $it->current());
510+
511+
$it->next();
512+
$this->assertTrue($it->valid());
513+
$this->assertSame('2', $it->key());
514+
$this->assertSame(2, $it->current());
515+
}
516+
499517
public function testCount()
500518
{
501519
$map = new OrderedHashMap();
502520
$map[] = 1;
503521
$map['foo'] = 2;
504-
unset($map[0]);
522+
unset($map['0']);
505523
$map[] = 3;
506524

507525
$this->assertCount(2, $map);

Util/OrderedHashMap.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,24 @@
6464
*
6565
* @author Bernhard Schussek <[email protected]>
6666
*
67-
* @template TKey of array-key
6867
* @template TValue
6968
*
70-
* @implements \ArrayAccess<TKey, TValue>
71-
* @implements \IteratorAggregate<TKey, TValue>
69+
* @implements \ArrayAccess<string, TValue>
70+
* @implements \IteratorAggregate<string, TValue>
7271
*/
7372
class OrderedHashMap implements \ArrayAccess, \IteratorAggregate, \Countable
7473
{
7574
/**
7675
* The elements of the map, indexed by their keys.
7776
*
78-
* @var array<TKey, TValue>
77+
* @var TValue[]
7978
*/
8079
private array $elements = [];
8180

8281
/**
8382
* The keys of the map in the order in which they were inserted or changed.
8483
*
85-
* @var list<TKey>
84+
* @var list<string>
8685
*/
8786
private array $orderedKeys = [];
8887

@@ -96,12 +95,13 @@ class OrderedHashMap implements \ArrayAccess, \IteratorAggregate, \Countable
9695
/**
9796
* Creates a new map.
9897
*
99-
* @param array<TKey, TValue> $elements The elements to insert initially
98+
* @param TValue[] $elements The elements to insert initially
10099
*/
101100
public function __construct(array $elements = [])
102101
{
103102
$this->elements = $elements;
104-
$this->orderedKeys = array_keys($elements);
103+
// the explicit string type-cast is necessary as digit-only keys would be returned as integers otherwise
104+
$this->orderedKeys = array_map(strval(...), array_keys($elements));
105105
}
106106

107107
public function offsetExists(mixed $key): bool

Util/OrderedHashMapIterator.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,34 @@
1818
*
1919
* @internal
2020
*
21-
* @template-covariant TKey of array-key
2221
* @template-covariant TValue
2322
*
24-
* @implements \Iterator<TKey, TValue>
23+
* @implements \Iterator<string, TValue>
2524
*/
2625
class OrderedHashMapIterator implements \Iterator
2726
{
28-
/** @var array<TKey, TValue> */
27+
/** @var TValue[] */
2928
private array $elements;
30-
/** @var list<TKey> */
29+
/** @var list<string> */
3130
private array $orderedKeys;
3231
private int $cursor = 0;
3332
private int $cursorId;
3433
/** @var array<int, int> */
3534
private array $managedCursors;
36-
/** @var TKey|null */
37-
private string|int|null $key = null;
35+
private string|null $key = null;
3836
/** @var TValue|null */
3937
private mixed $current = null;
4038

4139
/**
42-
* @param array<TKey, TValue> $elements The elements of the map, indexed by their
43-
* keys
44-
* @param list<TKey> $orderedKeys The keys of the map in the order in which
45-
* they should be iterated
46-
* @param array<int, int> $managedCursors An array from which to reference the
47-
* iterator's cursor as long as it is alive.
48-
* This array is managed by the corresponding
49-
* {@link OrderedHashMap} instance to support
50-
* recognizing the deletion of elements.
40+
* @param TValue[] $elements The elements of the map, indexed by their
41+
* keys
42+
* @param list<string> $orderedKeys The keys of the map in the order in which
43+
* they should be iterated
44+
* @param array<int, int> $managedCursors An array from which to reference the
45+
* iterator's cursor as long as it is alive.
46+
* This array is managed by the corresponding
47+
* {@link OrderedHashMap} instance to support
48+
* recognizing the deletion of elements.
5149
*/
5250
public function __construct(array &$elements, array &$orderedKeys, array &$managedCursors)
5351
{
@@ -113,9 +111,7 @@ public function key(): mixed
113111
return null;
114112
}
115113

116-
$array = [$this->key => null];
117-
118-
return key($array);
114+
return $this->key;
119115
}
120116

121117
/**

0 commit comments

Comments
 (0)