Skip to content

Commit

Permalink
Updating the Url Class
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Sep 13, 2015
1 parent 4f6b842 commit 1908da2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
44 changes: 31 additions & 13 deletions src/Utilities/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,35 +132,53 @@ public static function substituteAttributes(array $attributes, $uri)
/**
* Build URL using array data from parse_url.
*
* @param array|false $parsed - Array of data from parse_url function
* @param array|false $parsed
*
* @return string
*/
public static function unparse($parsed)
{
$url = '';

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

$parsed = self::checkParsedUrl($parsed);

$userInfo = ! strlen($parsed['pass']) ? $parsed['pass'] : $parsed['user'] . ':' . $parsed['pass'];
$host = ! (string) $parsed['port'] ? $parsed['host'] : $parsed['host'] . ':' . $parsed['port'];
$authority = ! strlen($userInfo) ? $host : $userInfo . '@' . $host;
$hierPart = ! strlen($authority) ? $parsed['path'] : '//' . $authority . $parsed['path'];
$url = ! strlen($parsed['scheme']) ? $hierPart : $parsed['scheme'] . ':' . $hierPart;
$url = ! strlen($parsed['query']) ? $url : $url . '?' . $parsed['query'];
$url = ! strlen($parsed['fragment']) ? $url : $url . '#' . $parsed['fragment'];

return $url;
}

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
*
* @param array $parsed
*
* @return array
*/
private static function checkParsedUrl($parsed)
{
$scheme =& $parsed['scheme'];
$host =& $parsed['host'];
$port =& $parsed['port'];
$user =& $parsed['user'];
$pass =& $parsed['pass'];
$path =& $parsed['path'];
$path = '/' . ltrim($path, '/');
$query =& $parsed['query'];
$fragment =& $parsed['fragment'];

$path = '/' . ltrim($path, '/');

$userInfo = ! strlen($pass) ? $user : "$user:$pass";
$host = ! "$port" ? $host : "$host:$port";
$authority = ! strlen($userInfo) ? $host : "$userInfo@$host";
$hierPart = ! strlen($authority) ? $path : "//$authority$path";
$url = ! strlen($scheme) ? $hierPart : "$scheme:$hierPart";
$url = ! strlen($query) ? $url : "$url?$query";
$url = ! strlen($fragment) ? $url : "$url#$fragment";

return $url;
return compact('scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment');
}
}
7 changes: 6 additions & 1 deletion tests/Utilities/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ public function it_can_unparse_url()
$urls = [
'',
'http://www.example.com/',
'http://username:[email protected]/'
'http://username:[email protected]/',
'http://example.com:80/',
'http://name:[email protected]/',
'http://example.com/products',
'http://example.com/products?sku=1234',
'http://name:[email protected]:80/products?sku=1234#price',
];

foreach ($urls as $url) {
Expand Down

0 comments on commit 1908da2

Please sign in to comment.