Skip to content

Commit 3f7f811

Browse files
committed
feature #1413 [Twig] Make ComponentAttributes traversable/countable (kbond)
This PR was merged into the 2.x branch. Discussion ---------- [Twig] Make `ComponentAttributes` traversable/countable | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Issues | n/a | License | MIT This will help with #1405 and #1414. Commits ------- 8040d9c feat(twig): make `ComponentAttributes` traversable/countable
2 parents 6b2a80f + 8040d9c commit 3f7f811

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/TwigComponent/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## Unreleased
4+
5+
- Make `ComponentAttributes` traversable/countable.
6+
37
## 2.13.0
48

59
- [BC BREAK] Add component metadata to `PreMountEvent` and `PostMountEvent`

src/TwigComponent/src/ComponentAttributes.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @immutable
2121
*/
22-
final class ComponentAttributes
22+
final class ComponentAttributes implements \IteratorAggregate, \Countable
2323
{
2424
/**
2525
* @param array<string, string|bool> $attributes
@@ -157,4 +157,14 @@ public function remove($key): self
157157

158158
return new self($attributes);
159159
}
160+
161+
public function getIterator(): \Traversable
162+
{
163+
return new \ArrayIterator($this->attributes);
164+
}
165+
166+
public function count(): int
167+
{
168+
return \count($this->attributes);
169+
}
160170
}

src/TwigComponent/tests/Unit/ComponentAttributesTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,12 @@ public function testNullBehaviour(): void
191191
$this->assertSame(['disabled' => null], $attributes->all());
192192
$this->assertSame(' disabled', (string) $attributes);
193193
}
194+
195+
public function testIsTraversableAndCountable(): void
196+
{
197+
$attributes = new ComponentAttributes(['foo' => 'bar']);
198+
199+
$this->assertSame($attributes->all(), iterator_to_array($attributes));
200+
$this->assertCount(1, $attributes);
201+
}
194202
}

0 commit comments

Comments
 (0)