|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace alsvanzelf\jsonapi\objects; |
|
4
|
|
|
|
|
5
|
|
|
use alsvanzelf\jsonapi\helpers\AtMemberManager; |
|
6
|
|
|
use alsvanzelf\jsonapi\helpers\ExtensionMemberManager; |
|
7
|
|
|
use alsvanzelf\jsonapi\interfaces\ObjectInterface; |
|
8
|
|
|
use alsvanzelf\jsonapi\objects\MetaObject; |
|
9
|
|
|
|
|
10
|
|
|
class LinkObject implements ObjectInterface { |
|
11
|
|
|
use AtMemberManager, ExtensionMemberManager; |
|
12
|
|
|
|
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
protected $href; |
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
protected $rel; |
|
17
|
|
|
/** @var LinkObject */ |
|
18
|
|
|
protected $describedby; |
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $title; |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $type; |
|
23
|
|
|
/** @var string[] */ |
|
24
|
|
|
protected $hreflang = []; |
|
25
|
|
|
/** @var MetaObject */ |
|
26
|
|
|
protected $meta; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $href |
|
30
|
|
|
* @param array $meta optional |
|
31
|
|
|
*/ |
|
32
|
38 |
|
public function __construct($href=null, array $meta=[]) { |
|
33
|
38 |
|
if ($href !== null) { |
|
34
|
25 |
|
$this->setHref($href); |
|
35
|
|
|
} |
|
36
|
38 |
|
if ($meta !== []) { |
|
37
|
6 |
|
$this->setMetaObject(MetaObject::fromArray($meta)); |
|
38
|
|
|
} |
|
39
|
38 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* human api |
|
43
|
|
|
*/ |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $href |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function setDescribedBy($href) { |
|
49
|
1 |
|
$this->setDescribedByLinkObject(new LinkObject($href)); |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $language |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function addLanguage($language) { |
|
56
|
2 |
|
if ($this->hreflang === []) { |
|
57
|
2 |
|
$this->setHreflang($language); |
|
58
|
|
|
} |
|
59
|
|
|
else { |
|
60
|
1 |
|
$this->setHreflang(...array_merge($this->hreflang, [$language])); |
|
61
|
|
|
} |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $key |
|
66
|
|
|
* @param mixed $value |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function addMeta($key, $value) { |
|
69
|
1 |
|
if ($this->meta === null) { |
|
70
|
1 |
|
$this->setMetaObject(new MetaObject()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
$this->meta->add($key, $value); |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* spec api |
|
78
|
|
|
*/ |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $href |
|
82
|
|
|
*/ |
|
83
|
25 |
|
public function setHref($href) { |
|
84
|
25 |
|
$this->href = $href; |
|
85
|
25 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @todo validate according to https://tools.ietf.org/html/rfc8288#section-2.1 |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $relationType |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function setRelationType($relationType) { |
|
93
|
1 |
|
$this->rel = $relationType; |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param LinkObject $describedBy |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function setDescribedByLinkObject(LinkObject $describedBy) { |
|
100
|
2 |
|
$this->describedby = $describedBy; |
|
101
|
2 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $friendlyTitle |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function setHumanTitle($humanTitle) { |
|
107
|
1 |
|
$this->title = $humanTitle; |
|
108
|
1 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $mediaType |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function setMediaType($mediaType) { |
|
114
|
1 |
|
$this->type = $mediaType; |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @todo validate according to https://tools.ietf.org/html/rfc5646 |
|
119
|
|
|
* |
|
120
|
|
|
* @param string ...$hreflang |
|
121
|
|
|
*/ |
|
122
|
3 |
|
public function setHreflang(...$hreflang) { |
|
123
|
3 |
|
$this->hreflang = $hreflang; |
|
124
|
3 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param MetaObject $metaObject |
|
128
|
|
|
*/ |
|
129
|
10 |
|
public function setMetaObject(MetaObject $metaObject) { |
|
130
|
10 |
|
$this->meta = $metaObject; |
|
131
|
10 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* ObjectInterface |
|
135
|
|
|
*/ |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @inheritDoc |
|
139
|
|
|
*/ |
|
140
|
33 |
|
public function isEmpty() { |
|
141
|
33 |
|
if ($this->href !== null) { |
|
142
|
22 |
|
return false; |
|
143
|
|
|
} |
|
144
|
16 |
|
if ($this->rel !== null) { |
|
145
|
1 |
|
return false; |
|
146
|
|
|
} |
|
147
|
16 |
|
if ($this->title !== null) { |
|
148
|
1 |
|
return false; |
|
149
|
|
|
} |
|
150
|
16 |
|
if ($this->type !== null) { |
|
151
|
1 |
|
return false; |
|
152
|
|
|
} |
|
153
|
16 |
|
if ($this->hreflang !== []) { |
|
154
|
2 |
|
return false; |
|
155
|
|
|
} |
|
156
|
16 |
|
if ($this->describedby !== null && $this->describedby->isEmpty() === false) { |
|
157
|
2 |
|
return false; |
|
158
|
|
|
} |
|
159
|
16 |
|
if ($this->meta !== null && $this->meta->isEmpty() === false) { |
|
160
|
1 |
|
return false; |
|
161
|
|
|
} |
|
162
|
16 |
|
if ($this->hasAtMembers()) { |
|
163
|
1 |
|
return false; |
|
164
|
|
|
} |
|
165
|
16 |
|
if ($this->hasExtensionMembers()) { |
|
166
|
1 |
|
return false; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
16 |
|
return true; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @inheritDoc |
|
174
|
|
|
*/ |
|
175
|
29 |
|
public function toArray() { |
|
176
|
29 |
|
$array = []; |
|
177
|
|
|
|
|
178
|
29 |
|
if ($this->hasAtMembers()) { |
|
179
|
2 |
|
$array = array_merge($array, $this->getAtMembers()); |
|
180
|
|
|
} |
|
181
|
29 |
|
if ($this->hasExtensionMembers()) { |
|
182
|
1 |
|
$array = array_merge($array, $this->getExtensionMembers()); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
29 |
|
$array['href'] = $this->href; |
|
186
|
|
|
|
|
187
|
29 |
|
if ($this->rel !== null) { |
|
188
|
1 |
|
$array['rel'] = $this->rel; |
|
189
|
|
|
} |
|
190
|
29 |
|
if ($this->title !== null) { |
|
191
|
1 |
|
$array['title'] = $this->title; |
|
192
|
|
|
} |
|
193
|
29 |
|
if ($this->type !== null) { |
|
194
|
1 |
|
$array['type'] = $this->type; |
|
195
|
|
|
} |
|
196
|
29 |
|
if ($this->hreflang !== []) { |
|
197
|
3 |
|
if (count($this->hreflang) === 1) { |
|
198
|
2 |
|
$array['hreflang'] = $this->hreflang[0]; |
|
199
|
|
|
} |
|
200
|
|
|
else { |
|
201
|
2 |
|
$array['hreflang'] = $this->hreflang; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
29 |
|
if ($this->describedby !== null && $this->describedby->isEmpty() === false) { |
|
205
|
2 |
|
$array['describedby'] = $this->describedby->toArray(); |
|
206
|
|
|
} |
|
207
|
29 |
|
if ($this->meta !== null && $this->meta->isEmpty() === false) { |
|
208
|
10 |
|
$array['meta'] = $this->meta->toArray(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
29 |
|
return $array; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|