Skip to content

Commit a74cfc7

Browse files
committed
Improve Interfaces documentation
1 parent 3bc85b5 commit a74cfc7

File tree

5 files changed

+106
-58
lines changed

5 files changed

+106
-58
lines changed

docs/interfaces/7.0/encoder.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ title: URI component encoder and decoder
66
URI component encoder and decoder
77
=======
88

9-
In order to safely encode and/or decode a URI component, we need a tool to correctly perform the conversion.
10-
To do so the package provides an enhanced OOP wrapper around PHP's `rawurlencode` and `rawurldecode` functions
9+
To safely encode and/or decode a URI component, we need a tool to correctly perform the conversion.
10+
To do so, the package provides an enhanced OOP wrapper around PHP's `rawurlencode` and `rawurldecode` functions
1111
using the `League\Uri\Encoder` helper class.
1212

13-
The class provides encoding mechanism for the following URI components:
13+
The class provides an encoding mechanism for the following URI components:
1414

1515
```php
1616
<?php
@@ -26,7 +26,7 @@ echo Encoder::encodePath($component); // returns "/thi:s/is/a%3Fsimple=pa
2626
echo Encoder::encodeQueryOrFragment($query); // returns "simple%23=path&k%C3%A9?=23"
2727
````
2828

29-
The class also provides a more specific encodage used for query string key/value pair.
29+
The class also provides a more specific encoding mechanism used for query-string key/value pair.
3030

3131
```php
3232
<?php
@@ -37,7 +37,7 @@ echo Encoder::encodeQueryKeyValue($queryKey); // returns "k%C3%A9%23"
3737
echo Encoder::encodeQueryKeyValue($queryValue); // returns "%26foobar"
3838
````
3939

40-
Each static encoding method is component aware and will prevent encoding component special characters that must or
40+
Each static encoding method is component-aware and will prevent encoding component special characters that must or
4141
must not be encoded in the context of a full URI.
4242

4343
To complete the encoding methods, the class also exposes static decoding methods to safely decode URI components:
@@ -56,7 +56,8 @@ echo Encoder::decodePath($component); // returns "%2Fthi:s%2Fis%
5656
echo Encoder::decodeQuery($component); // returns "/thi:s/is/a?simple%23=%20path"
5757
echo Encoder::decodeFragment($component); // returns "/thi:s/is/a?simple#=%20path"
5858
````
59-
Each static decoding method is component aware and will prevent decoding component specific characters in the context
59+
60+
Each static decoding method is component-aware and will prevent decoding component-specific characters in the context
6061
of a full URI.
6162

6263
<p class="message-info">The <code>scheme</code> and <code>port</code> do not requires a specific class to be correctly

docs/interfaces/7.0/uri-parser-builder.md

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,10 @@ URI Parser and Builder
88

99
PHP has been relying on the `parse_url` function to split URI into its component. But the
1010
function 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
1212
user-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

2516
The 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

@@ -50,7 +57,7 @@ var_export(UriString::parse('http://[email protected]/#'));
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
5663
var_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+
7384
If 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
7586
a 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
87101
use 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
109138
UriString::build(array $components): string
110139
UriString::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

114143
You 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]/');
@@ -134,4 +163,42 @@ echo UriString::build($components); //displays http://hello:[email protected][email protected]
134163

135164
The `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+
~~~

interfaces/CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ All Notable changes to `League\Uri\Interfaces` will be documented in this file
2424
- `UriString::containsValidRfc3987Characters`
2525
- `UriString::isValidScheme`
2626
- `UriString::isValidHost`
27-
- `UriString::buildUserInfo`
2827
- `FeatureDetection::supportsDom`
2928
- `Encoder::decodeNecessary`
3029
- `Encoder::decodePath`
@@ -47,7 +46,7 @@ All Notable changes to `League\Uri\Interfaces` will be documented in this file
4746
### Fixed
4847

4948
- `UriString::parse` will fail if the URI contains whitespace.
50-
- `UriString::buildUri` allows the `$path` argument to be `null`.
49+
- `UriString::buildUri` allows all arguments to be `null` including the `path`.
5150
- `UriString::buildAuthority` allows the `$user` argument to be `null` and still generate a user info component.
5251

5352
### Deprecated

interfaces/Encoder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public static function decodeQuery(Stringable|string|null $path): ?string
287287
}
288288

289289
/**
290-
* Normalize query component.
290+
* Normalize the query component.
291291
*
292292
* The value returned MUST be percent-encoded, but MUST NOT double-encode
293293
* any characters. To determine what characters to encode, please refer to
@@ -317,7 +317,7 @@ public static function decodeFragment(Stringable|string|null $path): ?string
317317
}
318318

319319
/**
320-
* Normalize fragment component.
320+
* Normalize the fragment component.
321321
*
322322
* The value returned MUST be percent-encoded, but MUST NOT double-encode
323323
* any characters. To determine what characters to encode, please refer to
@@ -329,7 +329,7 @@ public static function normalizeFragment(Stringable|string|null $fragment): ?str
329329
}
330330

331331
/**
332-
* Normalize host component.
332+
* Normalize the host component.
333333
*
334334
* @see https://www.rfc-editor.org/rfc/rfc3986.html#section-3.2.2
335335
*

interfaces/UriString.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,20 @@ public static function build(array $components): string
218218
}
219219

220220
/**
221-
* Generate a URI string representation based on RFC3986 algorithm.
221+
* Generates a URI string representation based on RFC3986 algorithm.
222222
*
223-
* valid URI component MUST be provided without their URI delimiters
223+
* Valid URI component MUST be provided without their URI delimiters
224224
* but properly encoded.
225225
*
226226
* @link https://tools.ietf.org/html/rfc3986#section-5.3
227227
* @link https://tools.ietf.org/html/rfc3986#section-7.5§
228228
*/
229229
public static function buildUri(
230-
?string $scheme,
231-
?string $authority,
232-
?string $path,
233-
?string $query,
234-
?string $fragment,
230+
?string $scheme = null,
231+
?string $authority = null,
232+
?string $path = null,
233+
?string $query = null,
234+
?string $fragment = null,
235235
): string {
236236
self::validateComponents($scheme, $authority, $path);
237237
$uri = '';
@@ -284,25 +284,6 @@ public static function buildAuthority(array $components): ?string
284284
return $authority;
285285
}
286286

287-
/**
288-
* Generate a URI UserInfo representation from the URI parsed representation.
289-
*
290-
* @param InputComponentMap $components
291-
*/
292-
public static function buildUserInfo(array $components): ?string
293-
{
294-
if (!isset($components['user'])) {
295-
return null;
296-
}
297-
298-
$userInfo = $components['user'];
299-
if (! isset($components['pass'])) {
300-
return $userInfo;
301-
}
302-
303-
return $userInfo.':'.$components['pass'];
304-
}
305-
306287
/**
307288
* Parses and normalizes the URI following RFC3986 destructive and non-destructive constraints.
308289
*

0 commit comments

Comments
 (0)