PageRenderTime 41ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/src/Symfony/Component/DomCrawler/Link.php

https://bitbucket.org/Leimz/leimzwebsite
PHP | 136 lines | 64 code | 20 blank | 52 comment | 10 complexity | f9973de6c25bacc1a1ed7fbc68ac8480 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause, 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. protected $node;
  21. protected $method;
  22. protected $currentUri;
  23. /**
  24. * Constructor.
  25. *
  26. * @param \DOMNode $node A \DOMNode instance
  27. * @param string $currentUri The URI of the page where the link is embedded (or the base href)
  28. * @param string $method The method to use for the link (get by default)
  29. *
  30. * @throws \LogicException if the node is not a link
  31. *
  32. * @api
  33. */
  34. public function __construct(\DOMNode $node, $currentUri, $method = 'GET')
  35. {
  36. if (!in_array(substr($currentUri, 0, 4), array('http', 'file'))) {
  37. throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri));
  38. }
  39. $this->setNode($node);
  40. $this->method = $method ? strtoupper($method) : null;
  41. $this->currentUri = $currentUri;
  42. }
  43. /**
  44. * Gets the node associated with this link.
  45. *
  46. * @return \DOMNode A \DOMNode instance
  47. */
  48. public function getNode()
  49. {
  50. return $this->node;
  51. }
  52. /**
  53. * Gets the method associated with this link.
  54. *
  55. * @return string The method
  56. *
  57. * @api
  58. */
  59. public function getMethod()
  60. {
  61. return $this->method;
  62. }
  63. /**
  64. * Gets the URI associated with this link.
  65. *
  66. * @return string The URI
  67. *
  68. * @api
  69. */
  70. public function getUri()
  71. {
  72. $uri = trim($this->getRawUri());
  73. // absolute URL?
  74. if ('http' === substr($uri, 0, 4)) {
  75. return $uri;
  76. }
  77. // empty URI
  78. if (!$uri) {
  79. return $this->currentUri;
  80. }
  81. // only an anchor
  82. if ('#' === $uri[0]) {
  83. $baseUri = $this->currentUri;
  84. if (false !== $pos = strpos($baseUri, '#')) {
  85. $baseUri = substr($baseUri, 0, $pos);
  86. }
  87. return $baseUri.$uri;
  88. }
  89. // only a query string
  90. if ('?' === $uri[0]) {
  91. $baseUri = $this->currentUri;
  92. // remove the query string from the current uri
  93. if (false !== $pos = strpos($baseUri, '?')) {
  94. $baseUri = substr($baseUri, 0, $pos);
  95. }
  96. return $baseUri.$uri;
  97. }
  98. // absolute path
  99. if ('/' === $uri[0]) {
  100. return preg_replace('#^(.*?//[^/]+)(?:\/.*)?$#', '$1', $this->currentUri).$uri;
  101. }
  102. // relative path
  103. return substr($this->currentUri, 0, strrpos($this->currentUri, '/') + 1).$uri;
  104. }
  105. protected function getRawUri()
  106. {
  107. return $this->node->getAttribute('href');
  108. }
  109. protected function setNode(\DOMNode $node)
  110. {
  111. if ('a' != $node->nodeName) {
  112. throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName));
  113. }
  114. $this->node = $node;
  115. }
  116. }