Skip to content

Commit

Permalink
feat(twig): make ComponentAttributes traversable/countable
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Jan 22, 2024
1 parent b73eac8 commit 8040d9c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/TwigComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Unreleased

- Make `ComponentAttributes` traversable/countable.

## 2.13.0

- [BC BREAK] Add component metadata to `PreMountEvent` and `PostMountEvent`
Expand Down
12 changes: 11 additions & 1 deletion src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @immutable
*/
final class ComponentAttributes
final class ComponentAttributes implements \IteratorAggregate, \Countable
{
/**
* @param array<string, string|bool> $attributes
Expand Down Expand Up @@ -157,4 +157,14 @@ public function remove($key): self

return new self($attributes);
}

public function getIterator(): \Traversable
{
return new \ArrayIterator($this->attributes);
}

public function count(): int
{
return \count($this->attributes);
}
}
8 changes: 8 additions & 0 deletions src/TwigComponent/tests/Unit/ComponentAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,12 @@ public function testNullBehaviour(): void
$this->assertSame(['disabled' => null], $attributes->all());
$this->assertSame(' disabled', (string) $attributes);
}

public function testIsTraversableAndCountable(): void
{
$attributes = new ComponentAttributes(['foo' => 'bar']);

$this->assertSame($attributes->all(), iterator_to_array($attributes));
$this->assertCount(1, $attributes);
}
}

0 comments on commit 8040d9c

Please sign in to comment.