1 | <?php |
||
32 | class Formatter |
||
33 | { |
||
34 | use UriBuilderTrait; |
||
35 | |||
36 | const HOST_AS_UNICODE = 1; |
||
37 | |||
38 | const HOST_AS_ASCII = 2; |
||
39 | |||
40 | /** |
||
41 | * host encoding property |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $hostEncoding = self::HOST_AS_UNICODE; |
||
46 | |||
47 | /** |
||
48 | * Port |
||
49 | * |
||
50 | * @var Port |
||
51 | */ |
||
52 | protected $port; |
||
53 | |||
54 | /** |
||
55 | * query encoding property |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $queryEncoding = PHP_QUERY_RFC3986; |
||
60 | |||
61 | /** |
||
62 | * Query Parser object |
||
63 | * |
||
64 | * @var QueryParser |
||
65 | */ |
||
66 | protected $queryParser; |
||
67 | |||
68 | /** |
||
69 | * query separator property |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $querySeparator = '&'; |
||
74 | |||
75 | /** |
||
76 | * Should the query component be preserved |
||
77 | * |
||
78 | * @var bool |
||
79 | */ |
||
80 | protected $preserveQuery = false; |
||
81 | |||
82 | /** |
||
83 | * Should the fragment component string be preserved |
||
84 | * |
||
85 | * @var bool |
||
86 | */ |
||
87 | protected $preserveFragment = false; |
||
88 | |||
89 | /** |
||
90 | * New Instance |
||
91 | */ |
||
92 | 54 | public function __construct() |
|
93 | { |
||
94 | 54 | $this->queryParser = new QueryParser(); |
|
95 | 54 | } |
|
96 | |||
97 | /** |
||
98 | * Host encoding setter |
||
99 | * |
||
100 | * @param int $encode a predefined constant value |
||
101 | */ |
||
102 | 21 | public function setHostEncoding($encode) |
|
103 | { |
||
104 | 21 | if (!in_array($encode, [self::HOST_AS_UNICODE, self::HOST_AS_ASCII])) { |
|
105 | 3 | throw new InvalidArgumentException('Unknown Host encoding rule'); |
|
106 | } |
||
107 | 18 | $this->hostEncoding = $encode; |
|
108 | 18 | } |
|
109 | |||
110 | /** |
||
111 | * Host encoding getter |
||
112 | * |
||
113 | * DEPRECATION WARNING! This method will be removed in the next major point release |
||
114 | * |
||
115 | * @deprecated deprecated since version 4.1 |
||
116 | * |
||
117 | * @return int |
||
118 | */ |
||
119 | public function getHostEncoding() |
||
123 | |||
124 | /** |
||
125 | * Enforce Port presence |
||
126 | * |
||
127 | * @param null|int $port URI port value |
||
128 | */ |
||
129 | 6 | public function setPort($port) |
|
133 | |||
134 | /** |
||
135 | * Query encoding setter |
||
136 | * |
||
137 | * @param int $encode a predefined constant value |
||
138 | */ |
||
139 | 9 | public function setQueryEncoding($encode) |
|
146 | |||
147 | /** |
||
148 | * Query encoding getter |
||
149 | * |
||
150 | * DEPRECATION WARNING! This method will be removed in the next major point release |
||
151 | * |
||
152 | * @deprecated deprecated since version 4.1 |
||
153 | * |
||
154 | * @return int |
||
155 | */ |
||
156 | public function getQueryEncoding() |
||
160 | |||
161 | /** |
||
162 | * Query separator setter |
||
163 | * |
||
164 | * @param string $separator |
||
165 | */ |
||
166 | 15 | public function setQuerySeparator($separator) |
|
172 | |||
173 | /** |
||
174 | * Query separator getter |
||
175 | * |
||
176 | * DEPRECATION WARNING! This method will be removed in the next major point release |
||
177 | * |
||
178 | * @deprecated deprecated since version 4.1 |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function getQuerySeparator() |
||
186 | |||
187 | /** |
||
188 | * Whether we should preserve the Query component |
||
189 | * regardless of its value. |
||
190 | * |
||
191 | * If set to true the query delimiter will be appended |
||
192 | * to the URI regardless of the query string value |
||
193 | * |
||
194 | * @param bool $status |
||
195 | */ |
||
196 | 3 | public function preserveQuery($status) |
|
200 | |||
201 | /** |
||
202 | * Whether we should preserve the Fragment component |
||
203 | * regardless of its value. |
||
204 | * |
||
205 | * If set to true the fragment delimiter will be appended |
||
206 | * to the URI regardless of the query string value |
||
207 | * |
||
208 | * @param bool $status |
||
209 | */ |
||
210 | 3 | public function preserveFragment($status) |
|
214 | |||
215 | /** |
||
216 | * Format an object |
||
217 | * |
||
218 | * @see Formatter::format |
||
219 | * |
||
220 | * @param UriPart|Uri|UriInterface $input |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | 42 | public function __invoke($input) |
|
228 | |||
229 | /** |
||
230 | * Format an object |
||
231 | * |
||
232 | * Format an object according to the formatter properties. |
||
233 | * The object must implement one of the following interface: |
||
234 | * <ul> |
||
235 | * <li>League\Uri\Interfaces\Uri |
||
236 | * <li>League\Uri\Interfaces\UriPart |
||
237 | * <li>Psr\Http\Message\UriInterface |
||
238 | * </ul> |
||
239 | * |
||
240 | * @param UriPart|Uri|UriInterface $input |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | 45 | public function format($input) |
|
258 | |||
259 | /** |
||
260 | * Format a UriPart implemented object according to the Formatter properties |
||
261 | * |
||
262 | * @param UriPart $part |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | 42 | protected function formatUriPart(UriPart $part) |
|
278 | |||
279 | /** |
||
280 | * Format a Host according to the Formatter properties |
||
281 | * |
||
282 | * @param HostInterface $host |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | 24 | protected function formatHost(HostInterface $host) |
|
294 | |||
295 | /** |
||
296 | * Format an Uri according to the Formatter properties |
||
297 | * |
||
298 | * @param Uri|UriInterface $uri |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | 24 | protected function formatUri($uri) |
|
317 | |||
318 | /** |
||
319 | * Format a URI authority according to the Formatter properties |
||
320 | * |
||
321 | * @param Uri|UriInterface $uri |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | 24 | protected function formatAuthority($uri) |
|
344 | |||
345 | /** |
||
346 | * Format a URI port component according to the Formatter properties |
||
347 | * |
||
348 | * @param null|int $port |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | 18 | protected function formatPort($port) |
|
364 | |||
365 | /** |
||
366 | * Format a URI Query component according to the Formatter properties |
||
367 | * |
||
368 | * @param Uri|UriInterface $uri |
||
369 | * |
||
370 | * @return string |
||
371 | */ |
||
372 | 24 | protected function formatQuery($uri) |
|
381 | |||
382 | /** |
||
383 | * Format a URI Fragment component according to the Formatter properties |
||
384 | * |
||
385 | * @param Uri|UriInterface $uri |
||
386 | * |
||
387 | * @return string |
||
388 | */ |
||
389 | 24 | protected function formatFragment($uri) |
|
398 | } |
||
399 |