-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvolvableLink.php
123 lines (106 loc) · 3.29 KB
/
EvolvableLink.php
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\Link;
use Psr\Link\EvolvableLinkInterface;
use Stringable;
/**
* @author Joshua Estes <[email protected]>
*/
class EvolvableLink extends Link implements EvolvableLinkInterface
{
/**
* Returns an instance with the specified href.
*
* @param string|Stringable $href
* The href value to include. It must be one of:
* - An absolute URI, as defined by RFC 5988.
* - A relative URI, as defined by RFC 5988. The base of the relative link
* is assumed to be known based on context by the client.
* - A URI template as defined by RFC 6570.
* - An object implementing __toString() that produces one of the above
* values.
*
* An implementing library SHOULD evaluate a passed object to a string
* immediately rather than waiting for it to be returned later.
*/
public function withHref(string|Stringable $href): static
{
if ($href instanceof Stringable) {
$href = (string) $href;
}
$that = clone $this;
$that->href = $href;
return $that;
}
/**
* Returns an instance with the specified relationship included.
*
* If the specified rel is already present, this method MUST return
* normally without errors, but without adding the rel a second time.
*
* @param string $rel
* The relationship value to add.
*/
public function withRel(string $rel): static
{
if (array_key_exists($rel, $this->rels)) {
return $this;
}
$that = clone $this;
$that->rels[$rel] = $rel;
return $that;
}
/**
* Returns an instance with the specified relationship excluded.
*
* If the specified rel is already not present, this method MUST return
* normally without errors.
*
* @param string $rel
* The relationship value to exclude.
*/
public function withoutRel(string $rel): static
{
if (!array_key_exists($rel, $this->rels)) {
return $this;
}
$that = clone $this;
unset($that->rels[$rel]);
return $that;
}
/**
* Returns an instance with the specified attribute added.
*
* If the specified attribute is already present, it will be overwritten
* with the new value.
*
* @param string $attribute
* The attribute to include.
* @param string|Stringable|int|float|bool|array $value
* The value of the attribute to set.
*/
public function withAttribute(string $attribute, string|Stringable|int|float|bool|array $value): static
{
$that = clone $this;
$that->attributes[$attribute] = $value;
return $that;
}
/**
* Returns an instance with the specified attribute excluded.
*
* If the specified attribute is not present, this method MUST return
* normally without errors.
*
* @param string $attribute
* The attribute to remove.
*/
public function withoutAttribute(string $attribute): static
{
if (!array_key_exists($attribute, $this->attributes)) {
return $this;
}
$that = clone $this;
unset($that->attributes[$attribute]);
return $that;
}
}