Skip to content

Commit

Permalink
Add support for Stringable
Browse files Browse the repository at this point in the history
  • Loading branch information
nlemoine committed Dec 18, 2024
1 parent a4a11ca commit 511d823
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
12 changes: 6 additions & 6 deletions extra/html-extra/HtmlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ public static function htmlClasses(...$args): string
{
$classes = [];
foreach ($args as $i => $arg) {
if (\is_string($arg)) {
$classes[] = $arg;
if (\is_string($arg) || $arg instanceof \Stringable) {
$classes[] = (string) $arg;
} elseif (\is_array($arg)) {
foreach ($arg as $class => $condition) {
if (!\is_string($class)) {
throw new RuntimeError(\sprintf('The "html_classes" function argument %d (key %d) should be a string, got "%s".', $i, $class, get_debug_type($class)));
if (!\is_string($class) && !$class instanceof \Stringable) {
throw new RuntimeError(\sprintf('The "html_classes" function argument %d (key %d) should be a string or an instance of Stringable, got "%s".', $i, $class, get_debug_type($class)));
}
if (!$condition) {
continue;
}
$classes[] = $class;
$classes[] = (string) $class;
}
} else {
throw new RuntimeError(\sprintf('The "html_classes" function argument %d should be either a string or an array, got "%s".', $i, get_debug_type($arg)));
throw new RuntimeError(\sprintf('The "html_classes" function argument %d should be either a string, an instance of Stringable or an array, got "%s".', $i, get_debug_type($arg)));
}
}

Expand Down
14 changes: 13 additions & 1 deletion extra/html-extra/Tests/Fixtures/html_classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@
{{ html_classes('a', {'b': true, 'c': false}, 'd', false ? 'e', true ? 'f', '0') }}
{% set class_a = 'a' %}
{% set class_b = 'b' %}
{%- set class_d -%}
d
{%- endset -%}
{{ html_classes(class_a, {(class_b): true})}}
{{ html_classes(class_a, class_c, {(class_d): true})}}
--DATA--
return []
return [
'class_c' => new class implements Stringable {
public function __toString(): string
{
return 'c';
}
},
]
--EXPECT--
a b d f 0
a b
a c d
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: The "html_classes" function argument 0 should be either a string or an array, got "bool" in "index.twig" at line 2.
Twig\Error\RuntimeError: The "html_classes" function argument 0 should be either a string, an instance of Stringable or an array, got "bool" in "index.twig" at line 2.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: The "html_classes" function argument 0 (key 0) should be a string, got "int" in "index.twig" at line 2.
Twig\Error\RuntimeError: The "html_classes" function argument 0 (key 0) should be a string or an instance of Stringable, got "int" in "index.twig" at line 2.

0 comments on commit 511d823

Please sign in to comment.