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