@@ -37,15 +37,15 @@ public static function prepend(string $target, string $html): string
37
37
*/
38
38
public static function replace (string $ target , string $ html , bool $ morph = false ): string
39
39
{
40
- return self ::wrap ('replace ' , $ target , $ html , $ morph ? ' method="morph" ' : '' );
40
+ return self ::wrap ('replace ' , $ target , $ html , $ morph ? [ ' method="morph" '] : [] );
41
41
}
42
42
43
43
/**
44
44
* Updates the content of the element(s) designated by the target CSS selector.
45
45
*/
46
46
public static function update (string $ target , string $ html , bool $ morph = false ): string
47
47
{
48
- return self ::wrap ('update ' , $ target , $ html , $ morph ? ' method="morph" ' : '' );
48
+ return self ::wrap ('update ' , $ target , $ html , $ morph ? [ ' method="morph" '] : [] );
49
49
}
50
50
51
51
/**
@@ -86,12 +86,28 @@ public static function refresh(?string $requestId = null): string
86
86
return \sprintf ('<turbo-stream action="refresh" request-id="%s"></turbo-stream> ' , htmlspecialchars ($ requestId ));
87
87
}
88
88
89
- private static function wrap (string $ action , string $ target , string $ html , string $ attr = '' ): string
89
+ /**
90
+ * Custom Action.
91
+ *
92
+ * @param array<string> $attr
93
+ */
94
+ public static function custom (string $ action , string $ target , string $ html , array $ attr = []): string
90
95
{
96
+ return self ::wrap ($ action , $ target , $ html , $ attr );
97
+ }
98
+
99
+ /**
100
+ * @param array<string> $attr
101
+ */
102
+ private static function wrap (string $ action , string $ target , string $ html , array $ attr = []): string
103
+ {
104
+ // Join array elements with a space and prepend a leading space
105
+ $ atrrString = empty ($ attr ) ? '' : ' ' .implode (' ' , $ attr );
106
+
91
107
return \sprintf (<<<EOHTML
92
108
<turbo-stream action="%s" targets="%s"%s>
93
109
<template>%s</template>
94
110
</turbo-stream>
95
- EOHTML , $ action , htmlspecialchars ($ target ), $ attr , $ html );
111
+ EOHTML , $ action , htmlspecialchars ($ target ), $ atrrString , $ html );
96
112
}
97
113
}
0 commit comments