Skip to content

Commit

Permalink
Updated to 1.1.3 version
Browse files Browse the repository at this point in the history
  • Loading branch information
josantonius committed Jul 9, 2017
1 parent 2893ec8 commit f97a39f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 32 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 1.1.3 - 2017-07-09
* Added option to analyze concrete urls in some methods.

## 1.1.2 - 2017-05-08
* Now you can choose to place backslash at the beginning, end or both ends in the addBackslash() method.

Expand Down
2 changes: 1 addition & 1 deletion README-ES.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ También puedes clonar el repositorio completo con Git:

### Requisitos

Esta ĺibrería es soportada por versiones de PHP 5.6 o superiores y es compatible con versiones de HHVM 3.0 o superiores.
Esta biblioteca es soportada por versiones de PHP 5.6 o superiores y es compatible con versiones de HHVM 3.0 o superiores.

### Cómo empezar y ejemplos

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require __DIR__ . '/vendor/autoload.php';

use Josantonius\Url\Url;
```

### Available Methods

Available methods in this library:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "josantonius/url",
"version": "1.1.2",
"version": "1.1.3",
"type": "library",
"description": "Library for urls manipulation.",
"keywords": [
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/UrlException.php
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
Expand Down
60 changes: 33 additions & 27 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}

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

Expand All @@ -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'];
}
Expand Down Expand Up @@ -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()), '/');
}

/**
Expand Down Expand Up @@ -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':

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, '/'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f97a39f

Please sign in to comment.