Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Url implements ArrayAccess
- {
- const DEFAULT_PORT = 80;
- private $_protocol;
- private $_user;
- private $_password;
- private $_host;
- private $_port;
- private $_path;
- private $_query;
- private $_fragement;
- public function __construct ($arg_url)
- {
- $parts = parse_url($arg_url);
- if (array_key_exists('scheme', $parts))
- {
- $this->_protocol = $parts['scheme'];
- }
- if (array_key_exists('user', $parts))
- {
- $this->_user = $parts['user'];
- }
- if (array_key_exists('pass', $parts))
- {
- $this->_password = $parts['pass'];
- }
- if (array_key_exists('host', $parts))
- {
- $this->_host = $parts['host'];
- }
- if (array_key_exists('port', $parts))
- {
- $this->_port = $parts['port'];
- }
- if (array_key_exists('path', $parts))
- {
- $this->_path = $parts['path'];
- }
- if (array_key_exists('query', $parts))
- {
- parse_str($parts['query'], $this->_query);
- }
- if (array_key_exists('fragment', $parts))
- {
- $this->_fragment = $parts['fragment'];
- }
- }
- public function setProtocol ($arg_protocol)
- {
- $this->_protocol = $arg_protocol;
- return $this;
- }
- public function setUser ($arg_user)
- {
- $this->_user = $arg_user;
- return $this;
- }
- public function setPassword ($arg_password)
- {
- $this->_password = $arg_password;
- return $this;
- }
- public function setHost ($arg_host)
- {
- $this->_host = $arg_host;
- return $this;
- }
- public function setPort ($arg_port)
- {
- $this->_port = $arg_port;
- return $this;
- }
- public function setPath ($arg_path)
- {
- $this->_path = $arg_path;
- return $this;
- }
- public function setQuery ($arg_query)
- {
- $this->_query = $arg_query;
- return $this;
- }
- public function setFragment ($arg_fragment)
- {
- $this->_fragment = $arg_fragment;
- return $this;
- }
- public function setQueryParameter ($arg_key, $arg_value)
- {
- $this->_query[$arg_key] = $arg_value;
- return $this;
- }
- public function getQueryParameter ($arg_key)
- {
- return $this->_query[$arg_key];
- }
- public function hasQueryParameter ($arg_key)
- {
- return array_key_exists($arg_key, $this->_query);
- }
- public function removeQueryParameter ($arg_key)
- {
- return unset($this->_query[$arg_key])
- }
- public function offsetSet ($arg_key, $value)
- {
- $this->setQueryParameter($arg_key, $value);
- }
- public function offsetGet ($arg_key)
- {
- if ( ! $this->hasQueryParameter($arg_key))
- {
- throw new InvalidArgumentException(sprintf('Query parameter "%s" is not defined.', $arg_key));
- }
- return $this->getQueryParameter($arg_key, $value);;
- }
- public function offsetExists ($arg_key)
- {
- return $this->hasQueryParameter($arg_key);
- }
- public function offsetUnset ($arg_key)
- {
- $this->removeQueryParameter($arg_key);
- }
- public function __toString ()
- {
- $format = NULL !== $this->_protocol ? '%1$s://' : '';
- if (NULL !== $this->_user)
- {
- $format .= '%2$s';
- if (NULL !== $this->_password)
- {
- $format .= ':%3$s';
- }
- $format .= '@';
- }
- if (NULL !== $this->_host)
- {
- $format .= '%4$s';
- }
- if (NULL !== $this->_port && self::DEFAULT_PORT !== $this->_port)
- {
- $format .= ':%5$d';
- }
- if (NULL !== $this->_path)
- {
- $format .= '/%6$s';
- }
- if (NULL !== $this->_query)
- {
- $format .= '?%7$s';
- }
- if (NULL !== $this->_fragment)
- {
- $format .= '#%8$s';
- }
- 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);
- }
- }
Add Comment
Please, Sign In to add comment