|
4 | 4 |
|
5 | 5 | class UrlBuilder |
6 | 6 | { |
7 | | - // |
| 7 | + /** |
| 8 | + * The parsed URL parts. |
| 9 | + * |
| 10 | + * @var array |
| 11 | + */ |
| 12 | + protected $urlParts; |
| 13 | + |
| 14 | + /** |
| 15 | + * Crate a new UrlBuilder instance. |
| 16 | + * |
| 17 | + * @param string $url |
| 18 | + * |
| 19 | + * @return \CodeZero\UrlBuilder\UrlBuilder |
| 20 | + */ |
| 21 | + public static function make(string $url): UrlBuilder |
| 22 | + { |
| 23 | + return new self($url); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Crate a new UrlBuilder instance. |
| 28 | + * |
| 29 | + * @param string $url |
| 30 | + */ |
| 31 | + public function __construct(string $url) |
| 32 | + { |
| 33 | + $this->urlParts = parse_url($url) ?: []; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Create a string from URL parts. |
| 38 | + * |
| 39 | + * @param bool $absolute |
| 40 | + * |
| 41 | + * @return string |
| 42 | + */ |
| 43 | + public function build(bool $absolute = true): string |
| 44 | + { |
| 45 | + $url = ''; |
| 46 | + $port = $this->getPort(); |
| 47 | + $query = $this->getQueryString(); |
| 48 | + |
| 49 | + if ($absolute === true) { |
| 50 | + $port = $port ? ":{$port}" : ''; |
| 51 | + $url .= $this->getScheme() . '://' . $this->getHost() . $port; |
| 52 | + } |
| 53 | + |
| 54 | + if ($absolute === false || $this->getPath() !== '/' || $query !== '') { |
| 55 | + $url .= $this->getPath(); |
| 56 | + } |
| 57 | + |
| 58 | + $url .= $query ? '?' . $query : ''; |
| 59 | + |
| 60 | + return $url; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the scheme. |
| 65 | + * |
| 66 | + * @return string |
| 67 | + */ |
| 68 | + public function getScheme(): string |
| 69 | + { |
| 70 | + return $this->get('scheme'); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Set the scheme. |
| 75 | + * |
| 76 | + * @param string $scheme |
| 77 | + * |
| 78 | + * @return \CodeZero\UrlBuilder\UrlBuilder |
| 79 | + */ |
| 80 | + public function setScheme(string $scheme): UrlBuilder |
| 81 | + { |
| 82 | + $this->set('scheme', $scheme); |
| 83 | + |
| 84 | + return $this; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the host. |
| 89 | + * |
| 90 | + * @return string |
| 91 | + */ |
| 92 | + public function getHost(): string |
| 93 | + { |
| 94 | + return $this->get('host'); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Set the host. |
| 99 | + * |
| 100 | + * @param string $host |
| 101 | + * |
| 102 | + * @return \CodeZero\UrlBuilder\UrlBuilder |
| 103 | + */ |
| 104 | + public function setHost(string $host): UrlBuilder |
| 105 | + { |
| 106 | + $this->set('host', $host); |
| 107 | + |
| 108 | + return $this; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Get the port. |
| 113 | + * |
| 114 | + * @return string |
| 115 | + */ |
| 116 | + public function getPort(): string |
| 117 | + { |
| 118 | + return $this->get('port'); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Set the port. |
| 123 | + * |
| 124 | + * @param string|int|null $port |
| 125 | + * |
| 126 | + * @return \CodeZero\UrlBuilder\UrlBuilder |
| 127 | + */ |
| 128 | + public function setPort($port): UrlBuilder |
| 129 | + { |
| 130 | + $this->set('port', $port); |
| 131 | + |
| 132 | + return $this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Get the path. |
| 137 | + * |
| 138 | + * @return string |
| 139 | + */ |
| 140 | + public function getPath(): string |
| 141 | + { |
| 142 | + return $this->get('path'); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Set the path. |
| 147 | + * |
| 148 | + * @param string $path |
| 149 | + * |
| 150 | + * @return \CodeZero\UrlBuilder\UrlBuilder |
| 151 | + */ |
| 152 | + public function setPath(string $path): UrlBuilder |
| 153 | + { |
| 154 | + $this->set('path', '/' . trim($path, '/')); |
| 155 | + |
| 156 | + return $this; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Get the slugs. |
| 161 | + * |
| 162 | + * @return array |
| 163 | + */ |
| 164 | + public function getSlugs(): array |
| 165 | + { |
| 166 | + $path = $this->getPath(); |
| 167 | + |
| 168 | + if ($path === '/') { |
| 169 | + return []; |
| 170 | + } |
| 171 | + |
| 172 | + return explode('/', trim($path, '/')); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Set the slugs. |
| 177 | + * |
| 178 | + * @param array $slugs |
| 179 | + * |
| 180 | + * @return \CodeZero\UrlBuilder\UrlBuilder |
| 181 | + */ |
| 182 | + public function setSlugs(array $slugs): UrlBuilder |
| 183 | + { |
| 184 | + $this->setPath('/' . join('/', $slugs)); |
| 185 | + |
| 186 | + return $this; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Get the query string. |
| 191 | + * |
| 192 | + * @return string |
| 193 | + */ |
| 194 | + public function getQueryString(): string |
| 195 | + { |
| 196 | + return $this->get('query'); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Set the query string. |
| 201 | + * |
| 202 | + * @param string $query |
| 203 | + * |
| 204 | + * @return \CodeZero\UrlBuilder\UrlBuilder |
| 205 | + */ |
| 206 | + public function setQueryString(string $query): UrlBuilder |
| 207 | + { |
| 208 | + $this->set('query', ltrim($query, '?')); |
| 209 | + |
| 210 | + return $this; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Get the query string as an array. |
| 215 | + * |
| 216 | + * @return array |
| 217 | + */ |
| 218 | + public function getQuery(): array |
| 219 | + { |
| 220 | + $query = $this->get('query'); |
| 221 | + |
| 222 | + if ($query === '') { |
| 223 | + return []; |
| 224 | + } |
| 225 | + |
| 226 | + parse_str($query, $queryArray); |
| 227 | + |
| 228 | + return $queryArray; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Set the query string parameters. |
| 233 | + * |
| 234 | + * @param array $query |
| 235 | + * |
| 236 | + * @return \CodeZero\UrlBuilder\UrlBuilder |
| 237 | + */ |
| 238 | + public function setQuery(array $query): UrlBuilder |
| 239 | + { |
| 240 | + $this->set('query', http_build_query($query)); |
| 241 | + |
| 242 | + return $this; |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Get the value of a URL part. |
| 247 | + * |
| 248 | + * @param string $part |
| 249 | + * |
| 250 | + * @return string |
| 251 | + */ |
| 252 | + protected function get(string $part): string |
| 253 | + { |
| 254 | + return $this->urlParts[$part] ?? ''; |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * Set a URL part to a new value. |
| 259 | + * |
| 260 | + * @param string $part |
| 261 | + * @param string|null $value |
| 262 | + * |
| 263 | + * @return \CodeZero\UrlBuilder\UrlBuilder |
| 264 | + */ |
| 265 | + protected function set(string $part, ?string $value): UrlBuilder |
| 266 | + { |
| 267 | + $this->urlParts[$part] = strval($value); |
| 268 | + |
| 269 | + return $this; |
| 270 | + } |
8 | 271 | } |
0 commit comments