Skip to content

Commit 1a68b93

Browse files
committed
Add support for attributes as an object
Fixed: - Lack of support for objects as affirmed in the README.
1 parent da9fcf4 commit 1a68b93

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Function.HTML-Build-Attributes.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
/**
55
* Generate a string of HTML attributes
66
*
7-
* @param array $attr Associative array of attribute names and values.
7+
* @param array|object $attr Associative array or object of attribute names and values.
88
* @param callable|null $callback Callback function to escape values for HTML attributes.
99
* Defaults to `htmlspecialchars()`.
10-
* @return string Returns a string of HTML attributes.
10+
* @return string Returns a string of HTML attributes
11+
* or a empty string if $attr is invalid or empty.
1112
*/
12-
function html_build_attributes(array $attr, callable $callback = null)
13+
function html_build_attributes($attr, callable $callback = null)
1314
{
14-
if (!count($attr)) {
15+
if (is_object($attr) && !($attr instanceof \Traversable)) {
16+
$attr = get_object_vars($attr);
17+
}
18+
19+
if (!is_array($attr) || !count($attr)) {
1520
return '';
1621
}
1722

0 commit comments

Comments
 (0)