Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introducing a uri_get_meta_data function
- Loading branch information
Showing
9 changed files
with
189 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters