|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\UX\Turbo\Helper; |
| 13 | + |
| 14 | +/** |
| 15 | + * @see https://turbo.hotwired.dev/reference/streams |
| 16 | + */ |
| 17 | +final class TurboStream |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Appends to the element(s) designated by the target CSS selector. |
| 21 | + */ |
| 22 | + public static function append(string $target, string $html): string |
| 23 | + { |
| 24 | + return self::wrap('append', $target, $html); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Prepends to the element(s) designated by the target CSS selector. |
| 29 | + */ |
| 30 | + public static function prepend(string $target, string $html): string |
| 31 | + { |
| 32 | + return self::wrap('prepend', $target, $html); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Replaces the element(s) designated by the target CSS selector. |
| 37 | + */ |
| 38 | + public static function replace(string $target, string $html, bool $morph = false): string |
| 39 | + { |
| 40 | + return self::wrap('replace', $target, $html, $morph ? ' method="morph"' : ''); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Updates the content of the element(s) designated by the target CSS selector. |
| 45 | + */ |
| 46 | + public static function update(string $target, string $html, bool $morph = false): string |
| 47 | + { |
| 48 | + return self::wrap('update', $target, $html, $morph ? ' method="morph"' : ''); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Removes the element(s) designated by the target CSS selector. |
| 53 | + */ |
| 54 | + public static function remove(string $target): string |
| 55 | + { |
| 56 | + return \sprintf('<turbo-stream action="remove" targets="%s"></turbo-stream>', htmlspecialchars($target)); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Inserts before the element(s) designated by the target CSS selector. |
| 61 | + */ |
| 62 | + public static function before(string $target, string $html): string |
| 63 | + { |
| 64 | + return self::wrap('before', $target, $html); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Inserts after the element(s) designated by the target CSS selector. |
| 69 | + */ |
| 70 | + public static function after(string $target, string $html): string |
| 71 | + { |
| 72 | + return self::wrap('after', $target, $html); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Initiates a Page Refresh to render new content with morphing. |
| 77 | + * |
| 78 | + * @see Initiates a Page Refresh to render new content with morphing. |
| 79 | + */ |
| 80 | + public static function refresh(?string $requestId = null): string |
| 81 | + { |
| 82 | + if (null === $requestId) { |
| 83 | + return '<turbo-stream action="refresh"></turbo-stream>'; |
| 84 | + } |
| 85 | + |
| 86 | + return \sprintf('<turbo-stream action="refresh" request-id="%s"></turbo-stream>', htmlspecialchars($requestId)); |
| 87 | + } |
| 88 | + |
| 89 | + private static function wrap(string $action, string $target, string $html, string $attr = ''): string |
| 90 | + { |
| 91 | + return \sprintf(<<<EOHTML |
| 92 | + <turbo-stream action="%s" targets="%s"%s> |
| 93 | + <template>%s</template> |
| 94 | + </turbo-stream> |
| 95 | + EOHTML, $action, htmlspecialchars($target), $attr, $html); |
| 96 | + } |
| 97 | +} |
0 commit comments