|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Francerz\HttpUtils\Dev; |
| 4 | + |
| 5 | +use Francerz\Http\Utils\UriHelper; |
| 6 | +use Psr\Http\Message\UriInterface; |
| 7 | + |
| 8 | +class Uri implements UriInterface |
| 9 | +{ |
| 10 | + private $scheme; |
| 11 | + private $user; |
| 12 | + private $password; |
| 13 | + private $host; |
| 14 | + private $port; |
| 15 | + private $path; |
| 16 | + private $query; |
| 17 | + private $fragment; |
| 18 | + |
| 19 | + public function __construct(string $uri) |
| 20 | + { |
| 21 | + $uriParts = parse_url($uri); |
| 22 | + $this->scheme = $uriParts['scheme']; |
| 23 | + $this->user = $uriParts['user'] ?? ''; |
| 24 | + $this->password = $uriParts['pass'] ?? null; |
| 25 | + $this->host = $uriParts['host'] ?? ''; |
| 26 | + $this->port = $uriParts['port'] ?? null; |
| 27 | + $this->path = $uriParts['path'] ?? '/'; |
| 28 | + $this->query = $uriParts['query'] ?? ''; |
| 29 | + $this->fragment = $uriParts['fragment'] ?? ''; |
| 30 | + } |
| 31 | + |
| 32 | + public function getScheme(): string |
| 33 | + { |
| 34 | + return $this->scheme; |
| 35 | + } |
| 36 | + |
| 37 | + public function getAuthority(): string |
| 38 | + { |
| 39 | + $authority = $this->host; |
| 40 | + if (!empty($this->user)) { |
| 41 | + $authority = $this->getUserInfo() . '@' . $authority; |
| 42 | + } |
| 43 | + if (isset($this->port)) { |
| 44 | + $authority .= ':' . $this->port; |
| 45 | + } |
| 46 | + return $authority; |
| 47 | + } |
| 48 | + |
| 49 | + public function getUserInfo(): string |
| 50 | + { |
| 51 | + $userInfo = $this->user; |
| 52 | + if (isset($this->password)) { |
| 53 | + $userInfo = ':' . $this->password; |
| 54 | + } |
| 55 | + return $userInfo; |
| 56 | + } |
| 57 | + |
| 58 | + public function getHost(): string |
| 59 | + { |
| 60 | + return $this->host; |
| 61 | + } |
| 62 | + |
| 63 | + public function getPort(): ?int |
| 64 | + { |
| 65 | + return $this->port; |
| 66 | + } |
| 67 | + |
| 68 | + public function getPath(): string |
| 69 | + { |
| 70 | + return $this->path; |
| 71 | + } |
| 72 | + |
| 73 | + public function getQuery(): string |
| 74 | + { |
| 75 | + return $this->query; |
| 76 | + } |
| 77 | + |
| 78 | + public function getFragment(): string |
| 79 | + { |
| 80 | + return $this->fragment; |
| 81 | + } |
| 82 | + |
| 83 | + public function withScheme(string $scheme): UriInterface |
| 84 | + { |
| 85 | + $clone = clone $this; |
| 86 | + $clone->scheme = $scheme; |
| 87 | + return $clone; |
| 88 | + } |
| 89 | + |
| 90 | + public function withUserInfo(string $user, ?string $password = null): UriInterface |
| 91 | + { |
| 92 | + $clone = clone $this; |
| 93 | + $clone->user = $user; |
| 94 | + $clone->password = $password; |
| 95 | + return $clone; |
| 96 | + } |
| 97 | + |
| 98 | + public function withHost(string $host): UriInterface |
| 99 | + { |
| 100 | + $clone = clone $this; |
| 101 | + $clone->host = $host; |
| 102 | + return $clone; |
| 103 | + } |
| 104 | + |
| 105 | + public function withPort(?int $port): UriInterface |
| 106 | + { |
| 107 | + $clone = clone $this; |
| 108 | + $clone->port = $port; |
| 109 | + return $clone; |
| 110 | + } |
| 111 | + |
| 112 | + public function withPath(string $path): UriInterface |
| 113 | + { |
| 114 | + $clone = clone $this; |
| 115 | + $clone->path = $path; |
| 116 | + return $clone; |
| 117 | + } |
| 118 | + |
| 119 | + public function withQuery(string $query): UriInterface |
| 120 | + { |
| 121 | + $clone = clone $this; |
| 122 | + $clone->query = $query; |
| 123 | + return $clone; |
| 124 | + } |
| 125 | + |
| 126 | + public function withFragment(string $fragment): UriInterface |
| 127 | + { |
| 128 | + $clone = clone $this; |
| 129 | + $clone->fragment = $fragment; |
| 130 | + return $clone; |
| 131 | + } |
| 132 | + |
| 133 | + public function __toString(): string |
| 134 | + { |
| 135 | + return UriHelper::buildStringFromParts([ |
| 136 | + 'scheme' => $this->scheme, |
| 137 | + 'user' => $this->user, |
| 138 | + 'pass' => $this->password, |
| 139 | + 'host' => $this->host, |
| 140 | + 'port' => $this->port, |
| 141 | + 'path' => $this->path, |
| 142 | + 'query' => $this->query, |
| 143 | + 'fragment' => $this->fragment |
| 144 | + ]); |
| 145 | + } |
| 146 | +} |
0 commit comments