PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Buzz/Util/Url.php

http://github.com/kriswallsmith/Buzz
PHP | 190 lines | 142 code | 26 blank | 22 comment | 15 complexity | 888ec065b1a992dd2e8062bd7ba9e679 MD5 | raw file
  1. <?php
  2. namespace Buzz\Util;
  3. use Buzz\Message\RequestInterface;
  4. use Buzz\Exception\InvalidArgumentException;
  5. class Url
  6. {
  7. private static $defaultPorts = array(
  8. 'http' => 80,
  9. 'https' => 443,
  10. );
  11. private $url;
  12. private $components;
  13. /**
  14. * Constructor.
  15. *
  16. * @param string $url The URL
  17. *
  18. * @throws InvalidArgumentException If the URL is invalid
  19. */
  20. public function __construct($url)
  21. {
  22. $components = parse_url($url);
  23. if (false === $components) {
  24. throw new InvalidArgumentException(sprintf('The URL "%s" is invalid.', $url));
  25. }
  26. // support scheme-less URLs
  27. if (!isset($components['host']) && isset($components['path'])) {
  28. $pos = strpos($components['path'], '/');
  29. if (false === $pos) {
  30. $components['host'] = $components['path'];
  31. unset($components['path']);
  32. } elseif (0 !== $pos) {
  33. list($host, $path) = explode('/', $components['path'], 2);
  34. $components['host'] = $host;
  35. $components['path'] = '/'.$path;
  36. }
  37. }
  38. // default port
  39. if (isset($components['scheme']) && !isset($components['port']) && isset(self::$defaultPorts[$components['scheme']])) {
  40. $components['port'] = self::$defaultPorts[$components['scheme']];
  41. }
  42. $this->url = $url;
  43. $this->components = $components;
  44. }
  45. public function getScheme()
  46. {
  47. return $this->parseUrl('scheme');
  48. }
  49. public function getHostname()
  50. {
  51. return $this->parseUrl('host');
  52. }
  53. public function getPort()
  54. {
  55. return $this->parseUrl('port');
  56. }
  57. public function getUser()
  58. {
  59. return $this->parseUrl('user');
  60. }
  61. public function getPassword()
  62. {
  63. return $this->parseUrl('pass');
  64. }
  65. public function getPath()
  66. {
  67. return $this->parseUrl('path');
  68. }
  69. public function getQueryString()
  70. {
  71. return $this->parseUrl('query');
  72. }
  73. public function getFragment()
  74. {
  75. return $this->parseUrl('fragment');
  76. }
  77. /**
  78. * Returns a host string that combines scheme, hostname and port.
  79. *
  80. * @return string A host value for an HTTP message
  81. */
  82. public function getHost()
  83. {
  84. if ($hostname = $this->parseUrl('host')) {
  85. $host = $scheme = $this->parseUrl('scheme', 'http');
  86. $host .= '://';
  87. $host .= $hostname;
  88. $port = $this->parseUrl('port');
  89. if ($port && (!isset(self::$defaultPorts[$scheme]) || self::$defaultPorts[$scheme] != $port)) {
  90. $host .= ':'.$port;
  91. }
  92. return $host;
  93. }
  94. }
  95. /**
  96. * Returns a resource string that combines path and query string.
  97. *
  98. * @return string A resource value for an HTTP message
  99. */
  100. public function getResource()
  101. {
  102. $resource = $this->parseUrl('path', '/');
  103. if ($query = $this->parseUrl('query')) {
  104. $resource .= '?'.$query;
  105. }
  106. return $resource;
  107. }
  108. /**
  109. * Returns a formatted URL.
  110. */
  111. public function format($pattern)
  112. {
  113. static $map = array(
  114. 's' => 'getScheme',
  115. 'u' => 'getUser',
  116. 'a' => 'getPassword',
  117. 'h' => 'getHostname',
  118. 'o' => 'getPort',
  119. 'p' => 'getPath',
  120. 'q' => 'getQueryString',
  121. 'f' => 'getFragment',
  122. 'H' => 'getHost',
  123. 'R' => 'getResource',
  124. );
  125. $url = '';
  126. $parts = str_split($pattern);
  127. while ($part = current($parts)) {
  128. if (isset($map[$part])) {
  129. $method = $map[$part];
  130. $url .= $this->$method();
  131. } elseif ('\\' == $part) {
  132. $url .= next($parts);
  133. } elseif (!ctype_alpha($part)) {
  134. $url .= $part;
  135. } else {
  136. throw new InvalidArgumentException(sprintf('The format character "%s" is invalid.', $part));
  137. }
  138. next($parts);
  139. }
  140. return $url;
  141. }
  142. /**
  143. * Applies the current URL to the supplied request.
  144. */
  145. public function applyToRequest(RequestInterface $request)
  146. {
  147. $request->setResource($this->getResource());
  148. $request->setHost($this->getHost());
  149. }
  150. private function parseUrl($component = null, $default = null)
  151. {
  152. if (null === $component) {
  153. return $this->components;
  154. } elseif (isset($this->components[$component])) {
  155. return $this->components[$component];
  156. } else {
  157. return $default;
  158. }
  159. }
  160. }