Skip to content

Commit e716638

Browse files
committed
Fix incorrect classes assignation and controller rendering
1 parent 1862d71 commit e716638

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/Dto/StimulusControllersDto.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function addController(string $controllerName, array $controllerValues =
3636
foreach ($controllerClasses as $key => $class) {
3737
$key = $this->escapeAsHtmlAttr($this->normalizeKeyName($key));
3838

39-
$this->values['data-'.$controllerName.'-'.$key.'-class'] = $class;
39+
$this->classes['data-'.$controllerName.'-'.$key.'-class'] = $class;
4040
}
4141
}
4242

@@ -46,15 +46,11 @@ public function __toString(): string
4646
return '';
4747
}
4848

49-
return rtrim(
50-
'data-controller="'.implode(' ', $this->controllers).'" '.
51-
implode(' ', array_map(function (string $attribute, string $value): string {
52-
return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"';
53-
}, array_keys($this->values), $this->values)).' '.
54-
implode(' ', array_map(function (string $attribute, string $value): string {
55-
return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"';
56-
}, array_keys($this->classes), $this->classes))
57-
);
49+
return rtrim(implode(' ', array_filter([
50+
'data-controller="'.implode(' ', $this->controllers).'"',
51+
$this->formatDataAttribute($this->values),
52+
$this->formatDataAttribute($this->classes),
53+
])));
5854
}
5955

6056
public function toArray(): array
@@ -82,4 +78,11 @@ private function normalizeKeyName(string $str): string
8278
// Adapted from ByteString::snake
8379
return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1-\2', $str));
8480
}
81+
82+
private function formatDataAttribute(array $data): string
83+
{
84+
return implode(' ', array_map(function (string $attribute, string $value): string {
85+
return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"';
86+
}, array_keys($data), $data));
87+
}
8588
}

0 commit comments

Comments
 (0)