Skip to content
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

[BUGFIX] Prevent php warning when passing array as tag attribute #858

Merged
merged 1 commit into from
Mar 11, 2024
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
16 changes: 12 additions & 4 deletions src/Core/ViewHelper/TagBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ public function ignoreEmptyAttributes($ignoreEmptyAttributes)
* Adds an attribute to the $attributes-collection
*
* @param string $attributeName name of the attribute to be added to the tag
* @param string|\Traversable|array|null $attributeValue attribute value
* @param string|\Traversable|array|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
* @param bool $escapeSpecialCharacters apply htmlspecialchars to attribute value
* @api
*/
Expand All @@ -190,9 +193,14 @@ public function addAttribute($attributeName, $attributeValue, $escapeSpecialChar
if ($escapeSpecialCharacters) {
$attributeName = htmlspecialchars($attributeName);
}
if (in_array($attributeName, ['data', 'aria'], true)
&& (is_array($attributeValue) || $attributeValue instanceof \Traversable)
) {
if (is_array($attributeValue) || $attributeValue instanceof \Traversable) {
if (!in_array($attributeName, ['data', 'aria'], true)) {
throw new \InvalidArgumentException(
sprintf('Value of tag attribute "%s" cannot be of type array.', $attributeName),
1709565127
);
}

foreach ($attributeValue as $name => $value) {
$this->addAttribute($attributeName . '-' . $name, $value, $escapeSpecialCharacters);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/Core/ViewHelper/TagBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,29 @@ public function attributesAreProperlyRendered(): void
self::assertEquals('<tag attribute1="attribute1value" attribute2="attribute2value" attribute3="attribute3value" />', $tagBuilder->render());
}

/**
* @test
*/
public function arrayAttributesAreProperlyRendered(): void
{
$tagBuilder = new TagBuilder('tag');
$tagBuilder->addAttribute('data', ['attribute1' => 'data1', 'attribute2' => 'data2']);
$tagBuilder->addAttribute('aria', ['attribute1' => 'aria1']);
self::assertEquals('<tag data-attribute1="data1" data-attribute2="data2" aria-attribute1="aria1" />', $tagBuilder->render());
}

/**
* @test
*/
public function customArrayAttributesThrowException(): void
{
self::expectException(\InvalidArgumentException::class);
self::expectExceptionCode(1709565127);
self::expectExceptionMessage('Value of tag attribute "custom" cannot be of type array.');
$tagBuilder = new TagBuilder('tag');
$tagBuilder->addAttribute('custom', ['attribute1' => 'data1', 'attribute2' => 'data2']);
}

/**
* @test
*/
Expand Down
Loading