PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/dom-crawler/Symfony/Component/DomCrawler/Link.php

https://bitbucket.org/vervcreations/projectis.at
PHP | 192 lines | 85 code | 27 blank | 80 comment | 15 complexity | fed6100bab111dbb3d90ce5dcc109ca3 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, BSD-3-Clause
  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. * Link represents an HTML link (an HTML a tag).
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @api
  17. */
  18. class Link
  19. {
  20. /**
  21. * @var \DOMNode A \DOMNode instance
  22. */
  23. protected $node;
  24. /**
  25. * @var string The method to use for the link
  26. */
  27. protected $method;
  28. /**
  29. * @var string The URI of the page where the link is embedded (or the base href)
  30. */
  31. protected $currentUri;
  32. /**
  33. * Constructor.
  34. *
  35. * @param \DOMNode $node A \DOMNode instance
  36. * @param string $currentUri The URI of the page where the link is embedded (or the base href)
  37. * @param string $method The method to use for the link (get by default)
  38. *
  39. * @throws \InvalidArgumentException if the node is not a link
  40. *
  41. * @api
  42. */
  43. public function __construct(\DOMNode $node, $currentUri, $method = 'GET')
  44. {
  45. if (!in_array(strtolower(substr($currentUri, 0, 4)), array('http', 'file'))) {
  46. throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri));
  47. }
  48. $this->setNode($node);
  49. $this->method = $method ? strtoupper($method) : null;
  50. $this->currentUri = $currentUri;
  51. }
  52. /**
  53. * Gets the node associated with this link.
  54. *
  55. * @return \DOMNode A \DOMNode instance
  56. */
  57. public function getNode()
  58. {
  59. return $this->node;
  60. }
  61. /**
  62. * Gets the method associated with this link.
  63. *
  64. * @return string The method
  65. *
  66. * @api
  67. */
  68. public function getMethod()
  69. {
  70. return $this->method;
  71. }
  72. /**
  73. * Gets the URI associated with this link.
  74. *
  75. * @return string The URI
  76. *
  77. * @api
  78. */
  79. public function getUri()
  80. {
  81. $uri = trim($this->getRawUri());
  82. // absolute URL?
  83. if (null !== parse_url($uri, PHP_URL_SCHEME)) {
  84. return $uri;
  85. }
  86. // empty URI
  87. if (!$uri) {
  88. return $this->currentUri;
  89. }
  90. // only an anchor
  91. if ('#' === $uri[0]) {
  92. $baseUri = $this->currentUri;
  93. if (false !== $pos = strpos($baseUri, '#')) {
  94. $baseUri = substr($baseUri, 0, $pos);
  95. }
  96. return $baseUri.$uri;
  97. }
  98. // only a query string
  99. if ('?' === $uri[0]) {
  100. $baseUri = $this->currentUri;
  101. // remove the query string from the current uri
  102. if (false !== $pos = strpos($baseUri, '?')) {
  103. $baseUri = substr($baseUri, 0, $pos);
  104. }
  105. return $baseUri.$uri;
  106. }
  107. $baseUri = preg_replace('#^(.*?//[^/]+)(?:\/.*)?$#', '$1', $this->currentUri);
  108. // absolute path
  109. if ('/' === $uri[0]) {
  110. return $baseUri.$uri;
  111. }
  112. // relative path
  113. $path = parse_url(substr($this->currentUri, strlen($baseUri)), PHP_URL_PATH);
  114. $path = $this->canonicalizePath(substr($path, 0, strrpos($path, '/')).'/'.$uri);
  115. return $baseUri.('' === $path || '/' !== $path[0] ? '/' : '').$path;
  116. }
  117. /**
  118. * Returns raw uri data.
  119. *
  120. * @return string
  121. */
  122. protected function getRawUri()
  123. {
  124. return $this->node->getAttribute('href');
  125. }
  126. /**
  127. * Returns the canonicalized URI path (see RFC 3986, section 5.2.4)
  128. *
  129. * @param string $path URI path
  130. *
  131. * @return string
  132. */
  133. protected function canonicalizePath($path)
  134. {
  135. if ('' === $path || '/' === $path) {
  136. return $path;
  137. }
  138. if ('.' === substr($path, -1)) {
  139. $path = $path.'/';
  140. }
  141. $output = array();
  142. foreach (explode('/', $path) as $segment) {
  143. if ('..' === $segment) {
  144. array_pop($output);
  145. } elseif ('.' !== $segment) {
  146. array_push($output, $segment);
  147. }
  148. }
  149. return implode('/', $output);
  150. }
  151. /**
  152. * Sets current \DOMNode instance.
  153. *
  154. * @param \DOMNode $node A \DOMNode instance
  155. *
  156. * @throws \LogicException If given node is not an anchor
  157. */
  158. protected function setNode(\DOMNode $node)
  159. {
  160. if ('a' != $node->nodeName) {
  161. throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName));
  162. }
  163. $this->node = $node;
  164. }
  165. }