Skip to content

[TwigComponent] Allow to pass stringable object as non mapped component attribute #319

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

Merged
merged 1 commit into from
May 6, 2022
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

## 2.2

- Allow to pass stringable object as non mapped component attribute

## 2.1

- Make public component properties available directly in the template (`{{ prop }}` vs `{{ this.prop }}`).
Expand Down
4 changes: 4 additions & 0 deletions src/TwigComponent/src/ComponentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public function create(string $name, array $data = []): MountedComponent

// ensure remaining data is scalar
foreach ($data as $key => $value) {
if ($value instanceof \Stringable) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny timing - we JUST did this with stimulus_controller() also. There, we went further and checked for __toString(): https://github.com/symfony/webpack-encore-bundle/pull/171/files#diff-aac7ca1aa215ab99c76587c963238c082fb42b7a4315344002b7df3046306b8fR64

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe we need to check for the method because this lib is PHP 8+.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

continue;
}

if (!\is_scalar($value) && null !== $value) {
throw new \LogicException(sprintf('Unable to use "%s" (%s) as an attribute. Attributes must be scalar or null. If you meant to mount this value on "%s", make sure "$%1$s" is a writable property or create a mount() method with a "$%1$s" argument.', $key, get_debug_type($value), $component::class));
}
Expand Down
12 changes: 12 additions & 0 deletions src/TwigComponent/tests/Integration/ComponentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ public function testExceptionThrownIfUnableToWritePassedDataToPropertyAndIsNotSc
$this->createComponent('component_a', ['propB' => 'B', 'service' => new \stdClass()]);
}

public function testStringableObjectCanBePassedToComponent(): void
{
$attributes = (string) $this->factory()->create('component_a', ['propB' => 'B', 'data-item-id-param' => new class() {
public function __toString(): string
{
return 'test';
}
}])->getAttributes();

self::assertSame(' data-item-id-param="test"', $attributes);
}

public function testTwigComponentServiceTagMustHaveKey(): void
{
$this->expectException(LogicException::class);
Expand Down