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\EvolvableLinkInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Link |
18
|
|
|
* |
19
|
|
|
* @package Phalcon\Link |
20
|
|
|
* |
21
|
|
|
* @property array $attributes |
22
|
|
|
* @property string $href |
23
|
|
|
* @property array $rels |
24
|
|
|
* @property bool $templated |
25
|
|
|
*/ |
26
|
|
|
class EvolvableLink extends Link implements EvolvableLinkInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Returns an instance with the specified attribute added. |
30
|
|
|
* |
31
|
|
|
* If the specified attribute is already present, it will be overwritten |
32
|
|
|
* with the new value. |
33
|
|
|
* |
34
|
|
|
* @param string $attribute The attribute to include. |
35
|
|
|
* @param string $value The value of the attribute to set. |
36
|
|
|
* |
37
|
|
|
* @return static |
38
|
|
|
*/ |
39
|
1 |
|
public function withAttribute($attribute, $value) |
40
|
|
|
{ |
41
|
1 |
|
$newInstance = clone $this; |
42
|
|
|
|
43
|
1 |
|
$newInstance->attributes[$attribute] = $value; |
44
|
|
|
|
45
|
1 |
|
return $newInstance; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns an instance with the specified href. |
50
|
|
|
* |
51
|
|
|
* @param string $href |
52
|
|
|
* The href value to include. It must be one of: |
53
|
|
|
* - An absolute URI, as defined by RFC 5988. |
54
|
|
|
* - A relative URI, as defined by RFC 5988. The base of the relative |
55
|
|
|
* link is assumed to be known based on context by the client. |
56
|
|
|
* - A URI template as defined by RFC 6570. |
57
|
|
|
* - An object implementing __toString() that produces one of the |
58
|
|
|
* above values. |
59
|
|
|
* |
60
|
|
|
* An implementing library SHOULD evaluate a passed object to a string |
61
|
|
|
* immediately rather than waiting for it to be returned later. |
62
|
|
|
* |
63
|
|
|
* @return static |
64
|
|
|
*/ |
65
|
1 |
|
public function withHref($href) |
66
|
|
|
{ |
67
|
1 |
|
$newInstance = clone $this; |
68
|
|
|
|
69
|
1 |
|
$newInstance->href = $href; |
70
|
1 |
|
$newInstance->templated = $this->hrefIsTemplated($href); |
71
|
|
|
|
72
|
1 |
|
return $newInstance; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns an instance with the specified relationship included. |
77
|
|
|
* |
78
|
|
|
* If the specified rel is already present, this method MUST return |
79
|
|
|
* normally without errors, but without adding the rel a second time. |
80
|
|
|
* |
81
|
|
|
* @param string $rel |
82
|
|
|
* The relationship value to add. |
83
|
|
|
* |
84
|
|
|
* @return static |
85
|
|
|
*/ |
86
|
2 |
|
public function withRel($rel) |
87
|
|
|
{ |
88
|
2 |
|
$newInstance = clone $this; |
89
|
|
|
|
90
|
2 |
|
$newInstance->rels[$rel] = true; |
91
|
|
|
|
92
|
2 |
|
return $newInstance; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns an instance with the specified attribute excluded. |
97
|
|
|
* |
98
|
|
|
* If the specified attribute is not present, this method MUST return |
99
|
|
|
* normally without errors. |
100
|
|
|
* |
101
|
|
|
* @param string $attribute |
102
|
|
|
* The attribute to remove. |
103
|
|
|
* |
104
|
|
|
* @return static |
105
|
|
|
*/ |
106
|
1 |
|
public function withoutAttribute($attribute) |
107
|
|
|
{ |
108
|
1 |
|
$newInstance = clone $this; |
109
|
|
|
|
110
|
1 |
|
unset($newInstance->attributes[$attribute]); |
111
|
|
|
|
112
|
1 |
|
return $newInstance; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns an instance with the specified relationship excluded. |
117
|
|
|
* |
118
|
|
|
* If the specified rel is already not present, this method MUST return |
119
|
|
|
* normally without errors. |
120
|
|
|
* |
121
|
|
|
* @param string $rel |
122
|
|
|
* The relationship value to exclude. |
123
|
|
|
* |
124
|
|
|
* @return static |
125
|
|
|
*/ |
126
|
1 |
|
public function withoutRel($rel) |
127
|
|
|
{ |
128
|
1 |
|
$newInstance = clone $this; |
129
|
|
|
|
130
|
1 |
|
unset($newInstance->rels[$rel]); |
131
|
|
|
|
132
|
1 |
|
return $newInstance; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|