2010-04-15 13:41:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-24 00:22:16 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-04-15 13:41:42 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-04-15 13:41:42 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\DomCrawler;
|
|
|
|
|
2010-04-15 13:41:42 +01:00
|
|
|
/**
|
|
|
|
* Link represents an HTML link (an HTML a tag).
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-03-24 09:00:10 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-04-15 13:41:42 +01:00
|
|
|
*/
|
|
|
|
class Link
|
|
|
|
{
|
2011-04-23 15:25:21 +01:00
|
|
|
protected $node;
|
|
|
|
protected $method;
|
|
|
|
protected $currentUri;
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-04-23 15:25:21 +01:00
|
|
|
* @param \DOMNode $node A \DOMNode 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)
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @throws \LogicException if the node is not a link
|
2011-03-24 09:00:10 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-05-25 14:38:02 +01:00
|
|
|
public function __construct(\DOMNode $node, $currentUri, $method = 'GET')
|
2011-04-23 15:25:21 +01:00
|
|
|
{
|
|
|
|
if (!in_array(substr($currentUri, 0, 4), array('http', 'file'))) {
|
2011-04-27 15:04:13 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri));
|
2011-04-23 15:25:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->setNode($node);
|
2011-05-25 14:38:02 +01:00
|
|
|
$this->method = $method ? strtoupper($method) : null;
|
2011-04-23 15:25:21 +01:00
|
|
|
$this->currentUri = $currentUri;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setNode(\DOMNode $node)
|
2010-04-15 13:41:42 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if ('a' != $node->nodeName) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->node = $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the node associated with this link.
|
|
|
|
*
|
|
|
|
* @return \DOMNode A \DOMNode instance
|
|
|
|
*/
|
|
|
|
public function getNode()
|
|
|
|
{
|
|
|
|
return $this->node;
|
2010-04-15 13:41:42 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets the URI associated with this link.
|
|
|
|
*
|
|
|
|
* @return string The URI
|
2011-03-24 09:00:10 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-04-23 15:25:21 +01:00
|
|
|
public function getUri()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-04-23 15:25:21 +01:00
|
|
|
$uri = $this->getRawUri();
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
// absolute URL?
|
|
|
|
if ('http' === substr($uri, 0, 4)) {
|
|
|
|
return $uri;
|
2011-04-08 16:52:43 +01:00
|
|
|
}
|
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
// empty URI
|
|
|
|
if (!$uri) {
|
|
|
|
return $this->currentUri;
|
2011-04-08 16:52:43 +01:00
|
|
|
}
|
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
// only an anchor or a query string
|
|
|
|
if (in_array($uri[0], array('?', '#'))) {
|
|
|
|
return $this->currentUri.$uri;
|
2010-09-02 17:56:55 +01:00
|
|
|
}
|
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
// absolute path
|
|
|
|
if ('/' === $uri[0]) {
|
|
|
|
return preg_replace('#^(.*?//[^/]+)(?:\/.*)?$#', '$1', $this->currentUri).$uri;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
// relative path
|
|
|
|
return substr($this->currentUri, 0, strrpos($this->currentUri, '/') + 1).$uri;
|
|
|
|
}
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
protected function getRawUri()
|
|
|
|
{
|
|
|
|
return $this->node->getAttribute('href');
|
2010-04-15 13:41:42 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets the method associated with this link.
|
|
|
|
*
|
|
|
|
* @return string The method
|
2011-03-24 09:00:10 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getMethod()
|
2010-04-15 13:41:42 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->method;
|
2010-04-15 13:41:42 +01:00
|
|
|
}
|
|
|
|
}
|