Skip to content

Commit

Permalink
Merge pull request #3 from transprime-research/fix-remove-question-ma…
Browse files Browse the repository at this point in the history
…rk-from-url-when-no-query

fix: Remove question mark from url if there's no query
  • Loading branch information
omitobi committed Aug 23, 2023
2 parents cc7f640 + ab142c0 commit 894ce78
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function __toString(): string

public function toString(): string
{
return implode($this->toArray());
return implode(array_filter($this->toArray()));
}

public function toArray(): array
Expand All @@ -94,7 +94,7 @@ public function toArray(): array
return [
$this->fullDomain,
$this->path,
'?',
$this->query ? '?' : '',
http_build_query($this->query),
];
}
Expand All @@ -104,7 +104,7 @@ public function toArray(): array
$this->domain,
$this->port ? ':' . $this->port : $this->port,
$this->path,
'?',
$this->query ? '?' : '',
http_build_query($this->query)
];
}
Expand Down
17 changes: 17 additions & 0 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ public function testUrlIsCorrectForSetFullDomain(): void
$this->assertEquals('http://localhost:8080/api/hello?name=John&public=yes', (string) $url);
}

public function testUrlIsCorrectWhenThereIsNoQuery(): void
{
$url = Url::make(
fullDomain: 'http://localhost:8080',
path: '/api/hello',
);

$this->assertEquals([
'http://localhost:8080',
'/api/hello',
'',
'',
], $url->toArray());

$this->assertEquals('http://localhost:8080/api/hello', (string) $url);
}

public function testUrlIsCorrectWhenSetOnConstructor(): void
{
$url = Url::make(
Expand Down

0 comments on commit 894ce78

Please sign in to comment.