1
2/**
3 * This file is part of the Phalcon Framework.
4 *
5 * (c) Phalcon Team <team@phalcon.io>
6 *
7 * For the full copyright and license information, please view the LICENSE.txt
8 * file that was distributed with this source code.
9 */
10
11namespace Phalcon\Html\Link;
12
13use Phalcon\Collection;
14use Phalcon\Collection\CollectionInterface;
15use Psr\Link\LinkInterface;
16
17/**
18 * Class Phalcon\Http\Link\Link
19 *
20 * @property array  attributes
21 * @property string href
22 * @property array  rels
23 * @property bool   templated
24 */
25class Link implements LinkInterface
26{
27    /**
28     * @var Collection|CollectionInterface
29     */
30    protected attributes;
31
32    /**
33     * @var string
34     */
35    protected href = "";
36
37    /**
38     * @var Collection|CollectionInterface
39     */
40    protected rels;
41
42    /**
43     * @var bool
44     */
45    protected templated = false;
46
47    /**
48     * Link constructor.
49     *
50     * @param string rel
51     * @param string href
52     */
53    public function __construct(
54        string rel = "",
55        string href = "",
56        array attributes = []
57    ) {
58        let this->rels       = new Collection(),
59            this->attributes = new Collection(attributes),
60            this->href       = href,
61            this->templated  = this->hrefIsTemplated(href);
62
63        if !empty rel {
64            this->rels->set(rel, true);
65        }
66    }
67
68    /**
69     * Returns a list of attributes that describe the target URI.
70     *
71     * @return array
72     *   A key-value list of attributes, where the key is a string and the value
73     *  is either a PHP primitive or an array of PHP strings. If no values are
74     *  found an empty array MUST be returned.
75     */
76    public function getAttributes()
77    {
78        return this->attributes->toArray();
79    }
80
81    /**
82     * Returns the target of the link.
83     *
84     * The target link must be one of:
85     * - An absolute URI, as defined by RFC 5988.
86     * - A relative URI, as defined by RFC 5988. The base of the relative link
87     *     is assumed to be known based on context by the client.
88     * - A URI template as defined by RFC 6570.
89     *
90     * If a URI template is returned, isTemplated() MUST return True.
91     *
92     * @return string
93     */
94    public function getHref()
95    {
96        return this->href;
97    }
98
99    /**
100     * Returns the relationship type(s) of the link.
101     *
102     * This method returns 0 or more relationship types for a link, expressed
103     * as an array of strings.
104     *
105     * @return string[]
106     */
107    public function getRels()
108    {
109        return this->rels->getKeys(false);
110    }
111
112    /**
113     * Returns whether or not this is a templated link.
114     *
115     * @return bool True if this link object is templated, False otherwise.
116     */
117    public function isTemplated()
118    {
119        return this->templated;
120    }
121
122    /**
123     * Determines if a href is a templated link or not.
124     *
125     * @see https://tools.ietf.org/html/rfc6570
126     *
127     * @param string href
128     *
129     * @return bool
130     */
131    protected function hrefIsTemplated(string href) -> bool
132    {
133        return (
134            false !== strpos(href, "{") &&
135            false !== strrpos(href, "}")
136        );
137    }
138}
139