This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DomCrawler/Link.php
Fabien Potencier f26c062c00 Merge branch '2.3' into 2.5
* 2.3:
  [2.3] CS And DocBlock Fixes
  [2.3] CS Fixes

Conflicts:
	src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php
	src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php
	src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php
	src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php
	src/Symfony/Component/Config/Definition/ReferenceDumper.php
	src/Symfony/Component/Console/Application.php
	src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
	src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
	src/Symfony/Component/Form/Extension/Csrf/EventListener/CsrfValidationListener.php
	src/Symfony/Component/Form/FormError.php
	src/Symfony/Component/HttpFoundation/Request.php
	src/Symfony/Component/HttpFoundation/Response.php
	src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php
	src/Symfony/Component/Process/ProcessUtils.php
	src/Symfony/Component/PropertyAccess/PropertyAccessor.php
	src/Symfony/Component/PropertyAccess/PropertyAccessorInterface.php
	src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
	src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php
	src/Symfony/Component/Validator/Constraints/GroupSequence.php
	src/Symfony/Component/Validator/Mapping/ClassMetadata.php
	src/Symfony/Component/Validator/Mapping/ClassMetadataFactory.php
	src/Symfony/Component/Validator/Mapping/MemberMetadata.php
	src/Symfony/Component/Validator/Tests/Fixtures/StubGlobalExecutionContext.php
2014-12-22 17:29:52 +01:00

233 lines
5.3 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DomCrawler;
/**
* Link represents an HTML link (an HTML a or area tag).
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
*/
class Link
{
/**
* @var \DOMElement
*/
protected $node;
/**
* @var string The method to use for the link
*/
protected $method;
/**
* @var string The URI of the page where the link is embedded (or the base href)
*/
protected $currentUri;
/**
* Constructor.
*
* @param \DOMElement $node A \DOMElement instance
* @param string $currentUri The URI of the page where the link is embedded (or the base href)
* @param string $method The method to use for the link (get by default)
*
* @throws \InvalidArgumentException if the node is not a link
*
* @api
*/
public function __construct(\DOMElement $node, $currentUri, $method = 'GET')
{
if (!in_array(strtolower(substr($currentUri, 0, 4)), array('http', 'file'))) {
throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri));
}
$this->setNode($node);
$this->method = $method ? strtoupper($method) : null;
$this->currentUri = $currentUri;
}
/**
* Gets the node associated with this link.
*
* @return \DOMElement A \DOMElement instance
*/
public function getNode()
{
return $this->node;
}
/**
* Gets the method associated with this link.
*
* @return string The method
*
* @api
*/
public function getMethod()
{
return $this->method;
}
/**
* Gets the URI associated with this link.
*
* @return string The URI
*
* @api
*/
public function getUri()
{
$uri = trim($this->getRawUri());
// absolute URL?
if (null !== parse_url($uri, PHP_URL_SCHEME)) {
return $uri;
}
// empty URI
if (!$uri) {
return $this->currentUri;
}
// an anchor
if ('#' === $uri[0]) {
return $this->cleanupAnchor($this->currentUri).$uri;
}
$baseUri = $this->cleanupUri($this->currentUri);
if ('?' === $uri[0]) {
return $baseUri.$uri;
}
// absolute URL with relative schema
if (0 === strpos($uri, '//')) {
return preg_replace('#^([^/]*)//.*$#', '$1', $baseUri).$uri;
}
$baseUri = preg_replace('#^(.*?//[^/]*)(?:\/.*)?$#', '$1', $baseUri);
// absolute path
if ('/' === $uri[0]) {
return $baseUri.$uri;
}
// relative path
$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;
}
/**
* Returns raw URI data.
*
* @return string
*/
protected function getRawUri()
{
return $this->node->getAttribute('href');
}
/**
* Returns the canonicalized URI path (see RFC 3986, section 5.2.4).
*
* @param string $path URI path
*
* @return string
*/
protected function canonicalizePath($path)
{
if ('' === $path || '/' === $path) {
return $path;
}
if ('.' === substr($path, -1)) {
$path = $path.'/';
}
$output = array();
foreach (explode('/', $path) as $segment) {
if ('..' === $segment) {
array_pop($output);
} elseif ('.' !== $segment) {
array_push($output, $segment);
}
}
return implode('/', $output);
}
/**
* Sets current \DOMElement instance.
*
* @param \DOMElement $node A \DOMElement instance
*
* @throws \LogicException If given node is not an anchor
*/
protected function setNode(\DOMElement $node)
{
if ('a' !== $node->nodeName && 'area' !== $node->nodeName) {
throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName));
}
$this->node = $node;
}
/**
* Removes the query string and the anchor from the given uri.
*
* @param string $uri The uri to clean
*
* @return string
*/
private function cleanupUri($uri)
{
return $this->cleanupQuery($this->cleanupAnchor($uri));
}
/**
* Remove the query string from the uri.
*
* @param string $uri
*
* @return string
*/
private function cleanupQuery($uri)
{
if (false !== $pos = strpos($uri, '?')) {
return substr($uri, 0, $pos);
}
return $uri;
}
/**
* Remove the anchor from the uri.
*
* @param string $uri
*
* @return string
*/
private function cleanupAnchor($uri)
{
if (false !== $pos = strpos($uri, '#')) {
return substr($uri, 0, $pos);
}
return $uri;
}
}