Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit d42a270

Browse files
committed
[Docs] Added base documentation
1 parent 4d7fd60 commit d42a270

File tree

1 file changed

+270
-0
lines changed

1 file changed

+270
-0
lines changed

doc/book/zend.uri.md

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
# Zend\\Uri
2+
3+
## Overview
4+
5+
`Zend\Uri` is a component that aids in manipulating and validating [Uniform
6+
Resource Identifiers](http://www.w3.org/Addressing/)
7+
([URIs](http://www.ietf.org/rfc/rfc3986.txt)). `Zend\Uri` exists primarily
8+
to service other components, such as `Zend\Http`, but is also useful as a
9+
standalone utility.
10+
11+
*URI*s always begin with a scheme, followed by a colon. The construction of the
12+
many different schemes varies significantly. The `Zend\Uri` component provides
13+
the `Zend\Uri\UriFactory` that returns a class implementing the
14+
`Zend\Uri\UriInterface` which specializes in the scheme if such a class is
15+
registered with the Factory.
16+
17+
## Creating a New URI
18+
19+
`Zend\Uri\UriFactory` will build a new URI from scratch if only a scheme is
20+
passed to `Zend\Uri\UriFactory::factory()`.
21+
22+
### Creating a New URI with ZendUriUriFactory::factory()
23+
24+
```php
25+
// To create a new URI from scratch, pass only the scheme
26+
// followed by a colon.
27+
$uri = Zend\Uri\UriFactory::factory('http:');
28+
29+
// $uri instanceof Zend\Uri\UriInterface
30+
```
31+
32+
To create a new *URI* from scratch, pass only the scheme followed by a colon to
33+
`Zend\Uri\UriFactory::factory()`. If an unsupported scheme is passed and no
34+
scheme-specific class is specified, a
35+
`Zend\Uri\Exception\InvalidArgumentException` will be thrown.
36+
37+
If the scheme or *URI* passed is supported, `Zend\Uri\UriFactory::factory()`
38+
will return a class implementing `Zend\Uri\UriInterface` that specializes in the
39+
scheme to be created.
40+
41+
> ### Note
42+
At the time of writing, `Zend\Uri` provides built-in support for the following
43+
schemes: HTTP, HTTPS, MAILTO and FILE
44+
45+
### Creating a New Custom-Class URI
46+
47+
You can specify a custom class to be used when using the `Zend\Uri\UriFactory`
48+
by registering your class with the Factory using
49+
`Zend\Uri\UriFactory::registerScheme()` which takes the scheme as first
50+
parameter. This enables you to create your own *URI*-class and instantiate new
51+
*URI* objects based on your own custom classes.
52+
53+
The 2nd parameter passed to `Zend\Uri\UriFactory::registerScheme()` must be a
54+
string with the name of a class implementing `Zend\Uri\UriInterface`. The class
55+
must either be already loaded, or be loadable by the autoloader.
56+
57+
#### Creating a URI using a custom class
58+
59+
```php
60+
// Create a new 'ftp' URI based on a custom class
61+
use Zend\Uri\UriFactory
62+
63+
UriFactory::registerScheme('ftp', 'MyNamespace\MyClass');
64+
65+
$ftpUri = UriFactory::factory(
66+
'ftp://[email protected]/path/file'
67+
);
68+
69+
// $ftpUri is an instance of MyLibrary\MyClass, which implements
70+
// Zend\Uri\UriInterface
71+
```
72+
73+
## Manipulating an Existing URI
74+
75+
To manipulate an existing *URI*, pass the entire *URI* as string to
76+
`Zend\Uri\UriFactory::factory()`.
77+
78+
### Manipulating an Existing URI with Zend\\Uri\\UriFactory::factory()
79+
80+
```php
81+
// To manipulate an existing URI, pass it in.
82+
$uri = Zend\Uri\UriFactory::factory('http://www.zend.com');
83+
84+
// $uri instanceof Zend\Uri\UriInterface
85+
```
86+
87+
The *URI* will be parsed and validated. If it is found to be invalid, a
88+
`Zend\Uri\Exception\InvalidArgumentException` will be thrown immediately.
89+
Otherwise, `Zend\Uri\UriFactory::factory()` will return a class implementing
90+
`Zend\Uri\UriInterface` that specializes in the scheme to be manipulated.
91+
92+
## Common Instance Methods
93+
94+
The `Zend\Uri\UriInterface` defines several instance methods that are useful for
95+
working with any kind of *URI*.
96+
97+
### Getting the Scheme of the URI
98+
99+
The scheme of the *URI* is the part of the *URI* that precedes the colon. For
100+
example, the scheme of `http://[email protected]/my/path?query#token` is
101+
'http'.
102+
103+
#### Getting the Scheme from a Zend\\Uri\\UriInterface Object
104+
105+
```php
106+
$uri = Zend\Uri\UriFactory::factory('mailto:[email protected]');
107+
108+
$scheme = $uri->getScheme(); // "mailto"
109+
```
110+
111+
The `getScheme()` instance method returns only the scheme part of the *URI*
112+
object.
113+
114+
### Getting the Userinfo of the URI
115+
116+
The userinfo of the *URI* is the optional part of the *URI* that follows the
117+
colon and comes before the host-part. For example, the userinfo of
118+
`http://[email protected]/my/path?query#token` is 'johndoe'.
119+
120+
#### Getting the Username from a Zend\Uri\UriInterface Object
121+
122+
```php
123+
$uri = Zend\Uri\UriFactory::factory('mailto:[email protected]');
124+
125+
$scheme = $uri->getUserinfo(); // "john.doe"
126+
```
127+
128+
The `getUserinfo()` method returns only the userinfo part of the *URI* object.
129+
130+
### Getting the host of the URI
131+
132+
The host of the *URI* is the optional part of the *URI* that follows the
133+
user-part and comes before the path-part. For example, the host of
134+
`http://[email protected]/my/path?query#token` is 'example.com'.
135+
136+
#### Getting the host from a Zend\\Uri\\UriInterface Object
137+
138+
```php
139+
$uri = Zend\Uri\UriFactory::factory('mailto:[email protected]');
140+
141+
$scheme = $uri->getHost(); // "example.com"
142+
```
143+
144+
The `getHost()` method returns only the host part of the *URI* object.
145+
146+
### Getting the port of the URI
147+
148+
The port of the *URI* is the optional part of the *URI* that follows the
149+
host-part and comes before the path-part. For example, the host of
150+
`http://[email protected]:80/my/path?query#token` is '80'. The *URI*-class can
151+
define default-ports that can be returned when no port is given in the *URI*.
152+
153+
#### Getting the port from a Zend\\Uri\\UriInterface Object
154+
155+
```php
156+
$uri = Zend\Uri\UriFactory::factory('http://example.com:8080');
157+
158+
$scheme = $uri->getPort(); // "8080"
159+
```
160+
161+
#### Getting a default port from a Zend\\Uri\\UriInterface Object
162+
163+
```php
164+
$uri = Zend\Uri\UriFactory::factory('http://example.com');
165+
166+
$scheme = $uri->getPort(); // "80"
167+
```
168+
169+
The `getHost()` method returns only the port part of the *URI* object.
170+
171+
### Getting the path of the URI
172+
173+
The path of the *URI* is the mandatory part of the *URI* that follows the port
174+
and comes before the query-part. For example, the path of
175+
`http://[email protected]:80/my/path?query#token` is '/my/path'.
176+
177+
#### Getting the path from a Zend\\Uri\\UriInterface Object
178+
179+
```php
180+
$uri = Zend\Uri\UriFactory::factory('http://example.com:80/my/path?a=b&c=d#token');
181+
182+
$scheme = $uri->getPath(); // "/my/path"
183+
```
184+
185+
The `getPath()` method returns only the path of the *URI* object.
186+
187+
### Getting the query-part of the URI
188+
189+
The query-part of the *URI* is the optional part of the *URI* that follows the
190+
path and comes before the fragment. For example, the query of
191+
`http://[email protected]:80/my/path?query#token` is 'quer'.
192+
193+
#### Getting the query from a Zend\\Uri\\UriInterface Object
194+
195+
```php
196+
$uri = Zend\Uri\UriFactory::factory('http://example.com:80/my/path?a=b&c=d#token');
197+
198+
$scheme = $uri->getQuery(); // "a=b&c=d"
199+
```
200+
201+
The `getQuery()` method returns only the query-part of the *URI* object.
202+
203+
#### Getting the query as array from a Zend\\Uri\\UriInterface Object
204+
205+
```php
206+
$uri = Zend\Uri\UriFactory::factory('http://example.com:80/my/path?a=b&c=d#token');
207+
208+
$scheme = $uri->getQueryAsArray();
209+
// [
210+
// 'a' => 'b',
211+
// 'c' => 'd',
212+
// ]
213+
```
214+
215+
The query-part often contains key=value pairs and therefore can be split into an
216+
associative array. This array can be retrieved using `getQueryAsArray()`.
217+
218+
### Getting the fragment-part of the URI
219+
220+
The fragment-part of the *URI* is the optional part of the *URI* that follows
221+
the query. For example, the fragment of
222+
`http://[email protected]:80/my/path?query#token` is 'token'.
223+
224+
#### Getting the fragment from a Zend\\Uri\\UriInterface Object
225+
226+
```php
227+
$uri = Zend\Uri\UriFactory::factory('http://example.com:80/my/path?a=b&c=d#token');
228+
229+
$scheme = $uri->getFragment(); // "token"
230+
```
231+
232+
The `getFragment()` method returns only the fragment-part of the *URI* object.
233+
234+
### Getting the Entire URI
235+
236+
#### Getting the Entire URI from a Zend\\Uri\\UriInterface Object
237+
238+
```php
239+
$uri = Zend\Uri\UriFactory::factory('http://www.zend.com');
240+
241+
echo $uri->toString(); // "http://www.zend.com"
242+
243+
// Alternate method:
244+
echo (string) $uri; // "http://www.zend.com"
245+
```
246+
247+
The `toString()` method returns the string representation of the entire *URI*.
248+
249+
The `Zend\Uri\UriInterface` defines also a magic `__toString()` method that
250+
returns the string representation of the *URI* when the Object is cast to a
251+
string.
252+
253+
### Validating the URI
254+
255+
When using `Zend\Uri\UriFactory::factory()` the given *URI* will always be
256+
validated and a `Zend\Uri\Exception\InvalidArgumentException` will be thrown
257+
when the *URI* is invalid. However, after the `Zend\Uri\UriInterface` is
258+
instantiated for a new *URI* or an existing valid one, it is possible that the
259+
*URI* can later become invalid after it is manipulated.
260+
261+
#### Validating a ZendUri* Object
262+
263+
```php
264+
$uri = Zend\Uri\UriFactory::factory('http://www.zend.com');
265+
266+
$isValid = $uri->isValid(); // TRUE
267+
```
268+
269+
The `isValid()` instance method provides a means to check that the *URI* object
270+
is still valid.

0 commit comments

Comments
 (0)