fix php 5.6 in docker dev env (#1740)
[openemr.git] / vendor / zendframework / zend-uri / doc / book / usage.md
blobb9039893e4b9e6b7786ac1a3540371b110e8eb94
1 # Usage
3 ## Creating a New URI
5 `Zend\Uri\UriFactory` will build a new URI from scratch if only a scheme is
6 passed to `Zend\Uri\UriFactory::factory()`.
8 ### Creating a New URI with ZendUriUriFactory::factory()
10 ```php
11 // To create a new URI from scratch, pass only the scheme
12 // followed by a colon.
13 $uri = Zend\Uri\UriFactory::factory('http:');
15 // $uri instanceof Zend\Uri\UriInterface
16 ```
18 To create a new URI from scratch, pass only the scheme followed by a colon to
19 `Zend\Uri\UriFactory::factory()`. If an unsupported scheme is passed and no
20 scheme-specific class is specified, a
21 `Zend\Uri\Exception\InvalidArgumentException` will be thrown.
23 If the scheme or URI passed is supported, `Zend\Uri\UriFactory::factory()` will
24 return a class implementing `Zend\Uri\UriInterface` that specializes in the
25 scheme referenced.
27 > ### Supported schemes
29 > At the time of writing, zend-uri provides built-in support for the following
30 > schemes only: HTTP, HTTPS, MAILTO and FILE.
32 ### Creating a New Custom-Class URI
34 You can specify a custom class to be used when using the `Zend\Uri\UriFactory`
35 by registering your class with the `UriFactory` using
36 `Zend\Uri\UriFactory::registerScheme($scheme, $class)`.  This enables you to
37 create your own URI class and instantiate new URI objects based on your own
38 custom classes.
40 The 2nd parameter passed to `Zend\Uri\UriFactory::registerScheme()` must be a
41 string with the name of a class implementing `Zend\Uri\UriInterface`. The class
42 must either be already loaded, or be loadable by the autoloader.
44 #### Creating a URI using a custom class
46 The following registers the `ftp` scheme with a custom URI class:
48 ```php
49 use MyNamespace\MyClass;
50 use Zend\Uri\UriFactory
52 UriFactory::registerScheme('ftp', MyClass::class);
54 $ftpUri = UriFactory::factory(
55     'ftp://user@ftp.example.com/path/file'
58 // $ftpUri is an instance of MyLibrary\MyClass, which implements
59 // Zend\Uri\UriInterface
60 ```
62 ## Manipulating an Existing URI
64 To manipulate an existing URI, pass the entire URI as a string to
65 `Zend\Uri\UriFactory::factory()`, and then manipulate the instance returned.
67 ### Manipulating an Existing URI with Zend\\Uri\\UriFactory::factory()
69 ```php
70 use Zend\Uri\UriFactory;
72 // To manipulate an existing URI, pass it in.
73 $uri = UriFactory::factory('http://www.zend.com');
75 // $uri instanceof Zend\Uri\UriInterface
76 ```
78 The URI will be parsed and validated. If it is found to be invalid, a
79 `Zend\Uri\Exception\InvalidArgumentException` will be thrown immediately.
80 Otherwise, `Zend\Uri\UriFactory::factory()` will return a class implementing
81 `Zend\Uri\UriInterface` that specializes in the scheme to be manipulated.
83 ## Common Instance Methods
85 The `Zend\Uri\UriInterface` defines several instance methods that are useful for
86 working with any kind of URI.
88 ### Getting the Scheme of the URI
90 The scheme of the URI is the part of the URI that precedes the colon. For
91 example, the scheme of `http://johndoe@example.com/my/path?query#token` is
92 'http'.
94 ```php
95 $uri = Zend\Uri\UriFactory::factory('mailto:john.doe@example.com');
97 $scheme = $uri->getScheme();  // "mailto"
98 ```
100 The `getScheme()` instance method returns only the scheme part of the URI
101 object (not the separator).
103 ### Getting the Userinfo of the URI
105 The userinfo of the URI is the optional part of the URI that follows the
106 colon and comes before the host-part. For example, the userinfo of
107 `http://johndoe@example.com/my/path?query#token` is 'johndoe'.
109 ```php
110 $uri = Zend\Uri\UriFactory::factory('mailto:john.doe@example.com');
112 $scheme = $uri->getUserinfo();  // "john.doe"
115 The `getUserinfo()` method returns only the userinfo part of the URI object.
117 ### Getting the host of the URI
119 The host of the URI is the optional part of the URI that follows the
120 user-part and comes before the path-part. For example, the host of
121 `http://johndoe@example.com/my/path?query#token` is 'example.com'.
123 ```php
124 $uri = Zend\Uri\UriFactory::factory('mailto:john.doe@example.com');
126 $scheme = $uri->getHost();  // "example.com"
129 The `getHost()` method returns only the host part of the URI object.
131 ### Getting the port of the URI
133 The port of the URI is the optional part of the URI that follows the host-part
134 and comes before the path-part. For example, the host of
135 `http://johndoe@example.com:80/my/path?query#token` is '80'.
137 ```php
138 $uri = Zend\Uri\UriFactory::factory('http://example.com:8080');
140 $scheme = $uri->getPort();  // "8080"
143 Concrete URI instances can define default ports that can be returned when no
144 port is given in the URI:
146 ```php
147 $uri = Zend\Uri\UriFactory::factory('http://example.com');
149 $scheme = $uri->getPort();  // "80"
152 The `getHost()` method returns only the port part of the URI object.
154 ### Getting the path of the URI
156 The path of the URI is a mandatory part of the URI that follows the port
157 and comes before the query-part. For example, the path of
158 `http://johndoe@example.com:80/my/path?query#token` is '/my/path'.
160 ```php
161 $uri = Zend\Uri\UriFactory::factory('http://example.com:80/my/path?a=b&c=d#token');
163 $scheme = $uri->getPath();  // "/my/path"
166 The `getPath()` method returns only the path of the URI object.
168 ### Getting the query-part of the URI
170 The query-part of the URI is an optional part of the URI that follows the
171 path and comes before the fragment. For example, the query of
172 `http://johndoe@example.com:80/my/path?query#token` is 'query'.
174 ```php
175 $uri = Zend\Uri\UriFactory::factory('http://example.com:80/my/path?a=b&c=d#token');
177 $scheme = $uri->getQuery();  // "a=b&c=d"
180 The `getQuery()` method returns only the query-part of the URI object.
182 The query string often contains key=value pairs and therefore can be split into an
183 associative array. This array can be retrieved using `getQueryAsArray()`:
185 ```php
186 $uri = Zend\Uri\UriFactory::factory('http://example.com:80/my/path?a=b&c=d#token');
188 $scheme = $uri->getQueryAsArray();
189 // [
190 //  'a' => 'b',
191 //  'c' => 'd',
192 // ]
195 ### Getting the fragment-part of the URI
197 The fragment-part of the URI is an optional part of the URI that follows
198 the query. For example, the fragment of
199 `http://johndoe@example.com:80/my/path?query#token` is 'token'.
201 ```php
202 $uri = Zend\Uri\UriFactory::factory('http://example.com:80/my/path?a=b&c=d#token');
204 $scheme = $uri->getFragment();  // "token"
207 The `getFragment()` method returns only the fragment-part of the URI object.
209 ### Getting the Entire URI
211 The `toString()` method returns the string representation of the entire *URI*.
213 ```php
214 $uri = Zend\Uri\UriFactory::factory('http://www.zend.com');
216 echo $uri->toString();  // "http://www.zend.com"
218 // Alternate method:
219 echo (string) $uri;     // "http://www.zend.com"
222 The `Zend\Uri\UriInterface` defines also the magic `__toString()` method that
223 returns the string representation of the URI when the object is cast to a
224 string.
226 ## Validating the URI
228 When using `Zend\Uri\UriFactory::factory()`, the given URI will always be
229 validated and a `Zend\Uri\Exception\InvalidArgumentException` will be thrown
230 when the URI is invalid. However, after the `Zend\Uri\UriInterface` is
231 instantiated for a new URI or an existing valid one, it is possible that the URI
232 can later become invalid after it is manipulated.
234 ```php
235 $uri = Zend\Uri\UriFactory::factory('http://www.zend.com');
237 $isValid = $uri->isValid();  // TRUE
240 The `isValid()` instance method provides a means to check that the URI object
241 is still valid.