Skip to content

Commit

Permalink
Adds comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Oct 30, 2023
1 parent 90735b1 commit 2a98702
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Http/TurboNativeRedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -30,13 +44,19 @@ 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);

return $query;
}

/**
* Returns the target URL without the query strings.
*/
protected function withoutQueryStrings(): self
{
$fragment = str_contains($this->getTargetUrl(), '#') ? Str::after($this->getTargetUrl(), '#') : '';
Expand Down

0 comments on commit 2a98702

Please sign in to comment.