From 747236f02733be68adcc5291feaee18befa5c3cd Mon Sep 17 00:00:00 2001 From: Oliver Bartsch Date: Wed, 25 Sep 2024 14:44:41 +0200 Subject: [PATCH] [BUGFIX] Handle PHP Enum as tag attribute value (#1007) It is now possible to provide PHP enums as value for additional tag attributes in tag-based ViewHelpers. --- src/Core/ViewHelper/TagBuilder.php | 8 +++++++- .../Core/ViewHelper/TagBasedViewHelperTest.php | 11 +++++++++++ .../Variables/Fixtures/UserRoleBackedEnum.php | 17 +++++++++++++++++ .../Core/Variables/Fixtures/UserRoleEnum.php | 17 +++++++++++++++++ tests/Unit/Core/ViewHelper/TagBuilderTest.php | 11 +++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Core/Variables/Fixtures/UserRoleBackedEnum.php create mode 100644 tests/Unit/Core/Variables/Fixtures/UserRoleEnum.php diff --git a/src/Core/ViewHelper/TagBuilder.php b/src/Core/ViewHelper/TagBuilder.php index 8b3c60553..41c8c24e1 100644 --- a/src/Core/ViewHelper/TagBuilder.php +++ b/src/Core/ViewHelper/TagBuilder.php @@ -173,7 +173,7 @@ public function ignoreEmptyAttributes(bool $ignoreEmptyAttributes): void * * @param string $attributeName name of the attribute to be added to the tag. Be extremely * careful if this value is user-provided input! - * @param string|bool|\Traversable|array|null $attributeValue attribute value, can only be array or traversable + * @param string|bool|\Traversable|array|\UnitEnum|\BackedEnum|null $attributeValue attribute value, can only be array or traversable * if the attribute name is either "data" or "area". In * that special case, multiple attributes will be created * with either "data-" or "area-" as prefix @@ -223,6 +223,12 @@ public function addAttribute(string $attributeName, $attributeValue, bool $escap $attributeValue = $attributeName; } + if ($attributeValue instanceof \BackedEnum) { + $attributeValue = (string)$attributeValue->value; + } elseif ($attributeValue instanceof \UnitEnum) { + $attributeValue = $attributeValue->name; + } + if (trim((string)$attributeValue) === '' && $this->ignoreEmptyAttributes) { return; } diff --git a/tests/Functional/Core/ViewHelper/TagBasedViewHelperTest.php b/tests/Functional/Core/ViewHelper/TagBasedViewHelperTest.php index 1fa8f46e3..788d17ec6 100644 --- a/tests/Functional/Core/ViewHelper/TagBasedViewHelperTest.php +++ b/tests/Functional/Core/ViewHelper/TagBasedViewHelperTest.php @@ -12,6 +12,8 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use TYPO3Fluid\Fluid\Tests\Functional\AbstractFunctionalTestCase; +use TYPO3Fluid\Fluid\Tests\Unit\Core\Variables\Fixtures\UserRoleBackedEnum; +use TYPO3Fluid\Fluid\Tests\Unit\Core\Variables\Fixtures\UserRoleEnum; use TYPO3Fluid\Fluid\View\TemplateView; final class TagBasedViewHelperTest extends AbstractFunctionalTestCase @@ -257,6 +259,15 @@ public static function renderTagBasedViewHelperDataProvider(): array [], '
', ], + 'enum attribute value' => [ + '', + '{test:tagBasedTest(userRole: userRole, userRoleBacked: userRoleBacked)}', + [ + 'userRole' => UserRoleEnum::GUEST, + 'userRoleBacked' => UserRoleBackedEnum::EDITOR, + ], + '
', + ], ]; } diff --git a/tests/Unit/Core/Variables/Fixtures/UserRoleBackedEnum.php b/tests/Unit/Core/Variables/Fixtures/UserRoleBackedEnum.php new file mode 100644 index 000000000..0f5a621ce --- /dev/null +++ b/tests/Unit/Core/Variables/Fixtures/UserRoleBackedEnum.php @@ -0,0 +1,17 @@ +" />', $tagBuilder->render()); } + #[Test] + public function attributesSupportEnum(): void + { + $tagBuilder = new TagBuilder('tag'); + $tagBuilder->addAttribute('attribute1', UserRoleEnum::GUEST); + $tagBuilder->addAttribute('attribute2', UserRoleBackedEnum::GUEST); + self::assertEquals('', $tagBuilder->render()); + } + #[Test] public function attributesCanBeRemoved(): void {