1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hateoas\Model; |
4
|
|
|
|
5
|
|
|
use Psr\Link\EvolvableLinkInterface; |
6
|
|
|
use Psr\Link\LinkInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Adrien Brault <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Link implements LinkInterface, EvolvableLinkInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $rel; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $href; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $attributes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $rel |
30
|
|
|
* @param string $href |
31
|
|
|
* @param array $attributes |
32
|
|
|
*/ |
33
|
|
|
public function __construct($rel, $href, array $attributes = array()) |
34
|
|
|
{ |
35
|
|
|
$this->rel = $rel; |
36
|
|
|
$this->href = $href; |
37
|
|
|
$this->attributes = $attributes; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function getAttributes() |
44
|
|
|
{ |
45
|
|
|
return $this->attributes; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getHref() |
52
|
|
|
{ |
53
|
|
|
return $this->href; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getRel() |
60
|
|
|
{ |
61
|
|
|
return $this->rel; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
|
|
public function isTemplated() |
68
|
|
|
{ |
69
|
|
|
return array_key_exists('templated', $this->attributes) && $this->attributes['templated']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
public function getRels() |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
$this->rel, |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
*/ |
85
|
|
|
public function withHref($href) |
86
|
|
|
{ |
87
|
|
|
return new static($this->rel, $href, $this->attributes); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
|
|
public function withRel($rel) |
94
|
|
|
{ |
95
|
|
|
return new static($rel, $this->href, $this->attributes); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
|
|
public function withoutRel($rel) |
102
|
|
|
{ |
103
|
|
|
// What should we do here? |
104
|
|
|
throw new \Exception('Not implemented'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritDoc} |
109
|
|
|
*/ |
110
|
|
|
public function withAttribute($attribute, $value) |
111
|
|
|
{ |
112
|
|
|
if (array_key_exists($attribute, $this->attributes) && $this->attributes[$attribute] === $value) { |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
return new static($this->rel, $this->href, array_merge($this->attributes, [ |
116
|
|
|
$attribute => $value, |
117
|
|
|
])); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritDoc} |
122
|
|
|
*/ |
123
|
|
|
public function withoutAttribute($attribute) |
124
|
|
|
{ |
125
|
|
|
if (!array_key_exists($attribute, $this->attributes)) { |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$attributes = array_filter($this->attributes, function ($filteredAttribute) use ($attribute) { |
130
|
|
|
return $filteredAttribute !== $attribute; |
131
|
|
|
}, ARRAY_FILTER_USE_KEY); |
132
|
|
|
return new static($this->rel, $this->href, $attributes); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|