AbstractUriElement.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * Any HTML element that can link to an URI.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. abstract class AbstractUriElement
  17. {
  18. /**
  19. * @var \DOMElement
  20. */
  21. protected $node;
  22. /**
  23. * @var string|null The method to use for the element
  24. */
  25. protected $method;
  26. /**
  27. * @var string The URI of the page where the element is embedded (or the base href)
  28. */
  29. protected $currentUri;
  30. /**
  31. * @param \DOMElement $node A \DOMElement instance
  32. * @param string $currentUri The URI of the page where the link is embedded (or the base href)
  33. * @param string|null $method The method to use for the link (GET by default)
  34. *
  35. * @throws \InvalidArgumentException if the node is not a link
  36. */
  37. public function __construct(\DOMElement $node, string $currentUri = null, ?string $method = 'GET')
  38. {
  39. $this->setNode($node);
  40. $this->method = $method ? strtoupper($method) : null;
  41. $this->currentUri = $currentUri;
  42. $elementUriIsRelative = null === parse_url(trim($this->getRawUri()), \PHP_URL_SCHEME);
  43. $baseUriIsAbsolute = \in_array(strtolower(substr($this->currentUri, 0, 4)), ['http', 'file']);
  44. if ($elementUriIsRelative && !$baseUriIsAbsolute) {
  45. throw new \InvalidArgumentException(sprintf('The URL of the element is relative, so you must define its base URI passing an absolute URL to the constructor of the "%s" class ("%s" was passed).', __CLASS__, $this->currentUri));
  46. }
  47. }
  48. /**
  49. * Gets the node associated with this link.
  50. *
  51. * @return \DOMElement A \DOMElement instance
  52. */
  53. public function getNode()
  54. {
  55. return $this->node;
  56. }
  57. /**
  58. * Gets the method associated with this link.
  59. *
  60. * @return string The method
  61. */
  62. public function getMethod()
  63. {
  64. return $this->method ?? 'GET';
  65. }
  66. /**
  67. * Gets the URI associated with this link.
  68. *
  69. * @return string The URI
  70. */
  71. public function getUri()
  72. {
  73. return UriResolver::resolve($this->getRawUri(), $this->currentUri);
  74. }
  75. /**
  76. * Returns raw URI data.
  77. *
  78. * @return string
  79. */
  80. abstract protected function getRawUri();
  81. /**
  82. * Returns the canonicalized URI path (see RFC 3986, section 5.2.4).
  83. *
  84. * @param string $path URI path
  85. *
  86. * @return string
  87. */
  88. protected function canonicalizePath(string $path)
  89. {
  90. if ('' === $path || '/' === $path) {
  91. return $path;
  92. }
  93. if ('.' === substr($path, -1)) {
  94. $path .= '/';
  95. }
  96. $output = [];
  97. foreach (explode('/', $path) as $segment) {
  98. if ('..' === $segment) {
  99. array_pop($output);
  100. } elseif ('.' !== $segment) {
  101. $output[] = $segment;
  102. }
  103. }
  104. return implode('/', $output);
  105. }
  106. /**
  107. * Sets current \DOMElement instance.
  108. *
  109. * @param \DOMElement $node A \DOMElement instance
  110. *
  111. * @throws \LogicException If given node is not an anchor
  112. */
  113. abstract protected function setNode(\DOMElement $node);
  114. }