Link.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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;
  41. $this->currentUri = $currentUri;
  42. }
  43. protected function setNode(\DOMNode $node)
  44. {
  45. if ('a' != $node->nodeName) {
  46. throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName));
  47. }
  48. $this->node = $node;
  49. }
  50. /**
  51. * Gets the node associated with this link.
  52. *
  53. * @return \DOMNode A \DOMNode instance
  54. */
  55. public function getNode()
  56. {
  57. return $this->node;
  58. }
  59. /**
  60. * Gets the URI associated with this link.
  61. *
  62. * @return string The URI
  63. *
  64. * @api
  65. */
  66. public function getUri()
  67. {
  68. $uri = $this->getRawUri();
  69. // absolute URL?
  70. if ('http' === substr($uri, 0, 4)) {
  71. return $uri;
  72. }
  73. // empty URI
  74. if (!$uri) {
  75. return $this->currentUri;
  76. }
  77. // only an anchor or a query string
  78. if (in_array($uri[0], array('?', '#'))) {
  79. return $this->currentUri.$uri;
  80. }
  81. // absolute path
  82. if ('/' === $uri[0]) {
  83. return preg_replace('#^(.*?//[^/]+)(?:\/.*)?$#', '$1', $this->currentUri).$uri;
  84. }
  85. // relative path
  86. return substr($this->currentUri, 0, strrpos($this->currentUri, '/') + 1).$uri;
  87. }
  88. protected function getRawUri()
  89. {
  90. return $this->node->getAttribute('href');
  91. }
  92. /**
  93. * Gets the method associated with this link.
  94. *
  95. * @return string The method
  96. *
  97. * @api
  98. */
  99. public function getMethod()
  100. {
  101. return $this->method;
  102. }
  103. }