Skip to content

Commit 8040d9c

Browse files
committed
feat(twig): make ComponentAttributes traversable/countable
1 parent b73eac8 commit 8040d9c

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)