$attribute_value) {
if (is_string($attribute_name)) {
$attribute_name = trim($attribute_name);
if (strlen($attribute_name) === 0) {
continue;
}
}
if (is_object($attribute_value) && is_callable($attribute_value)) {
$attribute_value = $attribute_value();
}
if (is_null($attribute_value)) {
continue;
}
if (is_object($attribute_value)) {
if (is_callable([ $attribute_value, 'toArray' ])) {
$attribute_value = $attribute_value->toArray();
} elseif (is_callable([ $attribute_value, '__toString' ])) {
$attribute_value = strval($attribute_value);
}
}
if (is_bool($attribute_value)) {
if ($attribute_value) {
$html[] = $attribute_name;
}
continue;
} elseif (is_array($attribute_value)) {
$attribute_value = implode(' ', array_reduce($attribute_value, function ($tokens, $token) {
if (is_string($token)) {
$token = trim($token);
if (strlen($token) > 0) {
$tokens[] = $token;
}
} elseif (is_numeric($token)) {
$tokens[] = $token;
}
return $tokens;
}, []));
if (strlen($attribute_value) === 0) {
continue;
}
} elseif (!is_string($attribute_value) && !is_numeric($attribute_value)) {
$attribute_value = json_encode($attribute_value, (JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
}
$html[] = sprintf(
'%1$s="%2$s"',
$attribute_name,
$escape($attribute_value, $attribute_name)
);
}
return implode(' ', $html);
}
}