4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DomCrawler;
15 * Any HTML element that can link to an URI.
17 * @author Fabien Potencier <fabien@symfony.com>
19 abstract class AbstractUriElement
27 * @var string The method to use for the element
32 * @var string The URI of the page where the element is embedded (or the base href)
34 protected $currentUri;
37 * @param \DOMElement $node A \DOMElement instance
38 * @param string $currentUri The URI of the page where the link is embedded (or the base href)
39 * @param string $method The method to use for the link (get by default)
41 * @throws \InvalidArgumentException if the node is not a link
43 public function __construct(\DOMElement $node, $currentUri, $method = 'GET')
45 if (!in_array(strtolower(substr($currentUri, 0, 4)), array('http', 'file'))) {
46 throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri));
49 $this->setNode($node);
50 $this->method = $method ? strtoupper($method) : null;
51 $this->currentUri = $currentUri;
55 * Gets the node associated with this link.
57 * @return \DOMElement A \DOMElement instance
59 public function getNode()
65 * Gets the method associated with this link.
67 * @return string The method
69 public function getMethod()
75 * Gets the URI associated with this link.
77 * @return string The URI
79 public function getUri()
81 $uri = trim($this->getRawUri());
84 if (null !== parse_url($uri, PHP_URL_SCHEME)) {
90 return $this->currentUri;
94 if ('#' === $uri[0]) {
95 return $this->cleanupAnchor($this->currentUri).$uri;
98 $baseUri = $this->cleanupUri($this->currentUri);
100 if ('?' === $uri[0]) {
101 return $baseUri.$uri;
104 // absolute URL with relative schema
105 if (0 === strpos($uri, '//')) {
106 return preg_replace('#^([^/]*)//.*$#', '$1', $baseUri).$uri;
109 $baseUri = preg_replace('#^(.*?//[^/]*)(?:\/.*)?$#', '$1', $baseUri);
112 if ('/' === $uri[0]) {
113 return $baseUri.$uri;
117 $path = parse_url(substr($this->currentUri, strlen($baseUri)), PHP_URL_PATH);
118 $path = $this->canonicalizePath(substr($path, 0, strrpos($path, '/')).'/'.$uri);
120 return $baseUri.('' === $path || '/' !== $path[0] ? '/' : '').$path;
124 * Returns raw URI data.
128 abstract protected function getRawUri();
131 * Returns the canonicalized URI path (see RFC 3986, section 5.2.4).
133 * @param string $path URI path
137 protected function canonicalizePath($path)
139 if ('' === $path || '/' === $path) {
143 if ('.' === substr($path, -1)) {
149 foreach (explode('/', $path) as $segment) {
150 if ('..' === $segment) {
152 } elseif ('.' !== $segment) {
153 $output[] = $segment;
157 return implode('/', $output);
161 * Sets current \DOMElement instance.
163 * @param \DOMElement $node A \DOMElement instance
165 * @throws \LogicException If given node is not an anchor
167 abstract protected function setNode(\DOMElement $node);
170 * Removes the query string and the anchor from the given uri.
172 * @param string $uri The uri to clean
176 private function cleanupUri($uri)
178 return $this->cleanupQuery($this->cleanupAnchor($uri));
182 * Remove the query string from the uri.
188 private function cleanupQuery($uri)
190 if (false !== $pos = strpos($uri, '?')) {
191 return substr($uri, 0, $pos);
198 * Remove the anchor from the uri.
204 private function cleanupAnchor($uri)
206 if (false !== $pos = strpos($uri, '#')) {
207 return substr($uri, 0, $pos);