Skip to content

Commit

Permalink
New method added to get the base url
Browse files Browse the repository at this point in the history
  • Loading branch information
josantonius committed Feb 28, 2017
1 parent 7c0a70e commit b1bd1a7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 31 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/UrlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,4 +37,4 @@ public function __construct($msg = '', $error = 0, $status = 0) {
$this->code = $error;
$this->statusCode = $status;
}
}
}
48 changes: 32 additions & 16 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -70,7 +86,7 @@ public static function getProtocol() {
*/
public static function isSSL() {

return (static::getProtocol() === 'https');
return (static::getProtocol() === 'https');
}

/**
Expand All @@ -82,7 +98,7 @@ public static function isSSL() {
*/
public static function getDomain() {

return $_SERVER['SERVER_NAME'];
return $_SERVER['SERVER_NAME'];
}

/**
Expand Down Expand Up @@ -120,7 +136,7 @@ public static function getUriMethods() {
*/
public static function getPort() {

return $_SERVER['SERVER_PORT'];
return $_SERVER['SERVER_PORT'];
}

/**
Expand Down Expand Up @@ -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, '/'));
}
Expand Down Expand Up @@ -249,4 +265,4 @@ public static function getLastSegment($segments) {

return end($segments);
}
}
}
36 changes: 23 additions & 13 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
<?php
/**
* Library for urls manipulation.
*
* @author Josantonius - [email protected]
* @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\Tests;
Expand All @@ -27,7 +27,17 @@ class UrlTest {
*/
public static function testGetCurrentPage() {

echo '<pre>'; var_dump(Url::getCurrentPage()); echo '</pre>';
echo '<pre>'; var_dump(Url::getCurrentPage()); echo '</pre>';
}

/**
* Get base url of the site.
*
* @since 1.0.0
*/
public static function testGetBaseUrl() {

echo '<pre>'; var_dump(Url::getBaseUrl()); echo '</pre>';
}

/**
Expand All @@ -37,7 +47,7 @@ public static function testGetCurrentPage() {
*/
public static function testGetProtocol() {

echo '<pre>'; var_dump(Url::getProtocol()); echo '</pre>';
echo '<pre>'; var_dump(Url::getProtocol()); echo '</pre>';
}

/**
Expand All @@ -47,7 +57,7 @@ public static function testGetProtocol() {
*/
public static function testIsSSL() {

echo '<pre>'; var_dump(Url::isSSL()); echo '</pre>';
echo '<pre>'; var_dump(Url::isSSL()); echo '</pre>';
}

/**
Expand All @@ -57,7 +67,7 @@ public static function testIsSSL() {
*/
public static function getDomain() {

echo '<pre>'; var_dump(Url::testGetProtocol()); echo '</pre>';
echo '<pre>'; var_dump(Url::testGetProtocol()); echo '</pre>';
}

/**
Expand All @@ -67,7 +77,7 @@ public static function getDomain() {
*/
public static function testGetUri() {

echo '<pre>'; var_dump(Url::getUri()); echo '</pre>';
echo '<pre>'; var_dump(Url::getUri()); echo '</pre>';
}

/**
Expand All @@ -87,7 +97,7 @@ public static function testGetUriMethods() {
*/
public static function testGetPort() {

echo '<pre>'; var_dump(Url::getPort()); echo '</pre>';
echo '<pre>'; var_dump(Url::getPort()); echo '</pre>';
}

/**
Expand All @@ -97,7 +107,7 @@ public static function testGetPort() {
*/
public static function testAddBackslash() {

echo '<pre>'; var_dump(Url::addBackslash('path/to')); echo '</pre>';
echo '<pre>'; var_dump(Url::addBackslash('path/to')); echo '</pre>';
}

/**
Expand All @@ -107,7 +117,7 @@ public static function testAddBackslash() {
*/
public static function testPrevious() {

Url::previous();
Url::previous();
}

/**
Expand Down Expand Up @@ -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 '<pre>'; var_dump(Url::getFirstSegment($segments)); echo '</pre>';
}
Expand All @@ -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 '<pre>'; var_dump(Url::getLastSegment($segments)); echo '</pre>';
}
}
}

0 comments on commit b1bd1a7

Please sign in to comment.