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