Skip to content

Commit

Permalink
introducing a uri_get_meta_data function
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jul 1, 2016
1 parent c1c6ff0 commit d793b2b
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 19 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -31,7 +31,8 @@
"autoload": {
"psr-4": {
"League\\Uri\\": "src"
}
},
"files": ["src/functions_include.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Expand Up @@ -20,6 +20,9 @@
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory suffix="include.php">src</directory>
</exclude>
</whitelist>
</filter>

Expand Down
15 changes: 7 additions & 8 deletions src/Modifiers/Normalize.php
Expand Up @@ -32,15 +32,14 @@ class Normalize extends AbstractUriModifier
*/
public function __invoke($uri)
{
$this->assertUriObject($uri);
$modifier = new Pipeline([
new HostToAscii(),
new KsortQuery(),
]);

static $modifier;
if (!$modifier instanceof Pipeline) {
$modifier = new Pipeline([
new HostToAscii(),
new RemoveDotSegments(),
new KsortQuery(),
]);
$path = $uri->getPath();
if (!\League\Uri\uri_get_meta_data($uri)['relative_path']) {
$modifier = $modifier->pipe(new RemoveDotSegments());
}

return $modifier($uri)->withScheme($uri->getScheme());
Expand Down
6 changes: 5 additions & 1 deletion src/Modifiers/Relativize.php
Expand Up @@ -11,6 +11,7 @@
*/
namespace League\Uri\Modifiers;

use InvalidArgumentException;
use League\Uri\Interfaces\Uri;
use Psr\Http\Message\UriInterface;

Expand Down Expand Up @@ -38,7 +39,10 @@ class Relativize extends AbstractUriModifier
*/
public function __construct($uri)
{
$this->assertUriObject($uri);
if (!\League\Uri\uri_get_meta_data($uri)['absolute_uri']) {
throw new InvalidArgumentException('The Base URI must be an Absolute URI');
}

$this->uri = $this->hostToAscii($uri);
}

Expand Down
16 changes: 10 additions & 6 deletions src/Modifiers/Resolve.php
Expand Up @@ -48,17 +48,21 @@ public function __construct($uri)
*/
public function __invoke($payload)
{
$this->assertUriObject($payload);
if ('' !== $payload->getScheme()) {
return $payload;
$meta = \League\Uri\uri_get_meta_data($payload);
$path = $payload->getPath();
if ($meta['absolute_uri']) {
return $payload
->withPath((new Path($path))->withoutDotSegments()->__toString());
}

if ('' !== $payload->getAuthority()) {
return $payload->withScheme($this->uri->getScheme());
if ($meta['network_path']) {
return $payload
->withScheme($this->uri->getScheme())
->withPath((new Path($path))->withoutDotSegments()->__toString());
}

$userInfo = explode(':', $this->uri->getUserInfo(), 2);
$components = $this->resolvePathAndQuery($payload->getPath(), $payload->getQuery());
$components = $this->resolvePathAndQuery($path, $payload->getQuery());

return $payload
->withPath($this->formatPath($components['path']))
Expand Down
79 changes: 79 additions & 0 deletions src/functions.php
@@ -0,0 +1,79 @@
<?php
/**
* League.Uri (http://uri.thephpleague.com)
*
* @package League.uri
* @author Ignace Nyamagana Butera <nyamsprod@gmail.com>
* @copyright 2016 Ignace Nyamagana Butera
* @license https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
* @version 4.2.0
* @link https://github.com/thephpleague/uri/
*/
namespace League\Uri;

use InvalidArgumentException;
use League\Uri\Interfaces\Uri;
use Psr\Http\Message\UriInterface;

/**
* A function to give information about URI Reference
*
* This function returns an associative array representing the URI Reference information:
* each key represents a given state and each value is a boolean to indicate the current URI
* status against the declared state. For any given URI only one of the listed state can be valid.
*
* <ul>
* <li>absolute_uri: Tell whether the URI is absolute (ie contains a non_empty scheme)
* <li>network_path: Tell whether the URI is a network_path relative reference
* <li>absolute_path: Tell whether the URI is a absolute_path relative reference
* <li>relative_path: Tell whether the URI is a relative_path relative reference
* </ul>
*
* @link https://tools.ietf.org/html/rfc3986#section_4.2
* @link https://tools.ietf.org/html/rfc3986#section_4.3
* @since 4.2.0
*
* @param Uri|UriInterface $uri
*
* @throws InvalidArgumentException if the submitted Uri is invalid
*
* @return array
*/
function uri_get_meta_data($uri)
{
if (!$uri instanceof Uri && !$uri instanceof UriInterface) {
throw new InvalidArgumentException(
'URI passed must implement PSR_7 UriInterface or League\Uri Uri interface'
);
}

$infos = [
'absolute_uri' => false,
'network_path' => false,
'absolute_path' => false,
'relative_path' => false,
];

if ('' !== $uri->getScheme()) {
$infos['absolute_uri'] = true;

return $infos;
}

if ('' !== $uri->getAuthority()) {
$infos['network_path'] = true;

return $infos;
}

$path = $uri->getPath();
if (isset($path[0]) && '/' === $path[0]) {
$infos['absolute_path'] = true;

return $infos;
}

$infos['relative_path'] = true;

return $infos;
}
6 changes: 6 additions & 0 deletions src/functions_include.php
@@ -0,0 +1,6 @@
<?php

// Don't redefine the functions if included multiple times.
if (!function_exists('League\Uri\uri_get_meta_data')) {
require __DIR__.'/functions.php';
}
75 changes: 75 additions & 0 deletions test/FunctionsTest.php
@@ -0,0 +1,75 @@
<?php

namespace League\Uri\Test;

use League\Uri;
use League\Uri\Schemes\Http as HttpUri;
use PHPUnit_Framework_TestCase;

/**
* @group functions
*/
class FunctionsTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider uriProvider
*/
public function testStat($uri, $absolute_uri, $network_path, $absolute_path, $relative_path)
{
$stats = Uri\uri_get_meta_data($uri);
$this->assertInternalType('array', $stats);
$this->assertSame($absolute_uri, $stats['absolute_uri']);
$this->assertSame($network_path, $stats['network_path']);
$this->assertSame($absolute_path, $stats['absolute_path']);
$this->assertSame($relative_path, $stats['relative_path']);
}

public function uriProvider()
{
return [
'absolute uri' => [
'uri' => HttpUri::createFromString('http://a/p?q#f'),
'absolute_uri' => true,
'network_path' => false,
'absolute_path' => false,
'relative_path' => false,
],
'network relative uri' => [
'uri' => HttpUri::createFromString('//a/p?q#f'),
'absolute_uri' => false,
'network_path' => true,
'absolute_path' => false,
'relative_path' => false,
],
'path absolute uri' => [
'uri' => HttpUri::createFromString('/p?q#f'),
'absolute_uri' => false,
'network_path' => false,
'absolute_path' => true,
'relative_path' => false,
],
'path relative uri with non empty path' => [
'uri' => HttpUri::createFromString('p?q#f'),
'absolute_uri' => false,
'network_path' => false,
'absolute_path' => false,
'relative_path' => true,
],
'path relative uri with empty' => [
'uri' => HttpUri::createFromString('?q#f'),
'absolute_uri' => false,
'network_path' => false,
'absolute_path' => false,
'relative_path' => true,
],
];
}

/**
* @expectedException InvalidArgumentException
*/
public function testStatThrowsInvalidArgumentException()
{
Uri\uri_get_meta_data('http://example.com');
}
}
5 changes: 2 additions & 3 deletions test/Modifiers/UriModifierTest.php
Expand Up @@ -185,10 +185,9 @@ public function emptyAbsolutePathProvider()
/**
* @expectedException InvalidArgumentException
*/
public function relativizeLetThrowUriException()
public function testRelativizeThrowInvalidArgumentException()
{
$dataUri = DataUri::createFromString('data:text/plain;charset=us-ascii,Bonjour%20le%20monde!');
(new Relativize($dataUri1))->__invoke($dataUri);
new Relativize(HttpUri::createFromString('//example.com'));
}

/**
Expand Down

0 comments on commit d793b2b

Please sign in to comment.