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