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
|
32 |
|
protected $type; |
22
|
32 |
|
/** @var string[] */ |
23
|
26 |
|
protected $hreflang; |
24
|
|
|
/** @var MetaObject */ |
25
|
32 |
|
protected $meta; |
26
|
6 |
|
|
27
|
|
|
/** |
28
|
32 |
|
* @param string $href |
29
|
|
|
* @param array $meta optional |
30
|
|
|
*/ |
31
|
|
|
public function __construct($href=null, array $meta=[]) { |
32
|
|
|
if ($href !== null) { |
33
|
|
|
$this->setHref($href); |
34
|
|
|
} |
35
|
|
|
if ($meta !== []) { |
36
|
|
|
$this->setMetaObject(MetaObject::fromArray($meta)); |
37
|
|
|
} |
38
|
1 |
|
} |
39
|
1 |
|
|
40
|
1 |
|
/** |
41
|
|
|
* human api |
42
|
|
|
*/ |
43
|
1 |
|
|
44
|
1 |
|
/** |
45
|
|
|
* @param string $href |
46
|
|
|
*/ |
47
|
|
|
public function setDescribedBy($href) { |
48
|
|
|
$this->setDescribedByLinkObject(new LinkObject($href)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $language |
53
|
26 |
|
*/ |
54
|
26 |
|
public function addLanguage($language) { |
55
|
26 |
|
if ($this->hreflang === null) { |
56
|
|
|
$this->setHreflang($language); |
57
|
|
|
} |
58
|
|
|
else { |
59
|
|
|
$this->setHreflang(...$this->hreflang, $language); |
60
|
9 |
|
} |
61
|
9 |
|
} |
62
|
9 |
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $key |
65
|
|
|
* @param mixed $value |
66
|
|
|
*/ |
67
|
|
|
public function addMeta($key, $value) { |
68
|
|
|
if ($this->meta === null) { |
69
|
|
|
$this->setMetaObject(new MetaObject()); |
70
|
|
|
} |
71
|
25 |
|
|
72
|
25 |
|
$this->meta->add($key, $value); |
73
|
20 |
|
} |
74
|
|
|
|
75
|
8 |
|
/** |
76
|
1 |
|
* spec api |
77
|
|
|
*/ |
78
|
8 |
|
|
79
|
1 |
|
/** |
80
|
|
|
* @param string $href |
81
|
|
|
*/ |
82
|
8 |
|
public function setHref($href) { |
83
|
|
|
$this->href = $href; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $relationType |
88
|
24 |
|
*/ |
89
|
24 |
|
public function setRelationType($relationType) { |
90
|
|
|
$this->rel = $relationType; |
91
|
24 |
|
} |
92
|
|
|
|
93
|
24 |
|
/** |
94
|
9 |
|
* @param LinkObject $describedBy |
95
|
|
|
*/ |
96
|
|
|
public function setDescribedByLinkObject(LinkObject $describedBy) { |
97
|
24 |
|
$this->describedby = $describedBy; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $friendlyTitle |
102
|
|
|
*/ |
103
|
|
|
public function setHumanTitle($humanTitle) { |
104
|
|
|
$this->title = $humanTitle; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $mediaType |
109
|
|
|
*/ |
110
|
|
|
public function setMediaType($mediaType) { |
111
|
|
|
$this->type = $mediaType; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string ...$hreflang |
116
|
|
|
*/ |
117
|
|
|
public function setHreflang(...$hreflang) { |
118
|
|
|
$this->hreflang = $hreflang; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param MetaObject $metaObject |
123
|
|
|
*/ |
124
|
|
|
public function setMetaObject(MetaObject $metaObject) { |
125
|
|
|
$this->meta = $metaObject; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* ObjectInterface |
130
|
|
|
*/ |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritDoc |
134
|
|
|
*/ |
135
|
|
|
public function isEmpty() { |
136
|
|
|
if ($this->href !== null) { |
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
if ($this->meta !== null && $this->meta->isEmpty() === false) { |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
if ($this->hasAtMembers()) { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritDoc |
151
|
|
|
*/ |
152
|
|
|
public function toArray() { |
153
|
|
|
$array = $this->getAtMembers(); |
154
|
|
|
|
155
|
|
|
$array['href'] = $this->href; |
156
|
|
|
|
157
|
|
|
if ($this->rel) { |
158
|
|
|
$array['rel'] = $this->rel; |
159
|
|
|
} |
160
|
|
|
if ($this->title) { |
161
|
|
|
$array['title'] = $this->title; |
162
|
|
|
} |
163
|
|
|
if ($this->type) { |
164
|
|
|
$array['type'] = $this->type; |
165
|
|
|
} |
166
|
|
|
if ($this->hreflang !== []) { |
167
|
|
|
if (count($this->hreflang) === 1) { |
168
|
|
|
$array['hreflang'] = $this->hreflang[0]; |
169
|
|
|
} |
170
|
|
|
else { |
171
|
|
|
$array['hreflang'] = $this->hreflang; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
if ($this->describedby !== null && $this->describedby->isEmpty() === false) { |
175
|
|
|
$array['describedby'] = $this->describedby->toArray(); |
176
|
|
|
} |
177
|
|
|
if ($this->meta !== null && $this->meta->isEmpty() === false) { |
178
|
|
|
$array['meta'] = $this->meta->toArray(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $array; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|