Skip to content

Commit

Permalink
Merge pull request #31 from emulsify-ds/fix-handling-integers
Browse files Browse the repository at this point in the history
  • Loading branch information
ModulesUnraveled authored May 19, 2022
2 parents 888c856 + b30bdc3 commit 9ce593d
Showing 1 changed file with 27 additions and 19 deletions.
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

0 comments on commit 9ce593d

Please sign in to comment.