123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace Symfony\Component\DomCrawler;
- abstract class AbstractUriElement
- {
-
- protected $node;
-
- protected $method;
-
- protected $currentUri;
-
- public function __construct(\DOMElement $node, string $currentUri = null, ?string $method = 'GET')
- {
- $this->setNode($node);
- $this->method = $method ? strtoupper($method) : null;
- $this->currentUri = $currentUri;
- $elementUriIsRelative = null === parse_url(trim($this->getRawUri()), PHP_URL_SCHEME);
- $baseUriIsAbsolute = \in_array(strtolower(substr($this->currentUri, 0, 4)), ['http', 'file']);
- if ($elementUriIsRelative && !$baseUriIsAbsolute) {
- 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));
- }
- }
-
- public function getNode()
- {
- return $this->node;
- }
-
- public function getMethod()
- {
- return $this->method;
- }
-
- public function getUri()
- {
- $uri = trim($this->getRawUri());
-
- if (null !== parse_url($uri, PHP_URL_SCHEME)) {
- return $uri;
- }
-
- if (!$uri) {
- return $this->currentUri;
- }
-
- if ('#' === $uri[0]) {
- return $this->cleanupAnchor($this->currentUri).$uri;
- }
- $baseUri = $this->cleanupUri($this->currentUri);
- if ('?' === $uri[0]) {
- return $baseUri.$uri;
- }
-
- if (0 === strpos($uri, '//')) {
- return preg_replace('#^([^/]*)//.*$#', '$1', $baseUri).$uri;
- }
- $baseUri = preg_replace('#^(.*?//[^/]*)(?:\/.*)?$#', '$1', $baseUri);
-
- if ('/' === $uri[0]) {
- return $baseUri.$uri;
- }
-
- $path = parse_url(substr($this->currentUri, \strlen($baseUri)), PHP_URL_PATH);
- $path = $this->canonicalizePath(substr($path, 0, strrpos($path, '/')).'/'.$uri);
- return $baseUri.('' === $path || '/' !== $path[0] ? '/' : '').$path;
- }
-
- abstract protected function getRawUri();
-
- protected function canonicalizePath($path)
- {
- if ('' === $path || '/' === $path) {
- return $path;
- }
- if ('.' === substr($path, -1)) {
- $path .= '/';
- }
- $output = [];
- foreach (explode('/', $path) as $segment) {
- if ('..' === $segment) {
- array_pop($output);
- } elseif ('.' !== $segment) {
- $output[] = $segment;
- }
- }
- return implode('/', $output);
- }
-
- abstract protected function setNode(\DOMElement $node);
-
- private function cleanupUri(string $uri): string
- {
- return $this->cleanupQuery($this->cleanupAnchor($uri));
- }
-
- private function cleanupQuery(string $uri): string
- {
- if (false !== $pos = strpos($uri, '?')) {
- return substr($uri, 0, $pos);
- }
- return $uri;
- }
-
- private function cleanupAnchor(string $uri): string
- {
- if (false !== $pos = strpos($uri, '#')) {
- return substr($uri, 0, $pos);
- }
- return $uri;
- }
- }
|