Skip to content

Commit

Permalink
More refactoring and Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Sep 13, 2015
1 parent abfe0ce commit f010f45
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
57 changes: 32 additions & 25 deletions src/Utilities/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ public static function substituteAttributes(array $attributes, $uri)
*/
public static function unparse($parsed)
{
$url = '';

if (empty($parsed)) {
return $url;
}
if (empty($parsed)) return '';

self::checkParsedUrl($parsed);

Expand All @@ -158,6 +154,8 @@ public static function unparse($parsed)
| ------------------------------------------------------------------------------------------------
*/
/**
* Check parsed URL.
*
* @param array $parsed
*
* @return array
Expand All @@ -180,21 +178,21 @@ private static function checkParsedUrl(array &$parsed)
}

/**
* Get Url.
* Get URL.
*
* @param array $parsed
*
* @return string
*/
private static function getUrl(array $parsed)
{
$hierPart = self::getHierPart($parsed);
$url = '';

if (strlen($parsed['scheme'])) {
return $parsed['scheme'] . ':' . $hierPart;
$url = $parsed['scheme'] . ':' . self::getHierPart($parsed);
}

return $hierPart;
return $url;
}

/**
Expand All @@ -206,13 +204,14 @@ private static function getUrl(array $parsed)
*/
private static function getHierPart(array $parsed)
{
$path = $parsed['path'];
$authority = self::getAuthority($parsed);

if (strlen($authority)) {
return '//' . $authority . $parsed['path'];
$path = '//' . $authority . $path;
}

return $parsed['path'];
return $path;
}

/**
Expand Down Expand Up @@ -243,11 +242,13 @@ private static function getAuthority(array $parsed)
*/
private static function getUserInfo(array $parsed)
{
$userInfo = '';

if (strlen($parsed['pass'])) {
return $parsed['user'] . ':' . $parsed['pass'];
$userInfo = $parsed['user'] . ':' . $parsed['pass'];
}

return '';
return $userInfo;
}

/**
Expand All @@ -259,42 +260,48 @@ private static function getUserInfo(array $parsed)
*/
private static function getHost(array $parsed)
{
$host = $parsed['host'];

if ( ! empty((string) $parsed['port'])) {
return $parsed['host'] . ':' . $parsed['port'];
$host = $host . ':' . $parsed['port'];
}

return $parsed['host'];
return $host;
}

/**
* Get fragment.
* Get Query.
*
* @param array $parsed
*
* @return string
*/
private static function getFragment(array $parsed)
private static function getQuery(array $parsed)
{
if (strlen($parsed['fragment'])) {
return '#' . $parsed['fragment'];
$query = '';

if (strlen($parsed['query'])) {
$query = '?' . $parsed['query'];
}

return '';
return $query;
}

/**
* Get Query.
* Get fragment.
*
* @param array $parsed
*
* @return string
*/
private static function getQuery(array $parsed)
private static function getFragment(array $parsed)
{
if (strlen($parsed['query'])) {
return '?' . $parsed['query'];
$fragment = '';

if (strlen($parsed['fragment'])) {
$fragment = '#' . $parsed['fragment'];
}

return '';
return $fragment;
}
}
9 changes: 7 additions & 2 deletions tests/Utilities/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ public function it_can_unparse_url()
$urls = [
'',
'http://www.example.com/',
'http://username:[email protected]/',
'http://example.com:80/',
'http://demo.example.com:80/',
'http://username:[email protected]/',
'http://name:[email protected]/',
'http://name:[email protected]/',
'http://name:[email protected]:80/',
'http://example.com/products',
'http://shop.example.com/products',
'http://example.com/products?sku=1234',
'http://name:[email protected]:80/products?sku=1234#price',
'http://shop.example.com/products?sku=1234',
'http://name:[email protected]:80/products?sku=1234#price',
];

foreach ($urls as $url) {
Expand Down

0 comments on commit f010f45

Please sign in to comment.