Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
adding functions for usage ease
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Aug 8, 2017
1 parent af8e1ba commit e349818
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 5 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

All Notable changes to `league-uri-parser` will be documented in this file

## 1.1.0 - TBD

### Added

- `League\Uri\build` function de build and URI from the result from `League\Uri\Parser::__invoke` or `parse_url`
- `League\Uri\parse` function version of `League\Uri\Parser::__invoke`
- `League\Uri\is_host` function version of `League\Uri\Parser::isHost`

### Fixed

- None

### Deprecated

- None

### Removed

- None

## 1.0.5 - 2017-04-19

### Added
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"autoload": {
"psr-4": {
"League\\Uri\\": "src"
}
},
"files": ["src/functions_include.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory suffix="include.php">src</directory>
</exclude>
</whitelist>
</filter>

Expand Down
109 changes: 109 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* League.Uri (http://uri.thephpleague.com)
*
* @package League\Uri
* @subpackage League\Uri
* @author Ignace Nyamagana Butera <[email protected]>
* @license https://github.com/thephpleague/uri-parser/blob/master/LICENSE (MIT License)
* @version 1.1.0
* @link https://github.com/thephpleague/uri-parser/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);

namespace League\Uri;

/**
* Returns whether the URI host component is valid according to RFC3986.
*
* @see https://tools.ietf.org/html/rfc3986#section-3.2.2
* @see Parser::isHost()
*
* @param string $host
*
* @return bool
*/
function is_host(string $host): bool
{
static $parser;

$parser = $parser ?? new Parser();

return $parser->isHost($host);
}

/**
* Parse an URI string into its components.
*
* This method parses a URL and returns an associative array containing any
* of the various components of the URL that are present.
*
* @see https://tools.ietf.org/html/rfc3986
* @see https://tools.ietf.org/html/rfc3986#section-2
* @see Parser::__invoke()
*
* @param string $uri
*
* @throws Exception if the URI contains invalid characters
*
* @return array
*/
function parse(string $uri): array
{
static $parser;

$parser = $parser ?? new Parser();

return $parser($uri);
}

/**
* Generate the URI string representation from its hash representation
* returned by Parser::__invoke() or PHP's parse_url
*
* If you supply your own hash you are responsible for providing
* valid components without their URI delimiters.
*
* For security reason the pass component has been deprecated
* as per RFC3986 and is never returned in the URI string
*
* @see https://tools.ietf.org/html/rfc3986#section-5.3
* @see https://tools.ietf.org/html/rfc3986#section-7.5
*
* @param array $components
*
* @return string
*/
function build(array $components): string
{
$uri = $components['path'] ?? '';
if (isset($components['query'])) {
$uri .= '?'.$components['query'];
}

if (isset($components['fragment'])) {
$uri .= '#'.$components['fragment'];
}

if (isset($components['host'])) {
$authority = $components['host'];
if (isset($components['port'])) {
$authority .= ':'.$components['port'];
}

if (isset($components['user'])) {
$authority = $components['user'].'@'.$authority;
}

$uri = '//'.$authority.$uri;
}

if (isset($components['scheme'])) {
return $components['scheme'].':'.$uri;
}

return $uri;
}
6 changes: 6 additions & 0 deletions src/functions_include.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Don't redefine the functions if included multiple times.
if (!function_exists('League\Uri\parse')) {
require __DIR__.'/functions.php';
}
Loading

0 comments on commit e349818

Please sign in to comment.