PluginProbe
Independent Analytics – WordPress Analytics Plugin / 2.5.0
Independent Analytics – WordPress Analytics Plugin v2.5.0
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 All 120 releases
independent-analytics / vendor / league / uri / src / Http.php
Http.php
293 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * League.Uri (https://uri.thephpleague.com)
5 *
6 * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 declare (strict_types=1);
12 namespace IAWPSCOPED\League\Uri;
13
14 use IAWPSCOPED\League\Uri\Contracts\UriInterface;
15 use IAWPSCOPED\League\Uri\Exceptions\SyntaxError;
16 use IAWPSCOPED\Psr\Http\Message\UriInterface as Psr7UriInterface;
17 use function is_object;
18 use function is_scalar;
19 use function method_exists;
20 use function sprintf;
21 /** @internal */
22 final class Http implements Psr7UriInterface, \JsonSerializable
23 {
24 /**
25 * @var UriInterface
26 */
27 private $uri;
28 /**
29 * New instance.
30 */
31 private function __construct(UriInterface $uri)
32 {
33 $this->validate($uri);
34 $this->uri = $uri;
35 }
36 /**
37 * Validate the submitted uri against PSR-7 UriInterface.
38 *
39 * @throws SyntaxError if the given URI does not follow PSR-7 UriInterface rules
40 */
41 private function validate(UriInterface $uri) : void
42 {
43 $scheme = $uri->getScheme();
44 if (null === $scheme && '' === $uri->getHost()) {
45 throw new SyntaxError(sprintf('an URI without scheme can not contains a empty host string according to PSR-7: %s', (string) $uri));
46 }
47 $port = $uri->getPort();
48 if (null !== $port && ($port < 0 || $port > 65535)) {
49 throw new SyntaxError(sprintf('The URI port is outside the established TCP and UDP port ranges: %s', (string) $uri->getPort()));
50 }
51 }
52 /**
53 * Static method called by PHP's var export.
54 */
55 public static function __set_state(array $components) : self
56 {
57 return new self($components['uri']);
58 }
59 /**
60 * Create a new instance from a string.
61 *
62 * @param string|mixed $uri
63 */
64 public static function createFromString($uri = '') : self
65 {
66 return new self(Uri::createFromString($uri));
67 }
68 /**
69 * Create a new instance from a hash of parse_url parts.
70 *
71 * @param array $components a hash representation of the URI similar
72 * to PHP parse_url function result
73 */
74 public static function createFromComponents(array $components) : self
75 {
76 return new self(Uri::createFromComponents($components));
77 }
78 /**
79 * Create a new instance from the environment.
80 */
81 public static function createFromServer(array $server) : self
82 {
83 return new self(Uri::createFromServer($server));
84 }
85 /**
86 * Create a new instance from a URI and a Base URI.
87 *
88 * The returned URI must be absolute.
89 *
90 * @param mixed $uri the input URI to create
91 * @param mixed $base_uri the base URI used for reference
92 */
93 public static function createFromBaseUri($uri, $base_uri = null) : self
94 {
95 return new self(Uri::createFromBaseUri($uri, $base_uri));
96 }
97 /**
98 * Create a new instance from a URI object.
99 *
100 * @param Psr7UriInterface|UriInterface $uri the input URI to create
101 */
102 public static function createFromUri($uri) : self
103 {
104 if ($uri instanceof UriInterface) {
105 return new self($uri);
106 }
107 return new self(Uri::createFromUri($uri));
108 }
109 /**
110 * {@inheritDoc}
111 */
112 public function getScheme() : string
113 {
114 return (string) $this->uri->getScheme();
115 }
116 /**
117 * {@inheritDoc}
118 */
119 public function getAuthority() : string
120 {
121 return (string) $this->uri->getAuthority();
122 }
123 /**
124 * {@inheritDoc}
125 */
126 public function getUserInfo() : string
127 {
128 return (string) $this->uri->getUserInfo();
129 }
130 /**
131 * {@inheritDoc}
132 */
133 public function getHost() : string
134 {
135 return (string) $this->uri->getHost();
136 }
137 /**
138 * {@inheritDoc}
139 */
140 public function getPort() : ?int
141 {
142 return $this->uri->getPort();
143 }
144 /**
145 * {@inheritDoc}
146 */
147 public function getPath() : string
148 {
149 return $this->uri->getPath();
150 }
151 /**
152 * {@inheritDoc}
153 */
154 public function getQuery() : string
155 {
156 return (string) $this->uri->getQuery();
157 }
158 /**
159 * {@inheritDoc}
160 */
161 public function getFragment() : string
162 {
163 return (string) $this->uri->getFragment();
164 }
165 /**
166 * {@inheritDoc}
167 */
168 public function withScheme($scheme) : self
169 {
170 $scheme = $this->filterInput($scheme);
171 if ('' === $scheme) {
172 $scheme = null;
173 }
174 $uri = $this->uri->withScheme($scheme);
175 if ($uri->getScheme() === $this->uri->getScheme()) {
176 return $this;
177 }
178 return new self($uri);
179 }
180 /**
181 * Safely stringify input when possible.
182 *
183 * @param mixed $str the value to evaluate as a string
184 *
185 * @throws SyntaxError if the submitted data can not be converted to string
186 *
187 * @return string|mixed
188 */
189 private function filterInput($str)
190 {
191 if (is_scalar($str) || is_object($str) && method_exists($str, '__toString')) {
192 return (string) $str;
193 }
194 return $str;
195 }
196 /**
197 * {@inheritDoc}
198 */
199 public function withUserInfo($user, $password = null) : self
200 {
201 $user = $this->filterInput($user);
202 if ('' === $user) {
203 $user = null;
204 }
205 $uri = $this->uri->withUserInfo($user, $password);
206 if ($uri->getUserInfo() === $this->uri->getUserInfo()) {
207 return $this;
208 }
209 return new self($uri);
210 }
211 /**
212 * {@inheritDoc}
213 */
214 public function withHost($host) : self
215 {
216 $host = $this->filterInput($host);
217 if ('' === $host) {
218 $host = null;
219 }
220 $uri = $this->uri->withHost($host);
221 if ($uri->getHost() === $this->uri->getHost()) {
222 return $this;
223 }
224 return new self($uri);
225 }
226 /**
227 * {@inheritDoc}
228 */
229 public function withPort($port) : self
230 {
231 $uri = $this->uri->withPort($port);
232 if ($uri->getPort() === $this->uri->getPort()) {
233 return $this;
234 }
235 return new self($uri);
236 }
237 /**
238 * {@inheritDoc}
239 */
240 public function withPath($path) : self
241 {
242 $uri = $this->uri->withPath($path);
243 if ($uri->getPath() === $this->uri->getPath()) {
244 return $this;
245 }
246 return new self($uri);
247 }
248 /**
249 * {@inheritDoc}
250 */
251 public function withQuery($query) : self
252 {
253 $query = $this->filterInput($query);
254 if ('' === $query) {
255 $query = null;
256 }
257 $uri = $this->uri->withQuery($query);
258 if ($uri->getQuery() === $this->uri->getQuery()) {
259 return $this;
260 }
261 return new self($uri);
262 }
263 /**
264 * {@inheritDoc}
265 */
266 public function withFragment($fragment) : self
267 {
268 $fragment = $this->filterInput($fragment);
269 if ('' === $fragment) {
270 $fragment = null;
271 }
272 $uri = $this->uri->withFragment($fragment);
273 if ($uri->getFragment() === $this->uri->getFragment()) {
274 return $this;
275 }
276 return new self($uri);
277 }
278 /**
279 * {@inheritDoc}
280 */
281 public function __toString() : string
282 {
283 return $this->uri->__toString();
284 }
285 /**
286 * {@inheritDoc}
287 */
288 public function jsonSerialize() : string
289 {
290 return $this->uri->__toString();
291 }
292 }
293