|
0
|
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 |
|
|
|
12 |
namespace Symfony\Component\DomCrawler; |
|
|
13 |
|
|
|
14 |
/** |
|
|
15 |
* Link represents an HTML link (an HTML a tag). |
|
|
16 |
* |
|
|
17 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
18 |
* |
|
|
19 |
* @api |
|
|
20 |
*/ |
|
|
21 |
class Link |
|
|
22 |
{ |
|
|
23 |
protected $node; |
|
|
24 |
protected $method; |
|
|
25 |
protected $currentUri; |
|
|
26 |
|
|
|
27 |
/** |
|
|
28 |
* Constructor. |
|
|
29 |
* |
|
|
30 |
* @param \DOMNode $node A \DOMNode instance |
|
|
31 |
* @param string $currentUri The URI of the page where the link is embedded (or the base href) |
|
|
32 |
* @param string $method The method to use for the link (get by default) |
|
|
33 |
* |
|
|
34 |
* @throws \LogicException if the node is not a link |
|
|
35 |
* |
|
|
36 |
* @api |
|
|
37 |
*/ |
|
|
38 |
public function __construct(\DOMNode $node, $currentUri, $method = 'GET') |
|
|
39 |
{ |
|
|
40 |
if (!in_array(substr($currentUri, 0, 4), array('http', 'file'))) { |
|
|
41 |
throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri)); |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
$this->setNode($node); |
|
|
45 |
$this->method = $method ? strtoupper($method) : null; |
|
|
46 |
$this->currentUri = $currentUri; |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
/** |
|
|
50 |
* Gets the node associated with this link. |
|
|
51 |
* |
|
|
52 |
* @return \DOMNode A \DOMNode instance |
|
|
53 |
*/ |
|
|
54 |
public function getNode() |
|
|
55 |
{ |
|
|
56 |
return $this->node; |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
/** |
|
|
60 |
* Gets the method associated with this link. |
|
|
61 |
* |
|
|
62 |
* @return string The method |
|
|
63 |
* |
|
|
64 |
* @api |
|
|
65 |
*/ |
|
|
66 |
public function getMethod() |
|
|
67 |
{ |
|
|
68 |
return $this->method; |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
/** |
|
|
72 |
* Gets the URI associated with this link. |
|
|
73 |
* |
|
|
74 |
* @return string The URI |
|
|
75 |
* |
|
|
76 |
* @api |
|
|
77 |
*/ |
|
|
78 |
public function getUri() |
|
|
79 |
{ |
|
|
80 |
$uri = $this->getRawUri(); |
|
|
81 |
|
|
|
82 |
// absolute URL? |
|
|
83 |
if ('http' === substr($uri, 0, 4)) { |
|
|
84 |
return $uri; |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
// empty URI |
|
|
88 |
if (!$uri) { |
|
|
89 |
return $this->currentUri; |
|
|
90 |
} |
|
|
91 |
|
|
|
92 |
// only an anchor |
|
|
93 |
if ('#' === $uri[0]) { |
|
|
94 |
$baseUri = $this->currentUri; |
|
|
95 |
if (false !== $pos = strpos($baseUri, '#')) { |
|
|
96 |
$baseUri = substr($baseUri, 0, $pos); |
|
|
97 |
} |
|
|
98 |
|
|
|
99 |
return $baseUri.$uri; |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
// only a query string |
|
|
103 |
if ('?' === $uri[0]) { |
|
|
104 |
$baseUri = $this->currentUri; |
|
|
105 |
|
|
|
106 |
// remove the query string from the current uri |
|
|
107 |
if (false !== $pos = strpos($baseUri, '?')) { |
|
|
108 |
$baseUri = substr($baseUri, 0, $pos); |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
return $baseUri.$uri; |
|
|
112 |
} |
|
|
113 |
|
|
|
114 |
// absolute path |
|
|
115 |
if ('/' === $uri[0]) { |
|
|
116 |
return preg_replace('#^(.*?//[^/]+)(?:\/.*)?$#', '$1', $this->currentUri).$uri; |
|
|
117 |
} |
|
|
118 |
|
|
|
119 |
// relative path |
|
|
120 |
return substr($this->currentUri, 0, strrpos($this->currentUri, '/') + 1).$uri; |
|
|
121 |
} |
|
|
122 |
|
|
|
123 |
protected function getRawUri() |
|
|
124 |
{ |
|
|
125 |
return $this->node->getAttribute('href'); |
|
|
126 |
} |
|
|
127 |
|
|
|
128 |
protected function setNode(\DOMNode $node) |
|
|
129 |
{ |
|
|
130 |
if ('a' != $node->nodeName) { |
|
|
131 |
throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName)); |
|
|
132 |
} |
|
|
133 |
|
|
|
134 |
$this->node = $node; |
|
|
135 |
} |
|
|
136 |
} |