-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2893ec8
commit f97a39f
Showing
7 changed files
with
42 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<?php | ||
<?php | ||
/** | ||
* Library for urls manipulation. | ||
* | ||
* @author Josantonius - [email protected] | ||
* @copyright Copyright (c) 2017 JST PHP Framework | ||
* @copyright Copyright (c) 2017 | ||
* @license https://opensource.org/licenses/MIT - The MIT License (MIT) | ||
* @link https://github.com/Josantonius/PHP-Url | ||
* @since 1.0.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,32 +4,21 @@ | |
* | ||
* @author Josantonius - [email protected] | ||
* @author David Carr - [email protected] | ||
* @copyright Copyright (c) 2017 JST PHP Framework | ||
* @copyright Copyright (c) 2017 | ||
* @license https://opensource.org/licenses/MIT - The MIT License (MIT) | ||
* @link https://github.com/Josantonius/PHP-Url | ||
* @since 1.0.0 | ||
*/ | ||
|
||
namespace Josantonius\Url; | ||
|
||
# use Josantonius\Url\Exception\UrlException; | ||
|
||
/** | ||
* Url handler. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
class Url { | ||
|
||
/** | ||
* Directory separator. | ||
* | ||
* @since 1.1.2 | ||
* | ||
* @var string | ||
*/ | ||
const DS = DIRECTORY_SEPARATOR; | ||
|
||
/** | ||
* Get url from the current page. | ||
* | ||
|
@@ -49,7 +38,7 @@ public static function getCurrentPage() { | |
|
||
$uri = self::getUri(); | ||
|
||
return $protocol . ':' . self::DS . self::DS . $host . $port . $uri; | ||
return $protocol . '://' . $host . $port . $uri; | ||
} | ||
|
||
/** | ||
|
@@ -65,9 +54,9 @@ public static function getBaseUrl() { | |
|
||
$url = self::addBackslash(self::getCurrentPage()); | ||
|
||
if ($uri !== self::DS) { | ||
if ($uri !== '/') { | ||
|
||
$url = trim(str_replace($uri, '', $url), self::DS); | ||
$url = trim(str_replace($uri, '', $url), '/'); | ||
} | ||
|
||
return self::addBackslash($url); | ||
|
@@ -78,13 +67,20 @@ public static function getBaseUrl() { | |
* | ||
* @since 1.0.0 | ||
* | ||
* @param string $url | ||
* | ||
* @return string → http|https | ||
*/ | ||
public static function getProtocol() { | ||
public static function getProtocol($url = false) { | ||
|
||
if ($url) { | ||
|
||
return (preg_match('/^https/', $url)) ? 'https' : 'http'; | ||
} | ||
|
||
$protocol = strtolower($_SERVER['SERVER_PROTOCOL']); | ||
|
||
$protocol = substr($protocol, 0, strpos($protocol, self::DS)); | ||
$protocol = substr($protocol, 0, strpos($protocol, '/')); | ||
|
||
$ssl = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'); | ||
|
||
|
@@ -96,21 +92,32 @@ public static function getProtocol() { | |
* | ||
* @since 1.0.0 | ||
* | ||
* @param string $url | ||
* | ||
* @return boolean | ||
*/ | ||
public static function isSSL() { | ||
public static function isSSL($url = false) { | ||
|
||
return (self::getProtocol() === 'https'); | ||
return (self::getProtocol($url) === 'https'); | ||
} | ||
|
||
/** | ||
* Get the server name. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @return string → server name | ||
* @param string $url | ||
* | ||
* @return string|false → server name | ||
*/ | ||
public static function getDomain() { | ||
public static function getDomain($url = false) { | ||
|
||
if ($url) { | ||
|
||
preg_match('/([\w]+[.]){1,}[a-z]+/', $url, $matches); | ||
|
||
return isset($matches[0]) ? $matches[0] : false; | ||
} | ||
|
||
return $_SERVER['SERVER_NAME']; | ||
} | ||
|
@@ -138,9 +145,9 @@ public static function getUriMethods() { | |
|
||
$root = str_replace($_SERVER["DOCUMENT_ROOT"], '', getcwd()); | ||
|
||
$subfolder = trim($root, self::DS); | ||
$subfolder = trim($root, '/'); | ||
|
||
return trim(str_replace($subfolder, '', self::getUri()), self::DS); | ||
return trim(str_replace($subfolder, '', self::getUri()), '/'); | ||
} | ||
|
||
/** | ||
|
@@ -171,11 +178,11 @@ public static function addBackslash($uri, $position = 'end') { | |
|
||
case 'top': | ||
|
||
return (substr($uri, 1) === self::DS) ? $uri : self::DS.$uri; | ||
return (substr($uri, 1) === '/') ? $uri : '/' . $uri; | ||
|
||
case 'end': | ||
|
||
return (substr($uri, -1) === self::DS) ? $uri : $uri.self::DS; | ||
return (substr($uri, -1) === '/') ? $uri : $uri . '/'; | ||
|
||
case 'both': | ||
|
||
|
@@ -240,7 +247,6 @@ public static function autoLink($url, $custom = null) { | |
/** | ||
* This function converts and url segment to an safe one. | ||
* For example: `test name @132` will be converted to `test-name--123`. | ||
* Replace every character that isn't an letter or an number to an dash sign. | ||
* It will also return all letters in lowercase | ||
* | ||
* @since 1.0.0 | ||
|
@@ -272,7 +278,7 @@ public static function segment($uri = null) { | |
|
||
$uri = (!is_null($uri)) ? $uri : $_SERVER['REQUEST_URI']; | ||
|
||
return explode(self::DS, trim($uri, self::DS)); | ||
return explode('/', trim($uri, '/')); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
* Library for urls manipulation. | ||
* | ||
* @author Josantonius - [email protected] | ||
* @copyright Copyright (c) 2017 JST PHP Framework | ||
* @copyright Copyright (c) 2017 | ||
* @license https://opensource.org/licenses/MIT - The MIT License (MIT) | ||
* @link https://github.com/Josantonius/PHP-Url | ||
* @since 1.0.0 | ||
|