also looking at this
Tags model
#14
5 files
+165
-74
| @@ -50,7 +50,7 @@ class Media | ||
| 50 | 50 | public function get($id = null) |
| 51 | 51 | { |
| 52 | 52 | if ($this->model) { |
| 53 | return $this->fetchMediaForFolder($this->model->getId()); | |
| 53 | return $this->fetchForModel(); | |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | if (! $id) { |
| @@ -189,16 +189,30 @@ class Media | ||
| 189 | 189 | return (new Folder($this->client))->setMedia($this); |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | /** | |
| 193 | * Initialize the tags model. | |
| 194 | * | |
| 195 | * @return Meema\MeemaApi\Models\Tag | |
| 196 | */ | |
| 197 | public function tags($id = null): Tag | |
| 198 | { | |
| 199 | $this->id = $id; | |
| 200 | ||
| 201 | return (new Tag($this->client))->setMedia($this); | |
| 202 | } | |
| 203 | ||
| 192 | 204 | /** |
| 193 | 205 | * Fetch the media for the folder. |
| 194 | 206 | * |
| 195 | 207 | * @param int $id |
| 196 | 208 | * |
| 197 | * @return array | |
| 209 | * @return self | |
| 198 | 210 | */ |
| 199 | public function fetchMediaForFolder($id): array | |
| 211 | public function setFolder($folder): self | |
| 200 | 212 | { |
| 201 | return $this->client->request('GET', "folders/{$id}/media"); | |
| 213 | $this->model = $folder; | |
| 214 | ||
| 215 | return $this; | |
| 202 | 216 | } |
| 203 | 217 | |
| 204 | 218 | /** |
| @@ -208,9 +222,9 @@ class Media | ||
| 208 | 222 | * |
| 209 | 223 | * @return self |
| 210 | 224 | */ |
| 211 | public function setFolder($folder): self | |
| 225 | public function setTag($tag): self | |
| 212 | 226 | { |
| 213 | $this->model = $folder; | |
| 227 | $this->model = $tag; | |
| 214 | 228 | |
| 215 | 229 | return $this; |
| 216 | 230 | } |
| @@ -224,4 +238,52 @@ class Media | ||
| 224 | 238 | { |
| 225 | 239 | return $this->id; |
| 226 | 240 | } |
| 241 | ||
| 242 | /** | |
| 243 | * Fetch the media for the folder. | |
| 244 | * | |
| 245 | * @param int $id | |
| 246 | * | |
| 247 | * @return array | |
| 248 | */ | |
| 249 | public function fetchMediaForFolder($id): array | |
| 250 | { | |
| 251 | return $this->client->request('GET', "folders/{$id}/media"); | |
| 252 | } | |
| 253 | ||
| 254 | /** | |
| 255 | * Fetch the media for tags. | |
| 256 | * | |
| 257 | * @param int $id | |
| 258 | * | |
| 259 | * @return array | |
| 260 | */ | |
| 261 | public function fetchMediaForTag($id): array | |
| 262 | { | |
| 263 | return $this->client->request('GET', "tags/{$id}/media"); | |
| 264 | } | |
| 265 | ||
| 266 | /** | |
| 267 | * Fetch child relations for this instance. | |
| 268 | * | |
| 269 | * @return array | |
| 270 | */ | |
| 271 | public function fetchForModel() | |
| 272 | { | |
| 273 | $data = []; | |
| 274 | ||
| 275 | switch (get_class($this->model)) { | |
| 276 | case Folder::class: | |
| 277 | $data = $this->fetchMediaForFolder($this->model->getId()); | |
| 278 | break; | |
| 279 | case Tag::class: | |
| 280 | $data = $this->fetchMediaForTag($this->model->getId()); | |
| 281 | break; | |
| 282 | default: | |
| 283 | $data = []; | |
| 284 | break; | |
| 285 | } | |
| 286 | ||
| 287 | return $data; | |
| 288 | } | |
| 227 | 289 | } |