Skip to content

Commit

Permalink
feature #1413 [Twig] Make ComponentAttributes traversable/countable…
Browse files Browse the repository at this point in the history
… (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
  • Loading branch information
weaverryan committed Jan 29, 2024
2 parents 6b2a80f + 8040d9c commit 3f7f811
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 3f7f811

Please sign in to comment.