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\ExtensionMemberManager; |
||||
9 | use alsvanzelf\jsonapi\helpers\Validator; |
||||
10 | use alsvanzelf\jsonapi\interfaces\ObjectInterface; |
||||
11 | use alsvanzelf\jsonapi\objects\LinkObject; |
||||
12 | use alsvanzelf\jsonapi\objects\LinksArray; |
||||
13 | |||||
14 | class LinksObject implements ObjectInterface { |
||||
15 | use AtMemberManager, ExtensionMemberManager; |
||||
16 | |||||
17 | /** @var array with string|LinkObject */ |
||||
18 | protected $links = []; |
||||
19 | |||||
20 | /** |
||||
21 | * human api |
||||
22 | */ |
||||
23 | |||||
24 | /** |
||||
25 | * @param array $links key-value with values being href strings |
||||
26 | * @return LinksObject |
||||
27 | */ |
||||
28 | 3 | public static function fromArray(array $links) { |
|||
29 | 3 | $linksObject = new self(); |
|||
30 | |||||
31 | 3 | foreach ($links as $key => $href) { |
|||
32 | 3 | $linksObject->add($key, $href); |
|||
33 | } |
||||
34 | |||||
35 | 3 | return $linksObject; |
|||
36 | } |
||||
37 | |||||
38 | /** |
||||
39 | * @param object $links |
||||
40 | * @return LinksObject |
||||
41 | */ |
||||
42 | 1 | public static function fromObject($links) { |
|||
43 | 1 | $array = Converter::objectToArray($links); |
|||
44 | |||||
45 | 1 | return self::fromArray($array); |
|||
46 | } |
||||
47 | |||||
48 | /** |
||||
49 | * @param string $key |
||||
50 | * @param string $href |
||||
51 | * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added |
||||
52 | */ |
||||
53 | 33 | public function add($key, $href, array $meta=[]) { |
|||
54 | 33 | if ($meta === []) { |
|||
55 | 30 | $this->addLinkString($key, $href); |
|||
56 | } |
||||
57 | else { |
||||
58 | 4 | $this->addLinkObject($key, new LinkObject($href, $meta)); |
|||
59 | } |
||||
60 | } |
||||
61 | |||||
62 | /** |
||||
63 | * appends a link to an array of links under a specific key |
||||
64 | * |
||||
65 | * @see LinksArray for use cases |
||||
66 | * |
||||
67 | * @deprecated array links are not supported anymore {@see ->add()} |
||||
68 | * |
||||
69 | * @param string $key |
||||
70 | * @param string $href |
||||
71 | * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added |
||||
72 | * |
||||
73 | * @throws DuplicateException if another link is already using that $key but is not an array |
||||
74 | */ |
||||
75 | 6 | public function append($key, $href, array $meta=[]) { |
|||
76 | 6 | Validator::checkMemberName($key); |
|||
77 | |||||
78 | 6 | if (isset($this->links[$key]) === false) { |
|||
79 | 5 | $this->addLinksArray($key, new LinksArray()); |
|||
0 ignored issues
–
show
|
|||||
80 | } |
||||
81 | 2 | elseif ($this->links[$key] instanceof LinksArray === false) { |
|||
82 | 1 | throw new DuplicateException('can not add to key "'.$key.'", it is not an array of links'); |
|||
83 | } |
||||
84 | |||||
85 | 5 | $this->links[$key]->add($href, $meta); |
|||
86 | } |
||||
87 | |||||
88 | /** |
||||
89 | * spec api |
||||
90 | */ |
||||
91 | |||||
92 | /** |
||||
93 | * @param string $key |
||||
94 | * @param string $href |
||||
95 | * |
||||
96 | * @throws DuplicateException if another link is already using that $key |
||||
97 | */ |
||||
98 | 34 | public function addLinkString($key, $href) { |
|||
99 | 34 | Validator::checkMemberName($key); |
|||
100 | |||||
101 | 33 | if (isset($this->links[$key])) { |
|||
102 | 1 | throw new DuplicateException('link with key "'.$key.'" already set'); |
|||
103 | } |
||||
104 | |||||
105 | 33 | $this->links[$key] = $href; |
|||
106 | } |
||||
107 | |||||
108 | /** |
||||
109 | * @param string $key |
||||
110 | * @param LinkObject $linkObject |
||||
111 | * |
||||
112 | * @throws DuplicateException if another link is already using that $key |
||||
113 | */ |
||||
114 | 25 | public function addLinkObject($key, LinkObject $linkObject) { |
|||
115 | 25 | Validator::checkMemberName($key); |
|||
116 | |||||
117 | 24 | if (isset($this->links[$key])) { |
|||
118 | 1 | throw new DuplicateException('link with key "'.$key.'" already set'); |
|||
119 | } |
||||
120 | |||||
121 | 24 | $this->links[$key] = $linkObject; |
|||
122 | } |
||||
123 | |||||
124 | /** |
||||
125 | * @deprecated array links are not supported anymore {@see ->addLinkObject()} |
||||
126 | * |
||||
127 | * @param string $key |
||||
128 | * @param LinksArray $linksArray |
||||
129 | * |
||||
130 | * @throws DuplicateException if another link is already using that $key |
||||
131 | */ |
||||
132 | 10 | public function addLinksArray($key, LinksArray $linksArray) { |
|||
133 | 10 | Validator::checkMemberName($key); |
|||
134 | |||||
135 | 10 | if (isset($this->links[$key])) { |
|||
136 | 1 | throw new DuplicateException('link with key "'.$key.'" already set'); |
|||
137 | } |
||||
138 | |||||
139 | 9 | $this->links[$key] = $linksArray; |
|||
140 | } |
||||
141 | |||||
142 | /** |
||||
143 | * @deprecated array links are not supported anymore {@see ->addLinkObject()} |
||||
144 | * |
||||
145 | * @param string $key |
||||
146 | * @param LinkObject $linkObject |
||||
147 | * |
||||
148 | * @throws DuplicateException if another link is already using that $key but is not an array |
||||
149 | */ |
||||
150 | 3 | public function appendLinkObject($key, LinkObject $linkObject) { |
|||
151 | 3 | Validator::checkMemberName($key); |
|||
152 | |||||
153 | 3 | if (isset($this->links[$key]) === false) { |
|||
154 | 2 | $this->addLinksArray($key, new LinksArray()); |
|||
0 ignored issues
–
show
The function
alsvanzelf\jsonapi\objec...Object::addLinksArray() has been deprecated: array links are not supported anymore {@see ->addLinkObject()}
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
155 | } |
||||
156 | 3 | elseif ($this->links[$key] instanceof LinksArray === false) { |
|||
157 | 1 | throw new DuplicateException('can not add to key "'.$key.'", it is not an array of links'); |
|||
158 | } |
||||
159 | |||||
160 | 2 | $this->links[$key]->addLinkObject($linkObject); |
|||
161 | } |
||||
162 | |||||
163 | /** |
||||
164 | * ObjectInterface |
||||
165 | */ |
||||
166 | |||||
167 | /** |
||||
168 | * @inheritDoc |
||||
169 | */ |
||||
170 | 45 | public function isEmpty() { |
|||
171 | 45 | if ($this->links !== []) { |
|||
172 | 44 | return false; |
|||
173 | } |
||||
174 | 3 | if ($this->hasAtMembers()) { |
|||
175 | 1 | return false; |
|||
176 | } |
||||
177 | 2 | if ($this->hasExtensionMembers()) { |
|||
178 | 1 | return false; |
|||
179 | } |
||||
180 | |||||
181 | 1 | return true; |
|||
182 | } |
||||
183 | |||||
184 | /** |
||||
185 | * @inheritDoc |
||||
186 | */ |
||||
187 | 55 | public function toArray() { |
|||
188 | 55 | $array = []; |
|||
189 | |||||
190 | 55 | if ($this->hasAtMembers()) { |
|||
191 | 2 | $array = array_merge($array, $this->getAtMembers()); |
|||
192 | } |
||||
193 | 55 | if ($this->hasExtensionMembers()) { |
|||
194 | 1 | $array = array_merge($array, $this->getExtensionMembers()); |
|||
195 | } |
||||
196 | |||||
197 | 55 | foreach ($this->links as $key => $link) { |
|||
198 | 55 | if ($link instanceof LinkObject && $link->isEmpty() === false) { |
|||
199 | 19 | $array[$key] = $link->toArray(); |
|||
200 | } |
||||
201 | 40 | elseif ($link instanceof LinksArray && $link->isEmpty() === false) { |
|||
202 | 9 | $array[$key] = $link->toArray(); |
|||
203 | } |
||||
204 | 31 | elseif ($link instanceof LinkObject && $link->isEmpty()) { |
|||
205 | 6 | $array[$key] = null; |
|||
206 | } |
||||
207 | else { // string or null |
||||
208 | 26 | $array[$key] = $link; |
|||
209 | } |
||||
210 | } |
||||
211 | |||||
212 | 55 | return $array; |
|||
213 | } |
||||
214 | } |
||||
215 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.