diff --git a/CHANGELOG.md b/CHANGELOG.md index e4011e4..dd3bce0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG +## 1.1.0 - 2017-02-28 +* Added `Josantonius\Url\Url::getBaseUrl()` method. + +## 1.1.0 - 2017-02-28 +* Added `Josantonius\Url\Tests\UrlTest::testGetBaseUrl()` method. + ## 1.0.0 - 2017-02-02 * Added `Josantonius\Url\Url` class. * Added `Josantonius\Url\Url::getCurrentPage()` method. diff --git a/src/Exception/UrlException.php b/src/Exception/UrlException.php index c4bed24..edd5233 100644 --- a/src/Exception/UrlException.php +++ b/src/Exception/UrlException.php @@ -6,7 +6,7 @@ * @copyright Copyright (c) 2017 JST PHP Framework * @license https://opensource.org/licenses/MIT - The MIT License (MIT) * @link https://github.com/Josantonius/PHP-Url - * @since File available since 1.0.0 - Update: 2017-02-14 + * @since 1.0.0 */ namespace Josantonius\Url\Exception; @@ -37,4 +37,4 @@ public function __construct($msg = '', $error = 0, $status = 0) { $this->code = $error; $this->statusCode = $status; } -} \ No newline at end of file +} diff --git a/src/Url.php b/src/Url.php index ae3039a..a57e29f 100644 --- a/src/Url.php +++ b/src/Url.php @@ -7,7 +7,7 @@ * @copyright Copyright (c) 2017 JST PHP Framework * @license https://opensource.org/licenses/MIT - The MIT License (MIT) * @link https://github.com/Josantonius/PHP-Url - * @since File available since 1.0.0 - Update: 2017-02-14 + * @since 1.0.0 */ namespace Josantonius\Url; @@ -30,17 +30,33 @@ class Url { */ public static function getCurrentPage() { - $protocol = static::getProtocol(); + $protocol = static::getProtocol(); - $host = static::getDomain(); + $host = static::getDomain(); - $port = ':' . static::getPort(); + $port = ':' . static::getPort(); - $port = (($port == ':80') || ($port == ':443')) ? '' : $port; + $port = (($port == ':80') || ($port == ':443')) ? '' : $port; - $uri = static::getUri(); + $uri = static::getUri(); - return $protocol . '://' . $host . $port . $uri; + return $protocol . '://' . $host . $port . $uri; + } + + /** + * Get base url of the site. + * + * @since 1.1.0 + * + * @return string → url + */ + public static function getBaseUrl() { + + $uri = static::getUriMethods(); + + $url = trim(str_replace($uri, '', static::getCurrentPage()), '/'); + + return static::addBackslash($url); } /** @@ -52,13 +68,13 @@ public static function getCurrentPage() { */ public static function getProtocol() { - $protocol = strtolower($_SERVER['SERVER_PROTOCOL']); + $protocol = strtolower($_SERVER['SERVER_PROTOCOL']); - $protocol = substr($protocol, 0, strpos($protocol, '/')); + $protocol = substr($protocol, 0, strpos($protocol, '/')); - $ssl = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'); + $ssl = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'); - return ($ssl) ? $protocol . 's' : $protocol; + return ($ssl) ? $protocol . 's' : $protocol; } /** @@ -70,7 +86,7 @@ public static function getProtocol() { */ public static function isSSL() { - return (static::getProtocol() === 'https'); + return (static::getProtocol() === 'https'); } /** @@ -82,7 +98,7 @@ public static function isSSL() { */ public static function getDomain() { - return $_SERVER['SERVER_NAME']; + return $_SERVER['SERVER_NAME']; } /** @@ -120,7 +136,7 @@ public static function getUriMethods() { */ public static function getPort() { - return $_SERVER['SERVER_PORT']; + return $_SERVER['SERVER_PORT']; } /** @@ -221,7 +237,7 @@ public static function generateSafeSlug($slug) { */ public static function segment($uri = null) { - $uri = (!is_null($uri)) ? $uri : $_SERVER['REQUEST_URI']; + $uri = (!is_null($uri)) ? $uri : $_SERVER['REQUEST_URI']; return explode('/', trim($uri, '/')); } @@ -249,4 +265,4 @@ public static function getLastSegment($segments) { return end($segments); } -} \ No newline at end of file +} diff --git a/tests/UrlTest.php b/tests/UrlTest.php index e572c75..4fc67f1 100644 --- a/tests/UrlTest.php +++ b/tests/UrlTest.php @@ -1,4 +1,4 @@ -'; var_dump(Url::getCurrentPage()); echo ''; + echo '
'; var_dump(Url::getCurrentPage()); echo ''; + } + + /** + * Get base url of the site. + * + * @since 1.0.0 + */ + public static function testGetBaseUrl() { + + echo '
'; var_dump(Url::getBaseUrl()); echo ''; } /** @@ -37,7 +47,7 @@ public static function testGetCurrentPage() { */ public static function testGetProtocol() { - echo '
'; var_dump(Url::getProtocol()); echo ''; + echo '
'; var_dump(Url::getProtocol()); echo ''; } /** @@ -47,7 +57,7 @@ public static function testGetProtocol() { */ public static function testIsSSL() { - echo '
'; var_dump(Url::isSSL()); echo ''; + echo '
'; var_dump(Url::isSSL()); echo ''; } /** @@ -57,7 +67,7 @@ public static function testIsSSL() { */ public static function getDomain() { - echo '
'; var_dump(Url::testGetProtocol()); echo ''; + echo '
'; var_dump(Url::testGetProtocol()); echo ''; } /** @@ -67,7 +77,7 @@ public static function getDomain() { */ public static function testGetUri() { - echo '
'; var_dump(Url::getUri()); echo ''; + echo '
'; var_dump(Url::getUri()); echo ''; } /** @@ -87,7 +97,7 @@ public static function testGetUriMethods() { */ public static function testGetPort() { - echo '
'; var_dump(Url::getPort()); echo ''; + echo '
'; var_dump(Url::getPort()); echo ''; } /** @@ -97,7 +107,7 @@ public static function testGetPort() { */ public static function testAddBackslash() { - echo '
'; var_dump(Url::addBackslash('path/to')); echo ''; + echo '
'; var_dump(Url::addBackslash('path/to')); echo ''; } /** @@ -107,7 +117,7 @@ public static function testAddBackslash() { */ public static function testPrevious() { - Url::previous(); + Url::previous(); } /** @@ -172,7 +182,7 @@ public static function testSegment() { */ public static function testGetFirstSegment() { - $segments = Url::segment('path/to/panel/user'); + $segments = Url::segment('path/to/panel/user'); echo '
'; var_dump(Url::getFirstSegment($segments)); echo ''; } @@ -184,8 +194,8 @@ public static function testGetFirstSegment() { */ public static function testGetLastSegment() { - $segments = Url::segment('path/to/panel/user'); + $segments = Url::segment('path/to/panel/user'); echo '
'; var_dump(Url::getLastSegment($segments)); echo ''; } -} \ No newline at end of file +}