44 *
55 * @author Josantonius - hello@josantonius.com
66 * @author David Carr - dave@simplemvcframework.com
7- * @copyright Copyright (c) 2017 JST PHP Framework
7+ * @copyright Copyright (c) 2017
88 * @license https://opensource.org/licenses/MIT - The MIT License (MIT)
99 * @link https://github.com/Josantonius/PHP-Url
1010 * @since 1.0.0
1111 */
1212
1313namespace Josantonius \Url ;
1414
15- # use Josantonius\Url\Exception\UrlException;
16-
1715/**
1816 * Url handler.
1917 *
2018 * @since 1.0.0
2119 */
2220class Url {
2321
24- /**
25- * Directory separator.
26- *
27- * @since 1.1.2
28- *
29- * @var string
30- */
31- const DS = DIRECTORY_SEPARATOR ;
32-
3322 /**
3423 * Get url from the current page.
3524 *
@@ -49,7 +38,7 @@ public static function getCurrentPage() {
4938
5039 $ uri = self ::getUri ();
5140
52- return $ protocol . ': ' . self :: DS . self :: DS . $ host . $ port . $ uri ;
41+ return $ protocol . ':// ' . $ host . $ port . $ uri ;
5342 }
5443
5544 /**
@@ -65,9 +54,9 @@ public static function getBaseUrl() {
6554
6655 $ url = self ::addBackslash (self ::getCurrentPage ());
6756
68- if ($ uri !== self :: DS ) {
57+ if ($ uri !== ' / ' ) {
6958
70- $ url = trim (str_replace ($ uri , '' , $ url ), self :: DS );
59+ $ url = trim (str_replace ($ uri , '' , $ url ), ' / ' );
7160 }
7261
7362 return self ::addBackslash ($ url );
@@ -78,13 +67,20 @@ public static function getBaseUrl() {
7867 *
7968 * @since 1.0.0
8069 *
70+ * @param string $url
71+ *
8172 * @return string → http|https
8273 */
83- public static function getProtocol () {
74+ public static function getProtocol ($ url = false ) {
75+
76+ if ($ url ) {
77+
78+ return (preg_match ('/^https/ ' , $ url )) ? 'https ' : 'http ' ;
79+ }
8480
8581 $ protocol = strtolower ($ _SERVER ['SERVER_PROTOCOL ' ]);
8682
87- $ protocol = substr ($ protocol , 0 , strpos ($ protocol , self :: DS ));
83+ $ protocol = substr ($ protocol , 0 , strpos ($ protocol , ' / ' ));
8884
8985 $ ssl = (!empty ($ _SERVER ['HTTPS ' ]) && $ _SERVER ['HTTPS ' ] == 'on ' );
9086
@@ -96,21 +92,32 @@ public static function getProtocol() {
9692 *
9793 * @since 1.0.0
9894 *
95+ * @param string $url
96+ *
9997 * @return boolean
10098 */
101- public static function isSSL () {
99+ public static function isSSL ($ url = false ) {
102100
103- return (self ::getProtocol () === 'https ' );
101+ return (self ::getProtocol ($ url ) === 'https ' );
104102 }
105103
106104 /**
107105 * Get the server name.
108106 *
109107 * @since 1.0.0
110108 *
111- * @return string → server name
109+ * @param string $url
110+ *
111+ * @return string|false → server name
112112 */
113- public static function getDomain () {
113+ public static function getDomain ($ url = false ) {
114+
115+ if ($ url ) {
116+
117+ preg_match ('/([\w]+[.]){1,}[a-z]+/ ' , $ url , $ matches );
118+
119+ return isset ($ matches [0 ]) ? $ matches [0 ] : false ;
120+ }
114121
115122 return $ _SERVER ['SERVER_NAME ' ];
116123 }
@@ -138,9 +145,9 @@ public static function getUriMethods() {
138145
139146 $ root = str_replace ($ _SERVER ["DOCUMENT_ROOT " ], '' , getcwd ());
140147
141- $ subfolder = trim ($ root , self :: DS );
148+ $ subfolder = trim ($ root , ' / ' );
142149
143- return trim (str_replace ($ subfolder , '' , self ::getUri ()), self :: DS );
150+ return trim (str_replace ($ subfolder , '' , self ::getUri ()), ' / ' );
144151 }
145152
146153 /**
@@ -171,11 +178,11 @@ public static function addBackslash($uri, $position = 'end') {
171178
172179 case 'top ' :
173180
174- return (substr ($ uri , 1 ) === self :: DS ) ? $ uri : self :: DS . $ uri ;
181+ return (substr ($ uri , 1 ) === ' / ' ) ? $ uri : ' / ' . $ uri ;
175182
176183 case 'end ' :
177184
178- return (substr ($ uri , -1 ) === self :: DS ) ? $ uri : $ uri. self :: DS ;
185+ return (substr ($ uri , -1 ) === ' / ' ) ? $ uri : $ uri . ' / ' ;
179186
180187 case 'both ' :
181188
@@ -240,7 +247,6 @@ public static function autoLink($url, $custom = null) {
240247 /**
241248 * This function converts and url segment to an safe one.
242249 * For example: `test name @132` will be converted to `test-name--123`.
243- * Replace every character that isn't an letter or an number to an dash sign.
244250 * It will also return all letters in lowercase
245251 *
246252 * @since 1.0.0
@@ -272,7 +278,7 @@ public static function segment($uri = null) {
272278
273279 $ uri = (!is_null ($ uri )) ? $ uri : $ _SERVER ['REQUEST_URI ' ];
274280
275- return explode (self :: DS , trim ($ uri , self :: DS ));
281+ return explode (' / ' , trim ($ uri , ' / ' ));
276282 }
277283
278284 /**
0 commit comments