Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Twig] Make ComponentAttributes traversable/countable #1413

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}