AbstractUriElement.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DomCrawler;
  11. /**
  12. * Any HTML element that can link to an URI.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. abstract class AbstractUriElement
  17. {
  18. /**
  19. * @var \DOMElement
  20. */
  21. protected $node;
  22. /**
  23. * @var string The method to use for the element
  24. */
  25. protected $method;
  26. /**
  27. * @var string The URI of the page where the element is embedded (or the base href)
  28. */
  29. protected $currentUri;
  30. /**
  31. * @param \DOMElement $node A \DOMElement instance
  32. * @param string $currentUri The URI of the page where the link is embedded (or the base href)
  33. * @param string $method The method to use for the link (get by default)
  34. *
  35. * @throws \InvalidArgumentException if the node is not a link
  36. */
  37. public function __construct(\DOMElement $node, $currentUri, $method = 'GET')
  38. {
  39. if (!\in_array(strtolower(substr($currentUri, 0, 4)), ['http', 'file'])) {
  40. throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri));
  41. }
  42. $this->setNode($node);
  43. $this->method = $method ? strtoupper($method) : null;
  44. $this->currentUri = $currentUri;
  45. }
  46. /**
  47. * Gets the node associated with this link.
  48. *
  49. * @return \DOMElement A \DOMElement instance
  50. */
  51. public function getNode()
  52. {
  53. return $this->node;
  54. }
  55. /**
  56. * Gets the method associated with this link.
  57. *
  58. * @return string The method
  59. */
  60. public function getMethod()
  61. {
  62. return $this->method;
  63. }
  64. /**
  65. * Gets the URI associated with this link.
  66. *
  67. * @return string The URI
  68. */
  69. public function getUri()
  70. {
  71. $uri = trim($this->getRawUri());
  72. // absolute URL?
  73. if (null !== parse_url($uri, PHP_URL_SCHEME)) {
  74. return $uri;
  75. }
  76. // empty URI
  77. if (!$uri) {
  78. return $this->currentUri;
  79. }
  80. // an anchor
  81. if ('#' === $uri[0]) {
  82. return $this->cleanupAnchor($this->currentUri).$uri;
  83. }
  84. $baseUri = $this->cleanupUri($this->currentUri);
  85. if ('?' === $uri[0]) {
  86. return $baseUri.$uri;
  87. }
  88. // absolute URL with relative schema
  89. if (0 === strpos($uri, '//')) {
  90. return preg_replace('#^([^/]*)//.*$#', '$1', $baseUri).$uri;
  91. }
  92. $baseUri = preg_replace('#^(.*?//[^/]*)(?:\/.*)?$#', '$1', $baseUri);
  93. // absolute path
  94. if ('/' === $uri[0]) {
  95. return $baseUri.$uri;
  96. }
  97. // relative path
  98. $path = parse_url(substr($this->currentUri, \strlen($baseUri)), PHP_URL_PATH);
  99. $path = $this->canonicalizePath(substr($path, 0, strrpos($path, '/')).'/'.$uri);
  100. return $baseUri.('' === $path || '/' !== $path[0] ? '/' : '').$path;
  101. }
  102. /**
  103. * Returns raw URI data.
  104. *
  105. * @return string
  106. */
  107. abstract protected function getRawUri();
  108. /**
  109. * Returns the canonicalized URI path (see RFC 3986, section 5.2.4).
  110. *
  111. * @param string $path URI path
  112. *
  113. * @return string
  114. */
  115. protected function canonicalizePath($path)
  116. {
  117. if ('' === $path || '/' === $path) {
  118. return $path;
  119. }
  120. if ('.' === substr($path, -1)) {
  121. $path .= '/';
  122. }
  123. $output = [];
  124. foreach (explode('/', $path) as $segment) {
  125. if ('..' === $segment) {
  126. array_pop($output);
  127. } elseif ('.' !== $segment) {
  128. $output[] = $segment;
  129. }
  130. }
  131. return implode('/', $output);
  132. }
  133. /**
  134. * Sets current \DOMElement instance.
  135. *
  136. * @param \DOMElement $node A \DOMElement instance
  137. *
  138. * @throws \LogicException If given node is not an anchor
  139. */
  140. abstract protected function setNode(\DOMElement $node);
  141. /**
  142. * Removes the query string and the anchor from the given uri.
  143. *
  144. * @param string $uri The uri to clean
  145. *
  146. * @return string
  147. */
  148. private function cleanupUri($uri)
  149. {
  150. return $this->cleanupQuery($this->cleanupAnchor($uri));
  151. }
  152. /**
  153. * Remove the query string from the uri.
  154. *
  155. * @param string $uri
  156. *
  157. * @return string
  158. */
  159. private function cleanupQuery($uri)
  160. {
  161. if (false !== $pos = strpos($uri, '?')) {
  162. return substr($uri, 0, $pos);
  163. }
  164. return $uri;
  165. }
  166. /**
  167. * Remove the anchor from the uri.
  168. *
  169. * @param string $uri
  170. *
  171. * @return string
  172. */
  173. private function cleanupAnchor($uri)
  174. {
  175. if (false !== $pos = strpos($uri, '#')) {
  176. return substr($uri, 0, $pos);
  177. }
  178. return $uri;
  179. }
  180. }