Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass any attribute value type to markup #31

Merged
merged 3 commits into from
May 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions src/AddAttributesTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,35 @@ public function add_attributes($context, $additional_attributes = []) {

if (!empty($additional_attributes)) {
foreach ($additional_attributes as $key => $value) {
if (is_array($value)) {
foreach ($value as $index => $item) {

switch (gettype($value)) {
case 'array':
foreach ($value as $index => $item) {
// Handle bem() output.
if ($item instanceof Attribute) {
// Remove the item.
unset($value[$index]);
$value = array_merge($value, $item->toArray()[$key]);
}
}
break;

case 'integer':
case 'boolean':
case 'string':
// Handle bem() output.
if ($item instanceof Attribute) {
// Remove the item.
unset($value[$index]);
$value = array_merge($value, $item->toArray()[$key]);
if ($value instanceof Attribute) {
$value = $value->toArray()[$key];
}
}
}
else {
// Handle bem() output.
if ($value instanceof Attribute) {
$value = $value->toArray()[$key];
}
elseif (is_string($value)) {
$value = [$value];
}
else {
continue;
}
else {
$value = [strval($value)];
}
break;

default:
// Set value to an empty string.
$value = '';
break;
}
// Merge additional attribute values with existing ones.
if ($context['attributes']->offsetExists($key)) {
Expand Down