PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/avalanche123/symfony
PHP | 117 lines | 53 code | 15 blank | 49 comment | 14 complexity | 9c2c8ffc86d34db6424af669ef13ba99 MD5 | raw file
  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. private $node;
  21. private $method;
  22. private $host;
  23. private $path;
  24. private $base;
  25. /**
  26. * Constructor.
  27. *
  28. * @param \DOMNode $node A \DOMNode instance
  29. * @param string $method The method to use for the link (get by default)
  30. * @param string $host The base URI to use for absolute links (like http://localhost)
  31. * @param string $path The base path for relative links (/ by default)
  32. * @param strin $base An optional base href for generating the uri
  33. *
  34. * @throws \LogicException if the node is not a link
  35. *
  36. * @api
  37. */
  38. public function __construct(\DOMNode $node, $method = 'get', $host = null, $path = '/', $base = null)
  39. {
  40. if ('a' != $node->nodeName) {
  41. throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName));
  42. }
  43. $this->node = $node;
  44. $this->method = $method;
  45. $this->host = $host;
  46. $this->path = empty($path) ? '/' : $path;
  47. $this->base = $base;
  48. }
  49. /**
  50. * Gets the node associated with this link.
  51. *
  52. * @return \DOMNode A \DOMNode instance
  53. */
  54. public function getNode()
  55. {
  56. return $this->node;
  57. }
  58. /**
  59. * Gets the URI associated with this link.
  60. *
  61. * @param Boolean $absolute Whether to return an absolute URI or not (this only works if a base URI has been provided)
  62. *
  63. * @return string The URI
  64. *
  65. * @api
  66. */
  67. public function getUri($absolute = true)
  68. {
  69. $uri = $this->node->getAttribute('href');
  70. $urlHaveScheme = 'http' === substr($uri, 0, 4);
  71. if (!$uri) {
  72. $uri = $this->path;
  73. }
  74. if ('#' === $uri[0]) {
  75. $uri = $this->path.$uri;
  76. }
  77. $path = $this->path;
  78. if ('?' !== substr($uri, 0, 1) && '/' !== substr($path, -1)) {
  79. $path = substr($path, 0, strrpos($path, '/') + 1);
  80. }
  81. if (!$this->base && $uri && '/' !== $uri[0] && !$urlHaveScheme) {
  82. $uri = $path.$uri;
  83. } elseif ($this->base) {
  84. $uri = $this->base.$uri;
  85. }
  86. if (!$this->base && $absolute && null !== $this->host && !$urlHaveScheme) {
  87. return $this->host.$uri;
  88. }
  89. return $uri;
  90. }
  91. /**
  92. * Gets the method associated with this link.
  93. *
  94. * @return string The method
  95. *
  96. * @api
  97. */
  98. public function getMethod()
  99. {
  100. return $this->method;
  101. }
  102. }