Skip to content

Commit

Permalink
Add utility methods to Uri
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Apr 15, 2019
1 parent 461aa8d commit 8a8f4e9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 41 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Parable Http

## 0.2.1

_Changes_
- `Uri` has gained 2 methods: `getUriBaseString()` and `getUriRestString`. Now it's easily possible to get the base uri, or only the path/query/fragment side of one.

## 0.2.0

_Changes_
Expand Down
102 changes: 62 additions & 40 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,67 @@ public static function setPropertiesFromArray(self $uri, array $values): self

public function getUriString(): string
{
return $this->__toString();
$restUri = $this->getUriRestString();

if (strlen($restUri) > 0 && !in_array(substr($restUri, 0, 1), ['?', '#'])) {
$restUri = '/' . $restUri;
}

return $this->getUriBaseString() . $restUri;
}

public function getUriBaseString(): string
{
$parts = [];

$parts[] = $this->getScheme();
$parts[] = '://';

if ($this->getUser() !== null) {
$parts[] = $this->getUser();

if ($this->getPass() !== null) {
$parts[] = ':';
$parts[] = $this->getPass();
}

$parts[] = '@';
}

$parts[] = $this->getHost();

// We need a port, but we ignore the defaults for http/https
if ($this->getPort() !== null
&& (!$this->isHttps() && $this->getPort() !== 80
|| $this->isHttps() && $this->getPort() !== 443
)
) {
$parts[] = ':';
$parts[] = $this->getPort();
}

return implode($parts);
}

public function getUriRestString(): string
{
$parts = [];

if ($this->getPath() !== null) {
$parts[] = $this->getPath();
}

if ($this->getQuery() !== null) {
$parts[] = '?';
$parts[] = $this->getQuery();
}

if ($this->getFragment() !== null) {
$parts[] = '#';
$parts[] = $this->getFragment();
}

return implode($parts);
}

public function getScheme(): ?string
Expand Down Expand Up @@ -201,44 +261,6 @@ public function withFragment(string $value): self

public function __toString(): string
{
$parts = [];

$parts[] = $this->getScheme();
$parts[] = '://';

if ($this->getUser() !== null) {
$parts[] = $this->getUser();

if ($this->getPass() !== null) {
$parts[] = ':';
$parts[] = $this->getPass();
}

$parts[] = '@';
}

$parts[] = $this->getHost();

if ($this->getPort() !== null) {
$parts[] = ':';
$parts[] = $this->getPort();
}

if ($this->getPath() !== null) {
$parts[] = '/';
$parts[] = $this->getPath();
}

if ($this->getQuery() !== null) {
$parts[] = '?';
$parts[] = $this->getQuery();
}

if ($this->getFragment() !== null) {
$parts[] = '#';
$parts[] = $this->getFragment();
}

return implode($parts);
return $this->getUriString();
}
}
12 changes: 11 additions & 1 deletion tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ public function setUp()
$this->uri = new Uri('https://user:[email protected]:1337/parable?doc=intro#yes');
}

public function testGetUriBuildsTheRightUri()
public function testGetUriStringBuildsTheRightUri()
{
self::assertSame('https://user:[email protected]:1337/parable?doc=intro#yes', $this->uri->getUriString());
}

public function testGetUriBaseStringBuildsOnlyTheBase()
{
self::assertSame('https://user:[email protected]:1337', $this->uri->getUriBaseString());
}

public function testGetUriRestStringBuildsOnlyTheRest()
{
self::assertSame('parable?doc=intro#yes', $this->uri->getUriRestString());
}

public function testUriToString()
{
self::assertSame('https://user:[email protected]:1337/parable?doc=intro#yes', (string)$this->uri);
Expand Down

0 comments on commit 8a8f4e9

Please sign in to comment.