Skip to content

Commit 1654fae

Browse files
bug #744 [Twig] Fixing a bug where a stringable attribute was converted too late (weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- [Twig] Fixing a bug where a stringable attribute was converted too late | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | Fix #741 | License | MIT This caused LiveComponents to try to use the Stringable object in its checksum and to json_encode() the object into the props data, instead of using the final, string value. Basically, we cast to the string earlier. Commits ------- 276d6923 [Twig] Fixing a bug where a strinagable attribute was converted too late
2 parents cc85e05 + aa665fd commit 1654fae

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/TwigComponent/src/ComponentFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
8686
// ensure remaining data is scalar
8787
foreach ($data as $key => $value) {
8888
if ($value instanceof \Stringable) {
89+
$data[$key] = (string) $value;
8990
continue;
9091
}
9192

src/TwigComponent/tests/Integration/ComponentFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ public function testExceptionThrownIfUnableToWritePassedDataToPropertyAndIsNotSc
100100

101101
public function testStringableObjectCanBePassedToComponent(): void
102102
{
103-
$attributes = (string) $this->factory()->create('component_a', ['propB' => 'B', 'data-item-id-param' => new class() {
103+
$attributes = $this->factory()->create('component_a', ['propB' => 'B', 'data-item-id-param' => new class() {
104104
public function __toString(): string
105105
{
106106
return 'test';
107107
}
108-
}])->getAttributes();
108+
}])->getAttributes()->all();
109109

110-
self::assertSame(' data-item-id-param="test"', $attributes);
110+
self::assertSame(['data-item-id-param' => 'test'], $attributes);
111111
}
112112

113113
public function testTwigComponentServiceTagMustHaveKey(): void

0 commit comments

Comments
 (0)