@@ -37,15 +37,15 @@ public static function prepend(string $target, string $html): string
3737 */
3838 public static function replace (string $ target , string $ html , bool $ morph = false ): string
3939 {
40- return self ::wrap ('replace ' , $ target , $ html , $ morph ? ' method="morph" ' : '' );
40+ return self ::wrap ('replace ' , $ target , $ html , $ morph ? [ ' method="morph" '] : [] );
4141 }
4242
4343 /**
4444 * Updates the content of the element(s) designated by the target CSS selector.
4545 */
4646 public static function update (string $ target , string $ html , bool $ morph = false ): string
4747 {
48- return self ::wrap ('update ' , $ target , $ html , $ morph ? ' method="morph" ' : '' );
48+ return self ::wrap ('update ' , $ target , $ html , $ morph ? [ ' method="morph" '] : [] );
4949 }
5050
5151 /**
@@ -86,12 +86,24 @@ public static function refresh(?string $requestId = null): string
8686 return \sprintf ('<turbo-stream action="refresh" request-id="%s"></turbo-stream> ' , htmlspecialchars ($ requestId ));
8787 }
8888
89- private static function wrap (string $ action , string $ target , string $ html , string $ attr = '' ): string
89+ /**
90+ * Custom Action
91+ * @param array<string> $attr
92+ */
93+ public static function custom (string $ action , string $ target , string $ html , array $ attr = []): string
94+ {
95+ return self ::wrap ($ action , $ target , $ html , $ attr );
96+ }
97+
98+ private static function wrap (string $ action , string $ target , string $ html , array $ attr = []): string
9099 {
100+ // Join array elements with a space and prepend a leading space
101+ $ atrrString = empty ($ attr ) ? '' : ' ' . implode (' ' , $ attr );
102+
91103 return \sprintf (<<<EOHTML
92104 <turbo-stream action="%s" targets="%s"%s>
93105 <template>%s</template>
94106 </turbo-stream>
95- EOHTML , $ action , htmlspecialchars ($ target ), $ attr , $ html );
107+ EOHTML , $ action , htmlspecialchars ($ target ), $ atrrString , $ html );
96108 }
97109}
0 commit comments