Skip to content

Commit

Permalink
Merge pull request #5
Browse files Browse the repository at this point in the history
Fixed: 
UrlBuilder creates errorneous URLs when there is no path but query parameter.
  • Loading branch information
discordier authored Feb 7, 2022
2 parents c193079 + 28ab354 commit 753ac00
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
23 changes: 20 additions & 3 deletions src/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ class UrlBuilder
*/
protected $fragment;

/**
* Flag to determine if the query string is to be prefixed with a question mark.
*
* See issue #3
*
* @var bool
*/
private $queryWasPrefixed = false;

/**
* Create a new instance.
*
Expand Down Expand Up @@ -558,6 +567,8 @@ public function getUrl()
$url .= '/';
}
$url .= '?';
} elseif ($this->queryWasPrefixed) {
$url .= '?';
}

$url .= $query;
Expand Down Expand Up @@ -593,17 +604,23 @@ private function parseUrl($url)
return [];
}

if (count($parsed) !== 1) {
$this->queryWasPrefixed = false;
return $parsed;
}
if (
(count($parsed) === 1)
&& isset($parsed['path'])
&& (0 === strpos($parsed['path'], '?') || false !== strpos($parsed['path'], '&'))
isset($parsed['path'])
&& (($queryWasPrefixed = (0 === strpos($parsed['path'], '?'))) || false !== strpos($parsed['path'], '&'))
) {
$parsed = array(
'query' => $parsed['path']
);

$this->queryWasPrefixed = $queryWasPrefixed;

return $parsed;
}
$this->queryWasPrefixed = isset($parsed['query']);

return $parsed;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/UrlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function prepareUrls()
),
array(
'input' => '?authenticated=1&token=123&perform',
'expected' => 'authenticated=1&token=123&perform',
'expected' => '?authenticated=1&token=123&perform',
'query' => 'authenticated=1&token=123&perform',
),
array(
Expand Down

0 comments on commit 753ac00

Please sign in to comment.