Skip to content

Commit 2ddb803

Browse files
committed
Improve array value handling
Added: - Condition to ignore attribute if an array is empty after its filtered. - Conditions to accept only numbers and (trimmed) strings. Changed: - Replaced `array_filter()` with `array_reduce()` to preserve only acceptable values. Removed: - Unnecessary usage of custom `is_blank()` function and rely on the fallback filter.
1 parent b945157 commit 2ddb803

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

Function.HTML-Build-Attributes.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,23 @@ function html_build_attributes($attr, callable $callback = null)
5454
}
5555

5656
if (is_array($val)) {
57-
if (function_exists('is_blank')) {
58-
$filter = function ($var) {
59-
return !is_blank($var);
60-
};
61-
} else {
62-
$filter = function ($var) {
63-
return !empty($var) || is_numeric($var);
64-
};
57+
$val = implode(' ', array_reduce($val, function ($tokens, $token) {
58+
if (is_string($token)) {
59+
$token = trim($token);
60+
61+
if (strlen($token) > 0) {
62+
$tokens[] = $token;
63+
}
64+
} elseif (is_numeric($token)) {
65+
$tokens[] = $token;
66+
}
67+
68+
return $tokens;
69+
}, []));
70+
71+
if (strlen($val) === 0) {
72+
continue;
6573
}
66-
$val = implode(' ', array_filter($val, $filter));
6774
}
6875

6976
if (is_callable($callback)) {

0 commit comments

Comments
 (0)