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

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

https://bitbucket.org/kim_yongwoon/rentcar
PHP | 157 lines | 64 code | 20 blank | 73 comment | 10 complexity | 6c3354de8a50664c6ddadf5d4009a1ae MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-3.0, LGPL-3.0, BSD-3-Clause, BSD-2-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. // absolute path
  108. if ('/' === $uri[0]) {
  109. return preg_replace('#^(.*?//[^/]+)(?:\/.*)?$#', '$1', $this->currentUri).$uri;
  110. }
  111. // relative path
  112. return substr($this->currentUri, 0, strrpos($this->currentUri, '/') + 1).$uri;
  113. }
  114. /**
  115. * Returns raw uri data.
  116. *
  117. * @return string
  118. */
  119. protected function getRawUri()
  120. {
  121. return $this->node->getAttribute('href');
  122. }
  123. /**
  124. * Sets current \DOMNode instance.
  125. *
  126. * @param \DOMNode $node A \DOMNode instance
  127. *
  128. * @throws \LogicException If given node is not an anchor
  129. */
  130. protected function setNode(\DOMNode $node)
  131. {
  132. if ('a' != $node->nodeName) {
  133. throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName));
  134. }
  135. $this->node = $node;
  136. }
  137. }