@@ -8,19 +8,10 @@ URI Parser and Builder
88
99PHP has been relying on the ` parse_url ` function to split URI into its component. But the
1010function predates RFC3986 and as such does not fully comply to the specification. To work
11- around this limitation the tooklit provides the ` League\Uri\UriString ` class. It is a
11+ around the limitation the tooklit provides the ` League\Uri\UriString ` class. It is a
1212user-land PHP URI parser and builder compliant with [ RFC 3986] ( http://tools.ietf.org/html/rfc3986 ) and [ RFC 3987] ( http://tools.ietf.org/html/rfc3987 )
13- The class act as a drop-in replacement for PHP's ` parse_url ` feature.
14-
15- ## URI parsing
16-
17- ~~~ php
18- UriString::parse(string $uri): array
19- UriString::parseAuthority(string $autority): array
20- UriString::normalize(string $uri): string
21- UriString::normalizeAuthority(string $autority): string
22- UriString::resolve(string $uri, ?string $baseUri = null): string
23- ~~~
13+ The class act as a drop-in replacement for PHP's ` parse_url ` feature. And expand on
14+ the feature to provide RFC3986 features:
2415
2516The parser is:
2617
@@ -30,6 +21,22 @@ The parser is:
3021- makes a distinction between empty and undefined components;
3122- the parser throws a ` League\Uri\Contracts\UriException ` exception instead of returning ` false ` on error;
3223
24+ ## URI parsing
25+
26+ The class covers parsing URI in different context:
27+
28+ ~~~ php
29+ UriString::parse(Stringable|string $uri): array
30+ UriString::parseAuthority(Stringable|string $autority): array
31+ UriString::parseNormalized(Stringable|string $uri): array
32+ UriString::normalize(Stringable|string $uri): string
33+ UriString::normalizeAuthority(Stringable|string $autority): string
34+ UriString::resolve(Stringable|string $uri, Stringable|string|null $baseUri = null): string
35+ ~~~
36+
37+ The ` Uri::parse ` method is an RFC compliant replacement to ` parse_url ` . It returns the same array
38+ but the parsed data is fully compliant with the RFC specificaition.
39+
3340~~~ php
3441<?php
3542
5057~~~
5158
5259<p class =" message-warning " >Just like <code >parse_url</code >, the <code >League\Uri\UriString</code > only
53- parses and extracts from the URI its components. Validating against scheme specific rules is still a requirement.</p >
60+ parses and extracts from the URI its components. Validating against scheme- specific rules is still a requirement.</p >
5461
5562~~~ php
5663var_export(UriString::parse('http:www.example.com'));
@@ -70,18 +77,25 @@ var_export(UriString::parse('http:www.example.com'));
7077<p class =" message-warning " >This invalid HTTP URI is successfully parsed.</p >
7178<p class =" message-notice " >The class also exposes a <code >UriString::parseAuthority</code > you can use to parse an authority string.</p >
7279
80+ ### URI resolution
81+
82+ <p class =" message-notice " >Available since version <code >7.6</code ></p >
83+
7384If you need to resolve your URI in the context of a Base URI the ` resolve ` public static method will let you
74- do just that. The method expect either a full URI as its single parameter or a relative URI following by
85+ do just that. The method expects either a full URI as its single parameter or a relative URI followed by
7586a base URI which must be absolute, the URI will then be resolved using the base URI.
7687
7788``` php
7889$components = UriString::resolve("/foo", "https://example.com");
7990//returns "https://example.com/foo"
8091```
8192
82- It is possible to normalize a URI against the RFC3986 rules using the ` UriString::normalize ` method.
83- The method expects a string and will return the same array as ` UriString::parse ` but each component will
84- have been normalized.
93+ ### URI normalization
94+
95+ It is possible to normalize a URI against the RFC3986 rules using two (2) methods:
96+
97+ - ` UriString::normalize ` which returns the normalized string.
98+ - ` UriString::parseNormalized ` which returns the same output as ` Uri::parse ` but each component is normalized.
8599
86100``` php
87101use League\Uri\UriString;
@@ -99,21 +113,36 @@ $parsed = UriString::parse("https://EXAMPLE.COM/foo/../bar");
99113// 'fragment' => null,
100114//);
101115
102- $normalized = UriString::normalize("https://EXAMPLE.COM/foo/../bar");
116+ $normalized = UriString::parseNormalized("https://EXAMPLE.COM/foo/../bar");
117+ //returns the following array
118+ //array(
119+ // 'scheme' => 'https',
120+ // 'user' => null,
121+ // 'pass' => null,
122+ // 'host' => 'example.com',
123+ // 'port' => null,
124+ // 'path' => '/bar',
125+ // 'query' => null,
126+ // 'fragment' => null,
127+ //);
128+
129+ $normalizedUri = UriString::normalize("https://EXAMPLE.COM/foo/../bar");
103130//returns "https://example.com/bar"
104131```
105132
133+ <p class =" message-notice " >The class also exposes a <code >UriString::normalizeAuthority</code > method, you can use to normalize an authority string.</p >
134+
106135## URI Building
107136
108137~~~ php
109138UriString::build(array $components): string
110139UriString::buildAuthority(array $components): string
111- UriString::buildUri(?string $scheme, ?string $authority, string $path, ?string $query, ?string $fragment): string
140+ UriString::buildUri(?string $scheme = null , ?string $authority = null, ? string $path = null , ?string $query = null , ?string $fragment = null ): string
112141~~~
113142
114143You can rebuild a URI from its hash representation returned by the ` UriString::parse ` method or PHP's ` parse_url ` function using the ` UriString::build ` public static method.
115144
116- <p class =" message-notice " >If you supply your own hash you are responsible for providing valid encoded components without their URI delimiters.</p >
145+ <p class =" message-notice " >If you supply your own hash, you are responsible for providing valid encoded components without their URI delimiters.</p >
117146
118147~~~ php
119148$components = UriString::parse('http://hello:
[email protected] [email protected] /');
134163
135164The ` build ` method provides similar functionality to the ` http_build_url() ` function from v1.x of the [ ` pecl_http ` ] ( https://pecl.php.net/package/pecl_http ) PECL extension.
136165
137- <p class =" message-notice " >The class also exposes a <code >UriString::buildAuthority</code > you can use to build an authority from its hash representation.</p >
166+ <p class =" message-notice " >The class also exposes a <code >UriString::buildAuthority</code > method you can use to build an authority from its hash representation.</p >
167+ <p class =" message-notice " >The class also exposes a <code >UriString::buildUri</code > method which strictly follow the specification.</p >
168+
169+ ## Removing dot segments
170+
171+ <p class =" message-notice " >Available since version <code >7.6</code ></p >
172+
173+ To remove dot segments as per [ RFC3986] ( https://tools.ietf.org/html/rfc3986#section-6 ) , you need to explicitly call
174+ the ` UriString::removeDotSegments ` method. The method takes a single argument the path string and returns
175+ a new string which represents the path without dot segments.
176+
177+ ~~~ php
178+ <?php
179+
180+ use League\Uri\UriString;
181+
182+ $path = UriString::removeDotSegments('path/to/./the/../the/sky%7bfoo%7d');
183+ echo $path; //displays 'path/to/the/sky%7Bfoo%7D'
184+ ~~~
185+
186+ ## Component and String Validation
187+
188+ The class exposes static methods to validate that a string:
189+
190+ - represents a valid scheme with the ` isValidScheme() ` method
191+ - represents a valid host according to RFC3986/RFC3987 with the ` isValidHost() ` method
192+ - contains RFC3986 characters only with the ` containsValidRfc3986Characters() ` method
193+ - contains RFC3987 characters only with the ` containsValidRfc3987Characters() ` method
194+
195+ ~~~ php
196+ <?php
197+
198+ use League\Uri\Uri;use League\Uri\UriString;
199+
200+ UriString::isValidScheme('foo '); //returns false because of the trailing space
201+ UriString::isValidHost('333.333.333.1.333'); //returns true
202+ UriString::containsValidRfc3986Characters('http://bébé.be'); //returns false non-ascii character are not allowed
203+ UriString::containsValidRfc3987Characters('http://bébé.be'); //returns true
204+ ~~~
0 commit comments