UrlHandler.php (5414B)
1 <?php 2 3 namespace Finwo\Datatools; 4 5 class UrlHandler 6 { 7 /** 8 * @var string 9 */ 10 protected $scheme; 11 12 /** 13 * @var string 14 */ 15 protected $user; 16 17 /** 18 * @var string 19 */ 20 protected $password; 21 22 /** 23 * @var string 24 */ 25 protected $host; 26 27 /** 28 * @var integer 29 */ 30 protected $port; 31 32 /** 33 * @var string 34 */ 35 protected $path; 36 37 /** 38 * @var array 39 */ 40 protected $query = array(); 41 42 /** 43 * @var string 44 */ 45 protected $fragment; 46 47 /** 48 * UrlHandler constructor. 49 * 50 * @param $url 51 */ 52 public function __construct($url = null) 53 { 54 if (gettype($url) == 'string') { 55 $this->setUrl($url); 56 } 57 } 58 59 /** 60 * @param $query 61 * 62 * @return $this 63 */ 64 public function setQuery( $query ) 65 { 66 $this->query = parse_str($query); 67 return $this; 68 } 69 70 /** 71 * @param string $key 72 * @param mixed $value 73 * 74 * @return $this 75 */ 76 public function setQueryValue( $key, $value ) 77 { 78 $this->query[$key] = $value; 79 return $this; 80 } 81 82 /** 83 * @return string 84 */ 85 public function getQuery() 86 { 87 return http_build_query($this->query); 88 } 89 90 /** 91 * getUrl() 92 * Reconstructs the url 93 * 94 * @return string 95 */ 96 public function getUrl() 97 { 98 return (isset($this->scheme)? $this->scheme . '://' : '') . 99 (isset($this->user) ? $this->user : '') . 100 (isset($this->password) ? ':' . $this->password : '') . 101 (isset($this->user) ? '@' : '') . 102 (isset($this->host) ? $this->host : '') . 103 (isset($this->port) ? ':' . $this->port : '') . 104 (count($this->query) ? '?' . $this->getQuery() : '') . 105 (isset($this->fragment) ? '#' . $this->fragment : '') 106 ; 107 } 108 109 public function setUrl( $url ) 110 { 111 $mapper = new \JsonMapper(); 112 $mapper->map(json_decode(json_encode($this->parseUrl($url))), $this); 113 } 114 115 // See http://php.net/manual/en/function.parse-url.php#116456 116 // Modified, but still based upon that 117 public function parseUrl( $url ) 118 { 119 // Init the output var in advance 120 $result = array(); 121 122 // Fetch fragment 123 if(strpos($url,"#")>-1){ 124 $a=explode("#",$url,2); 125 $url=$a[0]; 126 $result['fragment']=$a[1]; 127 } 128 129 // Fetch query 130 if(strpos($url,"?")>-1){ 131 $a=explode("?",$url,2); 132 $url=$a[0]; 133 $result['query']=$a[1]; 134 } 135 136 // Fetch scheme 137 if(strpos($url,"://")>-1){ 138 $result['scheme']=substr($url,0,strpos($url,"//")-1); 139 $url=substr($url,strpos($url,"//")+2,strlen($url)); 140 } 141 142 // Fetch path 143 if(strpos($url,"/")>-1){ 144 $a=explode("/",$url,2); 145 $url=$a[0]; 146 $result['path']="/".$a[1]; 147 } 148 149 // Fetch port 150 if(strpos($url,":")>-1){ 151 $a=explode(":",$url,2); 152 $url=$a[0]; 153 $result['port']=$a[1]; 154 } 155 156 // Only the host remains 157 if (strlen($url)) { 158 $result['host'] = $url; 159 } 160 161 return $result; 162 } 163 164 /** 165 * @return string 166 */ 167 public function getScheme() 168 { 169 return $this->scheme; 170 } 171 172 /** 173 * @param string $scheme 174 * 175 * @return UrlHandler 176 */ 177 public function setScheme($scheme) 178 { 179 $this->scheme = $scheme; 180 return $this; 181 } 182 183 /** 184 * @return string 185 */ 186 public function getUser() 187 { 188 return $this->user; 189 } 190 191 /** 192 * @param string $user 193 * 194 * @return UrlHandler 195 */ 196 public function setUser($user) 197 { 198 $this->user = $user; 199 return $this; 200 } 201 202 /** 203 * @return string 204 */ 205 public function getPassword() 206 { 207 return $this->password; 208 } 209 210 /** 211 * @param string $password 212 * 213 * @return UrlHandler 214 */ 215 public function setPassword($password) 216 { 217 $this->password = $password; 218 return $this; 219 } 220 221 /** 222 * @return string 223 */ 224 public function getHost() 225 { 226 return $this->host; 227 } 228 229 /** 230 * @param string $host 231 * 232 * @return UrlHandler 233 */ 234 public function setHost($host) 235 { 236 $this->host = $host; 237 return $this; 238 } 239 240 /** 241 * @return int 242 */ 243 public function getPort() 244 { 245 return $this->port; 246 } 247 248 /** 249 * @param int $port 250 * 251 * @return UrlHandler 252 */ 253 public function setPort($port) 254 { 255 $this->port = $port; 256 return $this; 257 } 258 259 /** 260 * @return string 261 */ 262 public function getPath() 263 { 264 return $this->path; 265 } 266 267 /** 268 * @param string $path 269 * 270 * @return UrlHandler 271 */ 272 public function setPath($path) 273 { 274 $this->path = $path; 275 return $this; 276 } 277 278 /** 279 * @return string 280 */ 281 public function getFragment() 282 { 283 return $this->fragment; 284 } 285 286 /** 287 * @param string $fragment 288 * 289 * @return UrlHandler 290 */ 291 public function setFragment($fragment) 292 { 293 $this->fragment = $fragment; 294 return $this; 295 } 296 }