2017 © Pedro Peláez
 

library uri

URIs

image

xp-forge/uri

URIs

  • Monday, March 26, 2018
  • by thekid
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3,979 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 27 % Grown

The README.md

URI handling

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.4+ Supports PHP 8.0+ Latest Stable Version, (*1)

A Uniform Resource Identifier (URI) is a compact sequence of characters that identifies an abstract or physical resource., (*2)

  foo://example.com:8042/over/there?name=ferret#nose
  \_/   \______________/\_________/ \_________/ \__/
   |           |            |            |        |
scheme     authority       path        query   fragment
   |   _____________________|__
  / \ /                        \
  urn:example:animal:ferret:nose

See https://tools.ietf.org/html/rfc3986, (*3)

Examples

Parsing from a string

The most common case will include constructing URIs from a given input string., (*4)

use util\URI;

$uri= new URI('https://user:password@localhost:8443/index?sort=name#top');
$uri->isOpaque();     // false - it's a hierarchical URI
$uri->scheme();       // "https"
$uri->authority();    // util.Authority("localhost", 8443, "user", util.Secret("password"))
$uri->host();         // "localhost"
$uri->port();         // 8443
$uri->user();         // "user"
$uri->password();     // util.Secret("password")
$uri->path();         // "index"
$uri->query();        // "sort=name"
$uri->params();       // util.uri.Parameters("sort=name")
$uri->param('sort');  // "name"
$uri->fragment();     // "top"
$uri->resource();     // "/index?sort=name#top"

Creating or modifying

URI instances are immutable. However, a fluent interface is offered via with() and using(). Both return fresh instances., (*5)

use util\URI;

$uri= URI::with()->scheme('mailto')->path('timm@example.com')->param('Subject', 'Hello')->create();
$uri->isOpaque();   // true - it's an opaque URI
$uri->scheme();     // "mailto"
$uri->authority();  // null
(string)$uri;       // "mailto:timm@example.com?Subject=Hello"

$copy= $uri->using()->path('cc@example.com')->create();
(string)$copy;      // "mailto:cc@example.com?Subject=Hello"

Resolving URIs

Given http://localhost/home/ as the base URI, you can resolve links in its context using the resolve() method:, (*6)

use util\URI;

$uri= new URI('http://localhost/home/');
$uri->resolve('/index.html');       // util.URI<http://localhost/index.html>
$uri->resolve('index.html');        // util.URI<http://localhost/home/index.html>
$uri->resolve('?sort=name');        // util.URI<http://localhost/home/?sort=name>
$uri->resolve('#top');              // util.URI<http://localhost/home/#top>
$uri->resolve('//example.com');     // util.URI<http://example.com>
$uri->resolve('https://localhost'); // util.URI<https://localhost>

Filesystem

URIs can point to filesystem paths. Converting between the two is not trivial - you need to handle Windows UNC paths correctly. The URI class' file() and asPath() methods take care of this., (*7)

use util\URI;

$uri= URI::file('/etc/php.ini');
(string)$uri;       // "file:///etc/php.ini"

$uri= new URI('file://c:/Windows');
$uri->path();       // "C:/Windows"
$uri->asPath();     // io.Path("C:\Windows")

$uri= new URI('file://share/file.txt');
$uri->authority();  // util.Authority("share")
$uri->path();       // "/file.txt"
$uri->asPath();     // io.Path("\\share\file.txt")

The Versions

26/03 2018

dev-master

9999999-dev http://xp-framework.net/

URIs

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

26/03 2018

v1.2.2

1.2.2.0 http://xp-framework.net/

URIs

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

16/02 2018

v1.2.1

1.2.1.0 http://xp-framework.net/

URIs

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

21/01 2018

v1.2.0

1.2.0.0 http://xp-framework.net/

URIs

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

20/01 2018

v1.1.0

1.1.0.0 http://xp-framework.net/

URIs

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

04/06 2017

v1.0.0

1.0.0.0 http://xp-framework.net/

URIs

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

26/03 2017

v0.5.0

0.5.0.0 http://xp-framework.net/

URIs

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

26/03 2017

v0.4.0

0.4.0.0 http://xp-framework.net/

URIs

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

26/03 2017

v0.3.0

0.3.0.0 http://xp-framework.net/

URIs

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

26/03 2017

v0.2.0

0.2.0.0 http://xp-framework.net/

URIs

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

24/03 2017

v0.1.0

0.1.0.0 http://xp-framework.net/

URIs

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp