1 | <?php |
||||
2 | |||||
3 | namespace Siren; |
||||
4 | |||||
5 | use Siren\Link; |
||||
6 | use LogicException; |
||||
7 | |||||
8 | class Entity |
||||
9 | { |
||||
10 | /** |
||||
11 | * @var array |
||||
12 | */ |
||||
13 | protected $class = array(); |
||||
14 | |||||
15 | /** |
||||
16 | * @var array |
||||
17 | */ |
||||
18 | protected $rel = array(); |
||||
19 | |||||
20 | /** |
||||
21 | * @var string |
||||
22 | */ |
||||
23 | protected $href; |
||||
24 | |||||
25 | /** |
||||
26 | * @var array |
||||
27 | */ |
||||
28 | protected $properties = array(); |
||||
29 | |||||
30 | /** |
||||
31 | * @var Link[] |
||||
32 | */ |
||||
33 | protected $links = array(); |
||||
34 | |||||
35 | /** |
||||
36 | * Get class |
||||
37 | * |
||||
38 | * @return string |
||||
39 | */ |
||||
40 | public function getClass() |
||||
41 | { |
||||
42 | return $this->class; |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
43 | } |
||||
44 | |||||
45 | /** |
||||
46 | * Set class |
||||
47 | * |
||||
48 | * @param array $class |
||||
49 | * @return $this |
||||
50 | * @throws LogicException |
||||
51 | */ |
||||
52 | View Code Duplication | public function setClass(array $class) |
|||
53 | { |
||||
54 | foreach ($class as $classItem) { |
||||
55 | if (!is_string($classItem)) { |
||||
56 | $exMsg = sprintf('Provided class `%s` is not a string', $classItem); |
||||
57 | throw new LogicException($exMsg); |
||||
58 | } |
||||
59 | } |
||||
60 | |||||
61 | $this->class = $class; |
||||
62 | |||||
63 | return $this; |
||||
64 | } |
||||
65 | |||||
66 | /** |
||||
67 | * Add class |
||||
68 | * |
||||
69 | * @param string $class |
||||
70 | * @return $this |
||||
71 | * @throws LogicException |
||||
72 | */ |
||||
73 | View Code Duplication | public function addClass($class) |
|||
74 | { |
||||
75 | if (!is_string($class)) { |
||||
76 | $exMsg = sprintf('Provided class `%s` is not a string', $class); |
||||
77 | throw new LogicException($exMsg); |
||||
78 | } |
||||
79 | |||||
80 | $currentClass = $this->getClass(); |
||||
81 | if (in_array($class, $currentClass)) { |
||||
0 ignored issues
–
show
$currentClass of type string is incompatible with the type array expected by parameter $haystack of in_array() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
82 | $exMsg = sprintf('Provided class `%s` is already registered', $class); |
||||
83 | throw new LogicException($exMsg); |
||||
84 | } |
||||
85 | |||||
86 | $this->class[] = $class; |
||||
87 | |||||
88 | return $this; |
||||
89 | } |
||||
90 | |||||
91 | /** |
||||
92 | * Get rel |
||||
93 | * |
||||
94 | * @return array |
||||
95 | */ |
||||
96 | public function getRel() |
||||
97 | { |
||||
98 | return $this->rel; |
||||
99 | } |
||||
100 | |||||
101 | /** |
||||
102 | * Set rel |
||||
103 | * |
||||
104 | * @param array $rel |
||||
105 | * @return $this |
||||
106 | * @throws LogicException |
||||
107 | */ |
||||
108 | View Code Duplication | public function setRel(array $rel) |
|||
109 | { |
||||
110 | foreach ($rel as $relItem) { |
||||
111 | if (!is_string($relItem)) { |
||||
112 | $exMsg = sprintf('Provided rel `%s` is not a string', $relItem); |
||||
113 | throw new LogicException($exMsg); |
||||
114 | } |
||||
115 | } |
||||
116 | |||||
117 | $this->rel = $rel; |
||||
118 | |||||
119 | return $this; |
||||
120 | } |
||||
121 | |||||
122 | /** |
||||
123 | * Add rel |
||||
124 | * |
||||
125 | * @param string $rel |
||||
126 | * @return $this |
||||
127 | * @throws LogicException |
||||
128 | */ |
||||
129 | View Code Duplication | public function addRel($rel) |
|||
130 | { |
||||
131 | if (!is_string($rel)) { |
||||
132 | $exMsg = sprintf('Provided rel `%s` is not a string', $rel); |
||||
133 | throw new LogicException($exMsg); |
||||
134 | } |
||||
135 | |||||
136 | $this->rel[] = $rel; |
||||
137 | |||||
138 | return $this; |
||||
139 | } |
||||
140 | |||||
141 | /** |
||||
142 | * Get href |
||||
143 | * |
||||
144 | * @return string |
||||
145 | */ |
||||
146 | public function getHref() |
||||
147 | { |
||||
148 | return $this->href; |
||||
149 | } |
||||
150 | |||||
151 | /** |
||||
152 | * Set href |
||||
153 | * |
||||
154 | * @param string $href |
||||
155 | * @return $this |
||||
156 | * @throws LogicException |
||||
157 | */ |
||||
158 | View Code Duplication | public function setHref($href) |
|||
159 | { |
||||
160 | if (!is_string($href)) { |
||||
161 | $exMsg = sprintf('Provided href `%s` is not a string', $href); |
||||
162 | throw new LogicException($exMsg); |
||||
163 | } |
||||
164 | |||||
165 | $this->href = $href; |
||||
166 | |||||
167 | return $this; |
||||
168 | } |
||||
169 | |||||
170 | /** |
||||
171 | * Get properties |
||||
172 | * |
||||
173 | * @return array |
||||
174 | */ |
||||
175 | public function getProperties() |
||||
176 | { |
||||
177 | return $this->properties; |
||||
178 | } |
||||
179 | |||||
180 | /** |
||||
181 | * Add property |
||||
182 | * |
||||
183 | * @param string $key |
||||
184 | * @param mixed $value |
||||
185 | * @return $this |
||||
186 | */ |
||||
187 | public function addProperty($key, $value) |
||||
188 | { |
||||
189 | $this->properties[$key] = $value; |
||||
190 | |||||
191 | return $this; |
||||
192 | } |
||||
193 | |||||
194 | /** |
||||
195 | * Set properties |
||||
196 | * |
||||
197 | * @param array $properties |
||||
198 | * @return $this |
||||
199 | * @throws LogicException |
||||
200 | */ |
||||
201 | public function setProperties(array $properties) |
||||
202 | { |
||||
203 | $this->properties = $properties; |
||||
204 | |||||
205 | return $this; |
||||
206 | } |
||||
207 | |||||
208 | /** |
||||
209 | * Get links |
||||
210 | * |
||||
211 | * @return Link[] |
||||
212 | */ |
||||
213 | public function getLinks() |
||||
214 | { |
||||
215 | return $this->links; |
||||
216 | } |
||||
217 | |||||
218 | /** |
||||
219 | * Set links |
||||
220 | * |
||||
221 | * @param Link[] $links |
||||
222 | * @return $this |
||||
223 | * @throws LogicException |
||||
224 | */ |
||||
225 | View Code Duplication | public function setLinks(array $links) |
|||
226 | { |
||||
227 | foreach ($links as $key => $link) { |
||||
228 | if (!($link instanceof Link)) { |
||||
229 | $exMsg = sprintf( |
||||
230 | 'Array key `%s` is not an instance of Link', |
||||
231 | $key |
||||
232 | ); |
||||
233 | throw new LogicException($exMsg); |
||||
234 | } |
||||
235 | } |
||||
236 | |||||
237 | $this->links = $links; |
||||
238 | |||||
239 | return $this; |
||||
240 | } |
||||
241 | |||||
242 | /** |
||||
243 | * Add link |
||||
244 | * |
||||
245 | * @param Link $link |
||||
246 | * @return $this |
||||
247 | */ |
||||
248 | public function addLink(Link $link) |
||||
249 | { |
||||
250 | $this->links[] = $link; |
||||
251 | |||||
252 | return $this; |
||||
253 | } |
||||
254 | |||||
255 | /** |
||||
256 | * Convert object to array |
||||
257 | * |
||||
258 | * @return array |
||||
259 | */ |
||||
260 | public function toArray() |
||||
261 | { |
||||
262 | $data = array(); |
||||
263 | |||||
264 | $class = $this->getClass(); |
||||
265 | if (!empty($class)) { |
||||
266 | $data['class'] = $class; |
||||
267 | } |
||||
268 | |||||
269 | $rel = $this->getRel(); |
||||
270 | if (!empty($rel)) { |
||||
271 | $data['rel'] = $rel; |
||||
272 | } |
||||
273 | |||||
274 | $href = $this->getHref(); |
||||
275 | if ($href !== null) { |
||||
276 | $data['href'] = $href; |
||||
277 | } |
||||
278 | |||||
279 | $properties = $this->getProperties(); |
||||
280 | if (!empty($properties)) { |
||||
281 | $data['properties'] = $properties; |
||||
282 | } |
||||
283 | |||||
284 | $links = $this->getLinks(); |
||||
285 | View Code Duplication | if (!empty($links)) { |
|||
286 | $data['links'] = array(); |
||||
287 | foreach ($links as $link) { |
||||
288 | $data['links'][] = $link->toArray(); |
||||
289 | } |
||||
290 | } |
||||
291 | |||||
292 | return $data; |
||||
293 | } |
||||
294 | } |
||||
295 |