|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TgUtils; |
| 4 | + |
| 5 | +/** |
| 6 | + * Analyzes URLs and its components. |
| 7 | + * @author ralph |
| 8 | + * |
| 9 | + */ |
| 10 | +class URL { |
| 11 | + |
| 12 | + protected $components; |
| 13 | + |
| 14 | + /** |
| 15 | + * Constructor. |
| 16 | + * @param mixed string $url as string or other URL object. |
| 17 | + */ |
| 18 | + public function __construct($url) { |
| 19 | + if (is_string($url)) { |
| 20 | + // Trim to avoid problem with parsing |
| 21 | + $url = trim($url); |
| 22 | + // Ad the scheme if it is missing |
| 23 | + if (strpos($url, '://') === FALSE) { |
| 24 | + $url = 'http://'.$url; |
| 25 | + } |
| 26 | + $this->components = parse_url($url); |
| 27 | + |
| 28 | + // And fixing some potential issues |
| 29 | + $this->fixIssues(); |
| 30 | + } else if (is_object($url) && is_a($url, 'TgUtils\\URL')) { |
| 31 | + $this->components = $url->components; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + protected function fixIssues() { |
| 36 | + $parts = array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment'); |
| 37 | + foreach ($parts AS $part) { |
| 38 | + if (!isset($this->components[$part])) { |
| 39 | + $this->components[$part] = NULL; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (!is_numeric($this->components['port'])) { |
| 44 | + $this->components['port'] = 0; |
| 45 | + } |
| 46 | + if ($this->components['path'] == NULL) { |
| 47 | + $this->components['path'] = '/'; |
| 48 | + } |
| 49 | + if (substr($this->components['path'], 0, 1) != '/') { |
| 50 | + $this->components['path'] = '/' . $this->components['path']; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns the URL scheme. |
| 56 | + * @return string the scheme |
| 57 | + */ |
| 58 | + public function getScheme() { |
| 59 | + return $this->components['scheme']; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Modify this URL by setting a new scheme. |
| 64 | + * @param string $scheme - the new scheme |
| 65 | + */ |
| 66 | + public function setScheme($scheme) { |
| 67 | + $this->components['scheme'] = $scheme; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the URL's host. |
| 72 | + * @return string the host |
| 73 | + */ |
| 74 | + public function getHost() { |
| 75 | + return $this->components['host']; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Modify this URL by setting a new host. |
| 80 | + * @param string $host - the new host |
| 81 | + */ |
| 82 | + public function setHost($host) { |
| 83 | + $this->components['host'] = $host; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns the URL's port. |
| 88 | + * @return string the port |
| 89 | + */ |
| 90 | + public function getPort() { |
| 91 | + return $this->components['port']; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Modify this URL by setting a new port. |
| 96 | + * @param int $port - the new port |
| 97 | + */ |
| 98 | + public function setPort($port) { |
| 99 | + $this->components['port'] = $port; |
| 100 | + $this->fixIssues(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns the URL's user. |
| 105 | + * @return string the user |
| 106 | + */ |
| 107 | + public function getUser() { |
| 108 | + return $this->components['user']; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Modify this URL by setting a new user. |
| 113 | + * @param string $user - the new user |
| 114 | + */ |
| 115 | + public function setUser($user) { |
| 116 | + $this->components['user'] = $user; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the URL's password. |
| 121 | + * @return string the password |
| 122 | + */ |
| 123 | + public function getPassword() { |
| 124 | + return $this->components['pass']; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Modify this URL by setting a new password. |
| 129 | + * @param string $password - the new password |
| 130 | + */ |
| 131 | + public function setPassword($password) { |
| 132 | + $this->components['pass'] = $password; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Returns the URL's path. |
| 137 | + * @return string the path |
| 138 | + */ |
| 139 | + public function getPath() { |
| 140 | + return $this->components['path']; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Modify this URL by setting a new path. |
| 145 | + * @param string $path - the new path |
| 146 | + */ |
| 147 | + public function setPath($path) { |
| 148 | + $this->components['path'] = $path; |
| 149 | + $this->fixIssues(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns the URL's query string. |
| 154 | + * @return string the query string |
| 155 | + */ |
| 156 | + public function getQuery() { |
| 157 | + return $this->components['query']; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Modify this URL by setting a new query string. |
| 162 | + * @param string $query - the new query string |
| 163 | + */ |
| 164 | + public function setQuery($query) { |
| 165 | + $this->components['query'] = $query; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Returns the URL's fragment. |
| 170 | + * @return string the fragment |
| 171 | + */ |
| 172 | + public function getFragment() { |
| 173 | + return $this->components['fragment']; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Modify this URL by setting a new fragment. |
| 178 | + * @param string $fragment - the new fragment |
| 179 | + */ |
| 180 | + public function setFragment($fragment) { |
| 181 | + $this->components['fragment'] = $fragment; |
| 182 | + } |
| 183 | + |
| 184 | + public function __toString() { |
| 185 | + // Scheme |
| 186 | + $rc = $this->components['scheme'].'://'; |
| 187 | + |
| 188 | + // User |
| 189 | + if ($this->components['user'] != NULL) { |
| 190 | + $rc .= $this->components['user']; |
| 191 | + |
| 192 | + // Password |
| 193 | + if ($this->components['pass'] != NULL) { |
| 194 | + $rc .= ':'.$this->components['pass']; |
| 195 | + } |
| 196 | + |
| 197 | + $rc .= '@'; |
| 198 | + } |
| 199 | + |
| 200 | + // Host |
| 201 | + $rc .= $this->components['host']; |
| 202 | + |
| 203 | + // Port |
| 204 | + if ($this->components['port'] != 0) { |
| 205 | + $rc .= ':'.$this->components['port']; |
| 206 | + } |
| 207 | + |
| 208 | + // Path |
| 209 | + $rc .= $this->components['path']; |
| 210 | + |
| 211 | + // Query |
| 212 | + if ($this->components['query'] != NULL) { |
| 213 | + $rc .= '?'.$this->components['query']; |
| 214 | + } |
| 215 | + |
| 216 | + // Fragment |
| 217 | + if ($this->components['fragment'] != NULL) { |
| 218 | + $rc .= '#'.$this->components['fragment']; |
| 219 | + } |
| 220 | + |
| 221 | + return $rc; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Returns whether to URLs are equal. |
| 226 | + * <p>Equality is defined as the equality of stringified URLs.</p> |
| 227 | + * @return boolean TRUE or FALSE |
| 228 | + */ |
| 229 | + public function equals($another) { |
| 230 | + if (is_string($another)) { |
| 231 | + $other = new URL($another); |
| 232 | + return $this->__toString() == $other->__toString(); |
| 233 | + } else if (is_object($another) && is_a($another, 'TgUtils\\URL')) { |
| 234 | + return $this->__toString() == $another->__toString(); |
| 235 | + } |
| 236 | + return FALSE; |
| 237 | + } |
| 238 | +} |
| 239 | + |
0 commit comments