Skip to content

Commit 6f2cc4c

Browse files
committed
Improve serializable value handling
Changed: - Moved JSON serialization to later to encode non-alphanumeric values.
1 parent 2ddb803 commit 6f2cc4c

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

Function.HTML-Build-Attributes.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ function html_build_attributes($attr, callable $callback = null)
3737
if (is_object($val)) {
3838
if ($val instanceof Closure) {
3939
$val = $val();
40-
} elseif ($val instanceof JsonSerializable) {
41-
$val = json_encode($val, (JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
4240
} elseif (is_callable([ $val, 'toArray' ])) {
4341
$val = $val->toArray();
4442
} elseif (is_callable([ $val, '__toString' ])) {
@@ -51,9 +49,7 @@ function html_build_attributes($attr, callable $callback = null)
5149
$html[] = $key;
5250
}
5351
continue;
54-
}
55-
56-
if (is_array($val)) {
52+
} elseif (is_array($val)) {
5753
$val = implode(' ', array_reduce($val, function ($tokens, $token) {
5854
if (is_string($token)) {
5955
$token = trim($token);
@@ -71,6 +67,8 @@ function html_build_attributes($attr, callable $callback = null)
7167
if (strlen($val) === 0) {
7268
continue;
7369
}
70+
} elseif (!is_string($val) && !is_numeric($val)) {
71+
$val = json_encode($val, (JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
7472
}
7573

7674
if (is_callable($callback)) {
@@ -81,10 +79,7 @@ function html_build_attributes($attr, callable $callback = null)
8179
$val = htmlspecialchars($val, ENT_QUOTES);
8280
}
8381

84-
if (is_string($val) || is_numeric($val)) {
85-
$html[] = sprintf('%1$s="%2$s"', $key, $val);
86-
continue;
87-
}
82+
$html[] = sprintf('%1$s="%2$s"', $key, $val);
8883
}
8984

9085
return implode(' ', $html);

0 commit comments

Comments
 (0)