symfony /src/Symfony/Components/DomCrawler/Link.php

Language PHP Lines 93
MD5 Hash 33900a445b30fe894ff82e62cafe8dee
Repository https://github.com/come/symfony.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php

namespace Symfony\Components\DomCrawler;

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Link represents an HTML link (an HTML a tag).
 *
 * @package    Symfony
 * @subpackage Components_DomCrawler
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 */
class Link
{
    protected $node;
    protected $method;
    protected $host;
    protected $path;

    /**
     * Constructor.
     *
     * @param \DOMNode $node   A \DOMNode instance
     * @param string   $method The method to use for the link (get by default)
     * @param string   $host   The base URI to use for absolute links (like http://localhost)
     * @param string   $path   The base path for relative links (/ by default)
     *
     * @throws \LogicException if the node is not a link
     */
    public function __construct(\DOMNode $node, $method = 'get', $host = null, $path = '/')
    {
        if ('a' != $node->nodeName) {
            throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName));
        }

        $this->node = $node;
        $this->method = $method;
        $this->host = $host;
        $this->path = empty($path) ? '/' : $path;
    }

    /**
     * Gets the node associated with this link.
     *
     * @return \DOMNode A \DOMNode instance
     */
    public function getNode()
    {
        return $this->node;
    }

    /**
     * Gets the URI associated with this link.
     *
     * @param Boolean $absolute Whether to return an absolute URI or not (this only works if a base URI has been provided)
     *
     * @return string The URI
     */
    public function getUri($absolute = true)
    {
        $uri = $this->node->getAttribute('href');
        $urlHaveScheme = 'http' === substr($uri, 0, 4);

        if ($uri && '/' !== $uri[0] && !$urlHaveScheme) {
            $uri = $this->path.$uri;
        }

        if ($absolute && null !== $this->host && !$urlHaveScheme) {
            return $this->host.$uri;
        }

        return $uri;
    }

    /**
     * Gets the method associated with this link.
     *
     * @return string The method
     */
    public function getMethod()
    {
        return $this->method;
    }
}
Back to Top