1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace alsvanzelf\jsonapi\objects; |
4
|
|
|
|
5
|
|
|
use alsvanzelf\jsonapi\exceptions\DuplicateException; |
6
|
|
|
use alsvanzelf\jsonapi\helpers\AtMemberManager; |
7
|
|
|
use alsvanzelf\jsonapi\helpers\Converter; |
8
|
|
|
use alsvanzelf\jsonapi\helpers\Validator; |
9
|
|
|
use alsvanzelf\jsonapi\interfaces\ObjectInterface; |
10
|
|
|
use alsvanzelf\jsonapi\objects\LinkObject; |
11
|
|
|
use alsvanzelf\jsonapi\objects\LinksArray; |
12
|
|
|
|
13
|
|
|
class LinksObject implements ObjectInterface { |
|
|
|
|
14
|
|
|
use AtMemberManager; |
15
|
|
|
|
16
|
|
|
/** @var array with string|LinkObject */ |
17
|
|
|
protected $links = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* human api |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array $links key-value with values being href strings |
|
|
|
|
25
|
|
|
* @return LinksObject |
26
|
|
|
*/ |
27
|
|
|
public static function fromArray(array $links) { |
28
|
|
|
$linksObject = new self(); |
29
|
|
|
|
30
|
|
|
foreach ($links as $key => $href) { |
31
|
|
|
$linksObject->add($key, $href); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $linksObject; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param object $links |
|
|
|
|
39
|
|
|
* @return LinksObject |
40
|
|
|
*/ |
41
|
|
|
public static function fromObject($links) { |
42
|
|
|
$array = Converter::objectToArray($links); |
43
|
|
|
|
44
|
|
|
return self::fromArray($array); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $key |
|
|
|
|
49
|
|
|
* @param string $href |
|
|
|
|
50
|
|
|
* @param array $meta optional, if given a LinkObject is added, otherwise a link string is added |
51
|
|
|
*/ |
|
|
|
|
52
|
|
|
public function add($key, $href, array $meta=[]) { |
53
|
|
|
if ($meta === []) { |
54
|
|
|
$this->addLinkString($key, $href); |
55
|
|
|
} |
56
|
|
|
else { |
57
|
|
|
$this->addLinkObject($key, new LinkObject($href, $meta)); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* appends a link to an array of links under a specific key |
63
|
|
|
* |
64
|
|
|
* @see LinksArray for use cases |
65
|
|
|
* |
66
|
|
|
* @param string $key |
|
|
|
|
67
|
|
|
* @param string $href |
|
|
|
|
68
|
|
|
* @param array $meta optional, if given a LinkObject is added, otherwise a link string is added |
69
|
|
|
* |
70
|
|
|
* @throws DuplicateException if another link is already using that $key but is not an array |
71
|
|
|
*/ |
|
|
|
|
72
|
|
View Code Duplication |
public function append($key, $href, array $meta=[]) { |
|
|
|
|
73
|
|
|
Validator::checkMemberName($key); |
74
|
|
|
|
75
|
|
|
if (isset($this->links[$key]) === false) { |
76
|
|
|
$this->addLinksArray($key, new LinksArray()); |
77
|
|
|
} |
78
|
|
|
elseif ($this->links[$key] instanceof LinksArray === false) { |
79
|
|
|
throw new DuplicateException('can not add to key "'.$key.'", it is not an array of links'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->links[$key]->add($href, $meta); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* spec api |
87
|
|
|
*/ |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $key |
|
|
|
|
91
|
|
|
* @param string $href |
|
|
|
|
92
|
|
|
* |
93
|
|
|
* @throws DuplicateException if another link is already using that $key |
94
|
|
|
*/ |
|
|
|
|
95
|
|
View Code Duplication |
public function addLinkString($key, $href) { |
|
|
|
|
96
|
|
|
Validator::checkMemberName($key); |
97
|
|
|
|
98
|
|
|
if (isset($this->links[$key])) { |
99
|
|
|
throw new DuplicateException('link with key "'.$key.'" already set'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->links[$key] = $href; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $key |
|
|
|
|
107
|
|
|
* @param LinkObject $linkObject |
|
|
|
|
108
|
|
|
* |
109
|
|
|
* @throws DuplicateException if another link is already using that $key |
110
|
|
|
*/ |
|
|
|
|
111
|
|
View Code Duplication |
public function addLinkObject($key, LinkObject $linkObject) { |
|
|
|
|
112
|
|
|
Validator::checkMemberName($key); |
113
|
|
|
|
114
|
|
|
if (isset($this->links[$key])) { |
115
|
|
|
throw new DuplicateException('link with key "'.$key.'" already set'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->links[$key] = $linkObject; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $key |
|
|
|
|
123
|
|
|
* @param LinksArray $linksArray |
|
|
|
|
124
|
|
|
* |
125
|
|
|
* @throws DuplicateException if another link is already using that $key |
126
|
|
|
*/ |
|
|
|
|
127
|
|
View Code Duplication |
public function addLinksArray($key, LinksArray $linksArray) { |
|
|
|
|
128
|
|
|
Validator::checkMemberName($key); |
129
|
|
|
|
130
|
|
|
if (isset($this->links[$key])) { |
131
|
|
|
throw new DuplicateException('link with key "'.$key.'" already set'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->links[$key] = $linksArray; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $key |
|
|
|
|
139
|
|
|
* @param LinkObject $linkObject |
|
|
|
|
140
|
|
|
* |
141
|
|
|
* @throws DuplicateException if another link is already using that $key but is not an array |
142
|
|
|
*/ |
|
|
|
|
143
|
|
View Code Duplication |
public function appendLinkObject($key, LinkObject $linkObject) { |
|
|
|
|
144
|
|
|
Validator::checkMemberName($key); |
145
|
|
|
|
146
|
|
|
if (isset($this->links[$key]) === false) { |
147
|
|
|
$this->addLinksArray($key, new LinksArray()); |
148
|
|
|
} |
149
|
|
|
elseif ($this->links[$key] instanceof LinksArray === false) { |
150
|
|
|
throw new DuplicateException('can not add to key "'.$key.'", it is not an array of links'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->links[$key]->addLinkObject($linkObject); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* ObjectInterface |
158
|
|
|
*/ |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritDoc |
162
|
|
|
*/ |
|
|
|
|
163
|
|
|
public function isEmpty() { |
164
|
|
|
return ($this->links === [] && $this->hasAtMembers() === false); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritDoc |
169
|
|
|
*/ |
|
|
|
|
170
|
|
|
public function toArray() { |
171
|
|
|
$array = $this->getAtMembers(); |
172
|
|
|
|
173
|
|
|
foreach ($this->links as $key => $link) { |
174
|
|
|
if ($link instanceof LinkObject && $link->isEmpty() === false) { |
175
|
|
|
$array[$key] = $link->toArray(); |
176
|
|
|
} |
177
|
|
|
elseif ($link instanceof LinksArray && $link->isEmpty() === false) { |
178
|
|
|
$array[$key] = $link->toArray(); |
179
|
|
|
} |
180
|
|
|
elseif ($link instanceof LinkObject && $link->isEmpty()) { |
181
|
|
|
$array[$key] = null; |
182
|
|
|
} |
183
|
|
|
else { // string or null |
184
|
|
|
$array[$key] = $link; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $array; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|