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: Revert "fix: pass any attribute value type to markup" #37

Closed
wants to merge 1 commit into from
Closed
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: 19 additions & 27 deletions src/AddAttributesTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,27 @@ public function add_attributes($context, $additional_attributes = []) {

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

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':
if (is_array($value)) {
foreach ($value as $index => $item) {
// Handle bem() output.
if ($value instanceof Attribute) {
$value = $value->toArray()[$key];
if ($item instanceof Attribute) {
// Remove the item.
unset($value[$index]);
$value = array_merge($value, $item->toArray()[$key]);
}
else {
$value = [strval($value)];
}
break;

default:
// Set value to an empty string.
$value = '';
break;
}
}
else {
// Handle bem() output.
if ($value instanceof Attribute) {
$value = $value->toArray()[$key];
}
elseif (is_string($value)) {
$value = [$value];
}
else {
continue;
}
}
// Merge additional attribute values with existing ones.
if ($context['attributes']->offsetExists($key)) {
Expand Down