Guest User

Untitled

a guest
Aug 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. <?php
  2.  
  3. class Url implements ArrayAccess
  4. {
  5.  
  6. const DEFAULT_PORT = 80;
  7.  
  8. private $_protocol;
  9. private $_user;
  10. private $_password;
  11. private $_host;
  12. private $_port;
  13. private $_path;
  14. private $_query;
  15. private $_fragement;
  16.  
  17. public function __construct ($arg_url)
  18. {
  19. $parts = parse_url($arg_url);
  20.  
  21. if (array_key_exists('scheme', $parts))
  22. {
  23. $this->_protocol = $parts['scheme'];
  24. }
  25.  
  26. if (array_key_exists('user', $parts))
  27. {
  28. $this->_user = $parts['user'];
  29. }
  30.  
  31. if (array_key_exists('pass', $parts))
  32. {
  33. $this->_password = $parts['pass'];
  34. }
  35.  
  36. if (array_key_exists('host', $parts))
  37. {
  38. $this->_host = $parts['host'];
  39. }
  40.  
  41. if (array_key_exists('port', $parts))
  42. {
  43. $this->_port = $parts['port'];
  44. }
  45.  
  46. if (array_key_exists('path', $parts))
  47. {
  48. $this->_path = $parts['path'];
  49. }
  50.  
  51. if (array_key_exists('query', $parts))
  52. {
  53. parse_str($parts['query'], $this->_query);
  54. }
  55.  
  56. if (array_key_exists('fragment', $parts))
  57. {
  58. $this->_fragment = $parts['fragment'];
  59. }
  60. }
  61.  
  62. public function setProtocol ($arg_protocol)
  63. {
  64. $this->_protocol = $arg_protocol;
  65.  
  66. return $this;
  67. }
  68.  
  69. public function setUser ($arg_user)
  70. {
  71. $this->_user = $arg_user;
  72.  
  73. return $this;
  74. }
  75.  
  76. public function setPassword ($arg_password)
  77. {
  78. $this->_password = $arg_password;
  79.  
  80. return $this;
  81. }
  82.  
  83. public function setHost ($arg_host)
  84. {
  85. $this->_host = $arg_host;
  86.  
  87. return $this;
  88. }
  89.  
  90. public function setPort ($arg_port)
  91. {
  92. $this->_port = $arg_port;
  93.  
  94. return $this;
  95. }
  96.  
  97. public function setPath ($arg_path)
  98. {
  99. $this->_path = $arg_path;
  100.  
  101. return $this;
  102. }
  103.  
  104. public function setQuery ($arg_query)
  105. {
  106. $this->_query = $arg_query;
  107.  
  108. return $this;
  109. }
  110.  
  111. public function setFragment ($arg_fragment)
  112. {
  113. $this->_fragment = $arg_fragment;
  114.  
  115. return $this;
  116. }
  117.  
  118. public function setQueryParameter ($arg_key, $arg_value)
  119. {
  120. $this->_query[$arg_key] = $arg_value;
  121.  
  122. return $this;
  123. }
  124.  
  125. public function getQueryParameter ($arg_key)
  126. {
  127. return $this->_query[$arg_key];
  128. }
  129.  
  130. public function hasQueryParameter ($arg_key)
  131. {
  132. return array_key_exists($arg_key, $this->_query);
  133. }
  134.  
  135. public function removeQueryParameter ($arg_key)
  136. {
  137. return unset($this->_query[$arg_key])
  138. }
  139.  
  140. public function offsetSet ($arg_key, $value)
  141. {
  142. $this->setQueryParameter($arg_key, $value);
  143. }
  144.  
  145. public function offsetGet ($arg_key)
  146. {
  147. if ( ! $this->hasQueryParameter($arg_key))
  148. {
  149. throw new InvalidArgumentException(sprintf('Query parameter "%s" is not defined.', $arg_key));
  150. }
  151.  
  152. return $this->getQueryParameter($arg_key, $value);;
  153. }
  154.  
  155. public function offsetExists ($arg_key)
  156. {
  157. return $this->hasQueryParameter($arg_key);
  158. }
  159.  
  160. public function offsetUnset ($arg_key)
  161. {
  162. $this->removeQueryParameter($arg_key);
  163. }
  164.  
  165. public function __toString ()
  166. {
  167. $format = NULL !== $this->_protocol ? '%1$s://' : '';
  168.  
  169. if (NULL !== $this->_user)
  170. {
  171. $format .= '%2$s';
  172.  
  173. if (NULL !== $this->_password)
  174. {
  175. $format .= ':%3$s';
  176. }
  177.  
  178. $format .= '@';
  179. }
  180.  
  181. if (NULL !== $this->_host)
  182. {
  183. $format .= '%4$s';
  184. }
  185.  
  186. if (NULL !== $this->_port && self::DEFAULT_PORT !== $this->_port)
  187. {
  188. $format .= ':%5$d';
  189. }
  190.  
  191. if (NULL !== $this->_path)
  192. {
  193. $format .= '/%6$s';
  194. }
  195.  
  196. if (NULL !== $this->_query)
  197. {
  198. $format .= '?%7$s';
  199. }
  200.  
  201. if (NULL !== $this->_fragment)
  202. {
  203. $format .= '#%8$s';
  204. }
  205.  
  206. return sprintf($format, $this->_protocol, $this->_user, $this->_password, $this->_host, $this->_port, $this->_path, http_build_query($this->_query, NULL, $this->_seperator), $this->_fragment);
  207. }
  208.  
  209. }
Add Comment
Please, Sign In to add comment