Skip to content

Commit 111d86b

Browse files
committed
Improve callable value handling
Changed: - Check if value is callable instead of just a `Closure` to support invokable objects. - Moved callable value to earlier in case its value is NULL or a special object.
1 parent 6f2cc4c commit 111d86b

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Function.HTML-Build-Attributes.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ function html_build_attributes($attr, callable $callback = null)
2222

2323
$html = [];
2424
foreach ($attr as $key => $val) {
25-
if (is_null($val)) {
26-
continue;
27-
}
28-
2925
if (is_string($key)) {
3026
$key = trim($key);
3127

@@ -34,10 +30,16 @@ function html_build_attributes($attr, callable $callback = null)
3430
}
3531
}
3632

33+
if (is_callable($val)) {
34+
$val = $val();
35+
}
36+
37+
if (is_null($val)) {
38+
continue;
39+
}
40+
3741
if (is_object($val)) {
38-
if ($val instanceof Closure) {
39-
$val = $val();
40-
} elseif (is_callable([ $val, 'toArray' ])) {
42+
if (is_callable([ $val, 'toArray' ])) {
4143
$val = $val->toArray();
4244
} elseif (is_callable([ $val, '__toString' ])) {
4345
$val = strval($val);

0 commit comments

Comments
 (0)