@@ -36,7 +36,7 @@ public function addController(string $controllerName, array $controllerValues =
36
36
foreach ($ controllerClasses as $ key => $ class ) {
37
37
$ key = $ this ->escapeAsHtmlAttr ($ this ->normalizeKeyName ($ key ));
38
38
39
- $ this ->values ['data- ' .$ controllerName .'- ' .$ key .'-class ' ] = $ class ;
39
+ $ this ->classes ['data- ' .$ controllerName .'- ' .$ key .'-class ' ] = $ class ;
40
40
}
41
41
}
42
42
@@ -46,15 +46,11 @@ public function __toString(): string
46
46
return '' ;
47
47
}
48
48
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
+ ])));
58
54
}
59
55
60
56
public function toArray (): array
@@ -82,4 +78,11 @@ private function normalizeKeyName(string $str): string
82
78
// Adapted from ByteString::snake
83
79
return strtolower (preg_replace (['/([A-Z]+)([A-Z][a-z])/ ' , '/([a-z\d])([A-Z])/ ' ], '\1-\2 ' , $ str ));
84
80
}
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
+ }
85
88
}
0 commit comments