PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / http / src / Http / UrlImmutable.php

UrlImmutable.php in Packeta trunk, at deps/nette/http/src/Http/UrlImmutable.php

256 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file is part of the Nette Framework (https://nette.org)
5 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6 */
7 declare (strict_types=1);
8 namespace Packetery\Nette\Http;
9
10 use Packetery\Nette;
11 /**
12 * Immutable representation of a URL.
13 *
14 * <pre>
15 * scheme user password host port path query fragment
16 * | | | | | | | |
17 * /--\ /--\ /------\ /-------\ /--\/------------\ /--------\ /------\
18 * http://john:x0y17575@nette.org:8042/en/manual.php?name=param#fragment <-- absoluteUrl
19 * \______\__________________________/
20 * | |
21 * hostUrl authority
22 * </pre>
23 *
24 * @property-read string $scheme
25 * @property-read string $user
26 * @property-read string $password
27 * @property-read string $host
28 * @property-read int $port
29 * @property-read string $path
30 * @property-read string $query
31 * @property-read string $fragment
32 * @property-read string $absoluteUrl
33 * @property-read string $authority
34 * @property-read string $hostUrl
35 * @property-read array $queryParameters
36 */
37 class UrlImmutable implements \JsonSerializable
38 {
39 use \Packetery\Nette\SmartObject;
40 /** @var string */
41 private $scheme = '';
42 /** @var string */
43 private $user = '';
44 /** @var string */
45 private $password = '';
46 /** @var string */
47 private $host = '';
48 /** @var int|null */
49 private $port;
50 /** @var string */
51 private $path = '';
52 /** @var array */
53 private $query = [];
54 /** @var string */
55 private $fragment = '';
56 /** @var string */
57 private $authority = '';
58 /**
59 * @param string|self|Url $url
60 * @throws \Packetery\Nette\InvalidArgumentException if URL is malformed
61 */
62 public function __construct($url)
63 {
64 if (!$url instanceof Url && !$url instanceof self && !\is_string($url)) {
65 throw new \Packetery\Nette\InvalidArgumentException();
66 }
67 $url = \is_string($url) ? new Url($url) : $url;
68 [$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment] = $url->export();
69 $this->build();
70 }
71 /** @return static */
72 public function withScheme(string $scheme)
73 {
74 $dolly = clone $this;
75 $dolly->scheme = $scheme;
76 $dolly->build();
77 return $dolly;
78 }
79 public function getScheme() : string
80 {
81 return $this->scheme;
82 }
83 /** @return static */
84 public function withUser(string $user)
85 {
86 $dolly = clone $this;
87 $dolly->user = $user;
88 $dolly->build();
89 return $dolly;
90 }
91 public function getUser() : string
92 {
93 return $this->user;
94 }
95 /** @return static */
96 public function withPassword(string $password)
97 {
98 $dolly = clone $this;
99 $dolly->password = $password;
100 $dolly->build();
101 return $dolly;
102 }
103 public function getPassword() : string
104 {
105 return $this->password;
106 }
107 /** @return static */
108 public function withoutUserInfo()
109 {
110 $dolly = clone $this;
111 $dolly->user = $dolly->password = '';
112 $dolly->build();
113 return $dolly;
114 }
115 /** @return static */
116 public function withHost(string $host)
117 {
118 $dolly = clone $this;
119 $dolly->host = $host;
120 $dolly->build();
121 return $dolly;
122 }
123 public function getHost() : string
124 {
125 return $this->host;
126 }
127 public function getDomain(int $level = 2) : string
128 {
129 $parts = \ip2long($this->host) ? [$this->host] : \explode('.', $this->host);
130 $parts = $level >= 0 ? \array_slice($parts, -$level) : \array_slice($parts, 0, $level);
131 return \implode('.', $parts);
132 }
133 /** @return static */
134 public function withPort(int $port)
135 {
136 $dolly = clone $this;
137 $dolly->port = $port;
138 $dolly->build();
139 return $dolly;
140 }
141 public function getPort() : ?int
142 {
143 return $this->port ?: $this->getDefaultPort();
144 }
145 public function getDefaultPort() : ?int
146 {
147 return Url::$defaultPorts[$this->scheme] ?? null;
148 }
149 /** @return static */
150 public function withPath(string $path)
151 {
152 $dolly = clone $this;
153 $dolly->path = $path;
154 $dolly->build();
155 return $dolly;
156 }
157 public function getPath() : string
158 {
159 return $this->path;
160 }
161 /**
162 * @param string|array $query
163 * @return static
164 */
165 public function withQuery($query)
166 {
167 $dolly = clone $this;
168 $dolly->query = \is_array($query) ? $query : Url::parseQuery($query);
169 $dolly->build();
170 return $dolly;
171 }
172 public function getQuery() : string
173 {
174 return \http_build_query($this->query, '', '&', \PHP_QUERY_RFC3986);
175 }
176 /**
177 * @param mixed $value null unsets the parameter
178 * @return static
179 */
180 public function withQueryParameter(string $name, $value)
181 {
182 $dolly = clone $this;
183 $dolly->query[$name] = $value;
184 return $dolly;
185 }
186 public function getQueryParameters() : array
187 {
188 return $this->query;
189 }
190 /** @return array|string|null */
191 public function getQueryParameter(string $name)
192 {
193 return $this->query[$name] ?? null;
194 }
195 /** @return static */
196 public function withFragment(string $fragment)
197 {
198 $dolly = clone $this;
199 $dolly->fragment = $fragment;
200 $dolly->build();
201 return $dolly;
202 }
203 public function getFragment() : string
204 {
205 return $this->fragment;
206 }
207 /**
208 * Returns the entire URI including query string and fragment.
209 */
210 public function getAbsoluteUrl() : string
211 {
212 return $this->getHostUrl() . $this->path . (($tmp = $this->getQuery()) ? '?' . $tmp : '') . ($this->fragment === '' ? '' : '#' . $this->fragment);
213 }
214 /**
215 * Returns the [user[:pass]@]host[:port] part of URI.
216 */
217 public function getAuthority() : string
218 {
219 return $this->authority;
220 }
221 /**
222 * Returns the scheme and authority part of URI.
223 */
224 public function getHostUrl() : string
225 {
226 return ($this->scheme ? $this->scheme . ':' : '') . ($this->authority !== '' ? '//' . $this->authority : '');
227 }
228 public function __toString() : string
229 {
230 return $this->getAbsoluteUrl();
231 }
232 /**
233 * @param string|Url|self $url
234 */
235 public function isEqual($url) : bool
236 {
237 return (new Url($this))->isEqual($url);
238 }
239 public function jsonSerialize() : string
240 {
241 return $this->getAbsoluteUrl();
242 }
243 /** @internal */
244 public final function export() : array
245 {
246 return [$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment];
247 }
248 protected function build() : void
249 {
250 if ($this->host && \substr($this->path, 0, 1) !== '/') {
251 $this->path = '/' . $this->path;
252 }
253 $this->authority = $this->host === '' ? '' : ($this->user !== '' ? \rawurlencode($this->user) . ($this->password === '' ? '' : ':' . \rawurlencode($this->password)) . '@' : '') . $this->host . ($this->port && $this->port !== $this->getDefaultPort() ? ':' . $this->port : '');
254 }
255 }
256