From f010f450c6127b4a125445766a3579950753452b Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Sun, 13 Sep 2015 23:57:27 +0100 Subject: [PATCH] More refactoring and Testing --- src/Utilities/Url.php | 57 +++++++++++++++++++++---------------- tests/Utilities/UrlTest.php | 9 ++++-- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/Utilities/Url.php b/src/Utilities/Url.php index 04c3dfd..827670c 100644 --- a/src/Utilities/Url.php +++ b/src/Utilities/Url.php @@ -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); @@ -158,6 +154,8 @@ public static function unparse($parsed) | ------------------------------------------------------------------------------------------------ */ /** + * Check parsed URL. + * * @param array $parsed * * @return array @@ -180,7 +178,7 @@ private static function checkParsedUrl(array &$parsed) } /** - * Get Url. + * Get URL. * * @param array $parsed * @@ -188,13 +186,13 @@ private static function checkParsedUrl(array &$parsed) */ 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; } /** @@ -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; } /** @@ -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; } /** @@ -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; } } diff --git a/tests/Utilities/UrlTest.php b/tests/Utilities/UrlTest.php index 98078d7..6d97f30 100644 --- a/tests/Utilities/UrlTest.php +++ b/tests/Utilities/UrlTest.php @@ -35,12 +35,17 @@ public function it_can_unparse_url() $urls = [ '', 'http://www.example.com/', - 'http://username:password@example.com/', 'http://example.com:80/', + 'http://demo.example.com:80/', + 'http://username:password@example.com/', 'http://name:pass@example.com/', + 'http://name:passd@admin.example.com/', + 'http://name:passd@admin.example.com:80/', 'http://example.com/products', + 'http://shop.example.com/products', 'http://example.com/products?sku=1234', - 'http://name:pass@example.com:80/products?sku=1234#price', + 'http://shop.example.com/products?sku=1234', + 'http://name:pass@shop.example.com:80/products?sku=1234#price', ]; foreach ($urls as $url) {