Skip to content

Commit

Permalink
Improve Uri class by adding useful tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Feb 17, 2018
1 parent ccf0f9c commit 0a79788
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 200 deletions.
17 changes: 15 additions & 2 deletions system/src/Grav/Framework/Psr7/AbstractUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace Grav\Framework\Psr7;

use Grav\Framework\Uri\UriFilter;
use Grav\Framework\Uri\UriHelper;
use Psr\Http\Message\UriInterface;

/**
Expand All @@ -19,6 +18,11 @@
*/
abstract class AbstractUri implements UriInterface
{
protected static $defaultPorts = [
'http' => 80,
'https' => 443
];

/** @var string Uri scheme. */
private $scheme = '';

Expand Down Expand Up @@ -384,9 +388,18 @@ private function validate()
}
}

protected function isDefaultPort()
{
$scheme = $this->scheme;
$port = $this->port;

return $this->port === null
|| (isset(static::$defaultPorts[$scheme]) && $port === static::$defaultPorts[$scheme]);
}

private function unsetDefaultPort()
{
if ($this->port !== null && UriHelper::isDefaultPort($this)) {
if ($this->isDefaultPort()) {
$this->port = null;
}
}
Expand Down
99 changes: 94 additions & 5 deletions system/src/Grav/Framework/Uri/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Grav\Framework\Uri;

use Grav\Framework\Psr7\AbstractUri;
use GuzzleHttp\Psr7\Uri as GuzzleUri;
use Psr\Http\Message\UriInterface;

/**
Expand All @@ -18,10 +19,11 @@
*/
class Uri extends AbstractUri
{
protected $queryParams;
/** @var array Array of Uri query. */
private $queryParams;

/**
* Uri constructor.
* You can use `UriFactory` functions to create new `Uri` objects.
*
* @param array $parts
* @throws \InvalidArgumentException
Expand Down Expand Up @@ -88,7 +90,7 @@ public function getQueryParam($key)
*/
public function withoutQueryParam($key)
{
return UriHelper::withoutQueryParam($this, $key);
return GuzzleUri::withoutQueryValue($this, $key);
}

/**
Expand All @@ -98,7 +100,7 @@ public function withoutQueryParam($key)
*/
public function withQueryParam($key, $value)
{
return UriHelper::withQueryParam($this, $key, $value);
return GuzzleUri::withQueryValue($this, $key, $value);
}

/**
Expand All @@ -120,6 +122,93 @@ public function getQueryParams()
*/
public function withQueryParams(array $params)
{
return empty($params) ? $this->withQuery('') : UriHelper::withQueryParams($this, $params);
$query = $params ? http_build_query($params) : '';

return $this->withQuery($query);
}

/**
* Whether the URI has the default port of the current scheme.
*
* `$uri->getPort()` may return the standard port. This method can be used for some non-http/https Uri.
*
* @return bool
*/
public function isDefaultPort()
{
return $this->getPort() === null || GuzzleUri::isDefaultPort($this);
}

/**
* Whether the URI is absolute, i.e. it has a scheme.
*
* An instance of UriInterface can either be an absolute URI or a relative reference. This method returns true
* if it is the former. An absolute URI has a scheme. A relative reference is used to express a URI relative
* to another URI, the base URI. Relative references can be divided into several forms:
* - network-path references, e.g. '//example.com/path'
* - absolute-path references, e.g. '/path'
* - relative-path references, e.g. 'subpath'
*
* @return bool
* @link https://tools.ietf.org/html/rfc3986#section-4
*/
public function isAbsolute()
{
return GuzzleUri::isAbsolute($this);
}

/**
* Whether the URI is a network-path reference.
*
* A relative reference that begins with two slash characters is termed an network-path reference.
*
* @return bool
* @link https://tools.ietf.org/html/rfc3986#section-4.2
*/
public function isNetworkPathReference()
{
return GuzzleUri::isNetworkPathReference($this);
}

/**
* Whether the URI is a absolute-path reference.
*
* A relative reference that begins with a single slash character is termed an absolute-path reference.
*
* @return bool
* @link https://tools.ietf.org/html/rfc3986#section-4.2
*/
public function isAbsolutePathReference()
{
return GuzzleUri::isAbsolutePathReference($this);
}

/**
* Whether the URI is a relative-path reference.
*
* A relative reference that does not begin with a slash character is termed a relative-path reference.
*
* @return bool
* @link https://tools.ietf.org/html/rfc3986#section-4.2
*/
public function isRelativePathReference()
{
return GuzzleUri::isRelativePathReference($this);
}

/**
* Whether the URI is a same-document reference.
*
* A same-document reference refers to a URI that is, aside from its fragment
* component, identical to the base URI. When no base URI is given, only an empty
* URI reference (apart from its fragment) is considered a same-document reference.
*
* @param UriInterface|null $base An optional base URI to compare against
* @return bool
* @link https://tools.ietf.org/html/rfc3986#section-4.4
*/
public function isSameDocumentReference(UriInterface $base = null)
{
return GuzzleUri::isSameDocumentReference($this, $base);
}
}
193 changes: 0 additions & 193 deletions system/src/Grav/Framework/Uri/UriHelper.php

This file was deleted.

0 comments on commit 0a79788

Please sign in to comment.