PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Components/DomCrawler/Link.php

https://github.com/come/symfony
PHP | 92 lines | 39 code | 11 blank | 42 comment | 8 complexity | 33900a445b30fe894ff82e62cafe8dee MD5 | raw file
Possible License(s): ISC
  1. <?php
  2. namespace Symfony\Components\DomCrawler;
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Link represents an HTML link (an HTML a tag).
  13. *
  14. * @package Symfony
  15. * @subpackage Components_DomCrawler
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. */
  18. class Link
  19. {
  20. protected $node;
  21. protected $method;
  22. protected $host;
  23. protected $path;
  24. /**
  25. * Constructor.
  26. *
  27. * @param \DOMNode $node A \DOMNode instance
  28. * @param string $method The method to use for the link (get by default)
  29. * @param string $host The base URI to use for absolute links (like http://localhost)
  30. * @param string $path The base path for relative links (/ by default)
  31. *
  32. * @throws \LogicException if the node is not a link
  33. */
  34. public function __construct(\DOMNode $node, $method = 'get', $host = null, $path = '/')
  35. {
  36. if ('a' != $node->nodeName) {
  37. throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName));
  38. }
  39. $this->node = $node;
  40. $this->method = $method;
  41. $this->host = $host;
  42. $this->path = empty($path) ? '/' : $path;
  43. }
  44. /**
  45. * Gets the node associated with this link.
  46. *
  47. * @return \DOMNode A \DOMNode instance
  48. */
  49. public function getNode()
  50. {
  51. return $this->node;
  52. }
  53. /**
  54. * Gets the URI associated with this link.
  55. *
  56. * @param Boolean $absolute Whether to return an absolute URI or not (this only works if a base URI has been provided)
  57. *
  58. * @return string The URI
  59. */
  60. public function getUri($absolute = true)
  61. {
  62. $uri = $this->node->getAttribute('href');
  63. $urlHaveScheme = 'http' === substr($uri, 0, 4);
  64. if ($uri && '/' !== $uri[0] && !$urlHaveScheme) {
  65. $uri = $this->path.$uri;
  66. }
  67. if ($absolute && null !== $this->host && !$urlHaveScheme) {
  68. return $this->host.$uri;
  69. }
  70. return $uri;
  71. }
  72. /**
  73. * Gets the method associated with this link.
  74. *
  75. * @return string The method
  76. */
  77. public function getMethod()
  78. {
  79. return $this->method;
  80. }
  81. }