Skip to content

Commit 98decc6

Browse files
committed
Extract logic to determine query string parameters
1 parent 61b7540 commit 98decc6

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/LocalizedUrlGenerator.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ public function __construct()
4040
public function generateFromRequest(string $locale = null, $parameters = null, bool $absolute = true, bool $keepQuery = true): string
4141
{
4242
$urlBuilder = UrlBuilder::make(Request::fullUrl());
43-
44-
if ($keepQuery === false) {
45-
$urlBuilder->setQueryString([]);
46-
}
43+
$requestQueryString = $urlBuilder->getQueryStringArray();
4744

4845
$currentDomain = $urlBuilder->getHost();
4946
$currentLocaleSlug = $urlBuilder->getSlugs()[0] ?? null;
@@ -71,11 +68,9 @@ public function generateFromRequest(string $locale = null, $parameters = null, b
7168
// $queryStringParameters contains "key" => "value" pairs.
7269
list($routePlaceholders, $routeParameters, $queryStringParameters) = $this->extractRouteAndQueryStringParameters($routeUri, $normalizedParameters);
7370

74-
// If any query string parameters have been passed to this method,
75-
// and we need to keep the query string, set them in the UrlBuilder.
76-
if (count($queryStringParameters) > 0 && $keepQuery === true) {
77-
$urlBuilder->setQueryString($queryStringParameters);
78-
}
71+
$urlBuilder->setQueryString(
72+
$this->determineQueryStringParameters($requestQueryString, $queryStringParameters, $keepQuery)
73+
);
7974

8075
// Merge the route parameters with the query string parameters, if any.
8176
$namedRouteParameters = array_merge($routeParameters, $urlBuilder->getQueryStringArray());
@@ -210,6 +205,28 @@ protected function updateLocaleInSlugs(array $slugs, string $locale): array
210205
return $slugs;
211206
}
212207

208+
/**
209+
* Determine what query string parameters to use.
210+
*
211+
* @param array $requestQueryString
212+
* @param array $queryStringParameters
213+
* @param bool $keepQuery
214+
*
215+
* @return array
216+
*/
217+
protected function determineQueryStringParameters(array $requestQueryString, array $queryStringParameters, bool $keepQuery): array
218+
{
219+
if ($keepQuery === false) {
220+
return [];
221+
}
222+
223+
if (count($queryStringParameters) > 0) {
224+
return $queryStringParameters;
225+
}
226+
227+
return $requestQueryString;
228+
}
229+
213230
/**
214231
* Extract URI parameters and query string parameters.
215232
*

0 commit comments

Comments
 (0)