Skip to content

Commit

Permalink
[BUGFIX] Handle PHP Enum as tag attribute value (#1007)
Browse files Browse the repository at this point in the history
It is now possible to provide PHP enums as value for additional tag attributes in tag-based ViewHelpers.
  • Loading branch information
o-ba authored Sep 25, 2024
1 parent a5d7bd3 commit 747236f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Core/ViewHelper/TagBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Functional/Core/ViewHelper/TagBasedViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -257,6 +259,15 @@ public static function renderTagBasedViewHelperDataProvider(): array
[],
'<div data-foo="attribute" />',
],
'enum attribute value' => [
'<test:tagBasedTest userRole="{userRole}" userRoleBacked="{userRoleBacked}" />',
'{test:tagBasedTest(userRole: userRole, userRoleBacked: userRoleBacked)}',
[
'userRole' => UserRoleEnum::GUEST,
'userRoleBacked' => UserRoleBackedEnum::EDITOR,
],
'<div userRole="GUEST" userRoleBacked="2" />',
],
];
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Core/Variables/Fixtures/UserRoleBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/

namespace TYPO3Fluid\Fluid\Tests\Unit\Core\Variables\Fixtures;

enum UserRoleBackedEnum: int
{
case ADMIN = 1;
case EDITOR = 2;
case GUEST = 3;
}
17 changes: 17 additions & 0 deletions tests/Unit/Core/Variables/Fixtures/UserRoleEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/

namespace TYPO3Fluid\Fluid\Tests\Unit\Core\Variables\Fixtures;

enum UserRoleEnum
{
case ADMIN;
case EDITOR;
case GUEST;
}
11 changes: 11 additions & 0 deletions tests/Unit/Core/ViewHelper/TagBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
use TYPO3Fluid\Fluid\Tests\Unit\Core\Variables\Fixtures\UserRoleBackedEnum;
use TYPO3Fluid\Fluid\Tests\Unit\Core\Variables\Fixtures\UserRoleEnum;

final class TagBuilderTest extends TestCase
{
Expand Down Expand Up @@ -160,6 +162,15 @@ public function attributeValuesAreNotEscapedIfDisabled(): void
self::assertEquals('<tag foo="<not to be escaped>" />', $tagBuilder->render());
}

#[Test]
public function attributesSupportEnum(): void
{
$tagBuilder = new TagBuilder('tag');
$tagBuilder->addAttribute('attribute1', UserRoleEnum::GUEST);
$tagBuilder->addAttribute('attribute2', UserRoleBackedEnum::GUEST);
self::assertEquals('<tag attribute1="GUEST" attribute2="3" />', $tagBuilder->render());
}

#[Test]
public function attributesCanBeRemoved(): void
{
Expand Down

0 comments on commit 747236f

Please sign in to comment.