From 2a987020b9666ef17743bb3977f188cab32bf245 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Mon, 30 Oct 2023 00:34:23 -0300 Subject: [PATCH] Adds comments --- src/Http/TurboNativeRedirectResponse.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Http/TurboNativeRedirectResponse.php b/src/Http/TurboNativeRedirectResponse.php index 0de6ed3..01a689b 100644 --- a/src/Http/TurboNativeRedirectResponse.php +++ b/src/Http/TurboNativeRedirectResponse.php @@ -7,12 +7,23 @@ class TurboNativeRedirectResponse extends RedirectResponse { - public static function createFromFallbackUrl(string $action, string $fallbackUrl) + /** + * Factory method that builds a new instance of the TurboNativeRedirectResponse + * and extracts the query strings from the given action and fallback URL. + */ + public static function createFromFallbackUrl(string $action, string $fallbackUrl): self { return (new self(route("turbo_{$action}_historical_location"))) ->withQueryString((new self($fallbackUrl))->getQueryString()); } + /** + * Sets the flashed data via query strings when redirecting to Turbo Native routes. + * + * @param string $key + * @param mixed $value + * @return self + */ public function with($key, $value = null) { $params = $this->getQueryString(); @@ -21,6 +32,9 @@ public function with($key, $value = null) ->setTargetUrl($this->getTargetUrl().'?'.http_build_query($params + [$key => urlencode($value)])); } + /** + * Sets multiple query strings at the same time. + */ protected function withQueryString(array $params): self { foreach ($params as $key => $val) { @@ -30,6 +44,9 @@ protected function withQueryString(array $params): self return $this; } + /** + * Returns the query string as an array. + */ protected function getQueryString(): array { parse_str(str_contains($this->getTargetUrl(), '?') ? Str::after($this->getTargetUrl(), '?') : '', $query); @@ -37,6 +54,9 @@ protected function getQueryString(): array return $query; } + /** + * Returns the target URL without the query strings. + */ protected function withoutQueryStrings(): self { $fragment = str_contains($this->getTargetUrl(), '#') ? Str::after($this->getTargetUrl(), '#') : '';