Skip to content

Commit

Permalink
feat: only use int, bool, and string values for attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
callinmullaney committed Nov 22, 2021
1 parent 4c3c4b5 commit b30bdc3
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/AddAttributesTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +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];
}
else {
switch (gettype($value)) {
case 'integer':
case 'boolean':
case 'string':
$value = [strval($value)];
break;
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 b30bdc3

Please sign in to comment.