ReviewOS

also looking at this

meemalabs/meema-client-php

Tags model

#14
Merged glennmichael123 wants to merge tags-model-2 into main
5 files +165 -74

Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.

src/Client.phpmodified+5-5
Changes to src/Client.php
@@ -6,7 +6,7 @@ use Dotenv\Dotenv;
66use GuzzleHttp\Client as GuzzleClient;
77use Meema\MeemaApi\Models\Folder;
88use Meema\MeemaApi\Models\Media;
9use Meema\MeemaApi\Models\Team;
9use Meema\MeemaApi\Models\Tag;
1010
1111class Client
1212{
@@ -108,12 +108,12 @@ class Client
108108 }
109109
110110 /**
111 * Initialize the team model.
111 * Initialize the media model.
112112 *
113 * @return Meema\MeemaApi\Models\Team
113 * @return Meema\MeemaApi\Models\Tag
114114 */
115 public function teams(): Team
115 public function tags(): Tag
116116 {
117 return new Team($this);
117 return new Tag($this);
118118 }
119119}
src/Models/Folder.phpmodified+17-4
Changes to src/Models/Folder.php
@@ -15,7 +15,7 @@ class Folder
1515 /**
1616 * @var int
1717 */
18 protected $id;
18 public $id;
1919
2020 /**
2121 * @var object
@@ -176,15 +176,16 @@ class Folder
176176 }
177177
178178 /**
179 * Add folder to media.
179 * Delete folder from media.
180180 *
181181 * @param int $id
182 * @param int $folderId
182183 *
183184 * @return null
184185 */
185 public function deleteFolderFromMedia($mediaId, $folderId)
186 public function deleteFolderFromMedia($id, $folderId)
186187 {
187 return $this->client->request('DELETE', "media/{$mediaId}/folders/{$folderId}");
188 return $this->client->request('DELETE', "media/{$id}/folders/{$folderId}");
188189 }
189190
190191 /**
@@ -222,4 +223,16 @@ class Folder
222223
223224 return (new Media($this->client))->setFolder($this);
224225 }
226
227 /**
228 * Initialize the tags model.
229 *
230 * @return Meema\MeemaApi\Models\Tag
231 */
232 public function tags(): Tag
233 {
234 $client = new Client($this->client->getAccessKey());
235
236 return (new Tag($client))->setFolder($this);
237 }
225238}
src/Models/Media.phpmodified+2-13
Changes to src/Models/Media.php
@@ -50,7 +50,7 @@ class Media
5050 public function get($id = null)
5151 {
5252 if ($this->model) {
53 return $this->fetchMediaForFolder($this->model->getId());
53 return $this->fetchForModel();
5454 }
5555
5656 if (! $id) {
@@ -189,18 +189,6 @@ class Media
189189 return (new Folder($this->client))->setMedia($this);
190190 }
191191
192 /**
193 * Fetch the media for the folder.
194 *
195 * @param int $id
196 *
197 * @return array
198 */
199 public function fetchMediaForFolder($id): array
200 {
201 return $this->client->request('GET', "folders/{$id}/media");
202 }
203
204192 /**
205193 * Initialize media model.
206194 *
@@ -224,4 +212,5 @@ class Media formatting only
224212 {
225213 return $this->id;
226214 }
215
227216}
src/Models/Tag.phpadded+208-0
Changes to src/Models/Tag.php
@@ -0,0 +1,208 @@
1<?php
2
3namespace Meema\MeemaApi\Models;
4
5use Meema\MeemaApi\Client;
6use Meema\MeemaApi\Response\Response;
7use Meema\MeemaApi\Traits\SerializesResponse;
8
9class Tag
10{
11 use SerializesResponse;
12
13 /**
14 * @var Meema\MeemaApi\Client
15 */
16 protected $client;
17
18 /**
19 * @var int
20 */
21 public $id;
22
23 /**
24 * @var object
25 */
26 protected $model;
27
28 /**
29 * Construct Folder model.
30 *
31 * @param Meema\MeemaApi\Client $client
32 */
33 public function __construct(Client $client)
34 {
35 $this->client = $client;
36 }
37
38 /**
39 * List all tags.
40 *
41 * @return array
42 */
43 public function all(): array
44 {
45 return $this->client->request('GET', 'tags');
46 }
47
48 /**
49 * Get specific tags.
50 *
51 * @param int $id
52 *
53 * @return array
54 */
55 public function get($id = null)
56 {
57 if ($this->model) {
58 return $this->fetchForModel();
59 }
60
61 if (! $id) {
62 return $this->all();
63 }
64
65 $ids = is_array($id) ? $id : func_get_args();
66
67 return $this->client->request('GET', 'tags', ['tag_ids' => $ids]);
68 }
69
70 /**
71 * Get specific tags.
72 *
73 * @param int $id
74 *
75 * @return array
76 */
77 public function find($id)
78 {
79 $response = $this->client->request('GET', "tags/${id}");
80
81 $this->id = $response['data']['id'];
82
83 return new Response($this, $response);
84 }
85
86 /**
87 * Create folder.
88 *
89 * @param string $name
90 *
91 * @return array
92 */
93 public function create($name)
94 {
95 $name = is_array($name) ? $name : compact('name');
96
97 return $this->client->request('POST', "tags/{$this->client->getAccessKey()}", $name);
98 }
99
100 /**
101 * Update folder.
102 *
103 * @param string $name
104 * @param int $id
105 *
106 * @return array
107 */
108 public function update($id, $data): array
109 {
110 return $this->client->request('PATCH', 'tags/color', $data);
111 }
112
113 /**
114 * Delete a folder.
115 *
116 * @param int $id
117 *
118 * @return null
119 */
120 public function delete($id)
121 {
122 $id = $this->id ?? $id;
123
124 return $this->client->request('DELETE', "tags/{$id}");
125 }
126
127
128 /**
129 * Initialize the media model.
130 *
131 * @return Meema\MeemaApi\Models\Media
132 */
133 public function media(): Media
134 {
135 $client = new Client($this->client->getAccessKey());
136
137 return new Media($client, $this);
138 }
139
140 /**
141 * Initialize the folder model.
142 *
143 * @return Meema\MeemaApi\Models\Folder
144 */
145 public function folders(): Folder
146 {
147 $client = new Client($this->client->getAccessKey());
148
149 return new Folder($client, $this);
150 }
151
152 /**
153 * Fetch the tags for media.
154 *
155 * @param int $id
156 *
157 * @return array
158 */
159 public function fetchTagsForMedia($id): array
160 {
161 return $this->client->request('GET', "media/{$id}/tags");
162 }
163
164 /**
165 * Fetch the tags for folders.
166 *
167 * @param int $id
168 *
169 * @return array
170 */
171 public function fetchTagsForFolder($id): array
172 {
173 return $this->client->request('GET', "folders/{$id}/tags");
174 }
175
176 /**
177 * Initialize media model.
178 *
179 * @param Meema\MeemaApi\Models\Folder $folder
180 *
181 * @return self
182 */
183 public function setFolder($folder): self
184 {
185 $this->model = $folder;
186
187 return $this;
188 }
189
190 /**
191 * Fetch child relations for this instance.
192 *
193 * @return array
194 */
195 public function fetchForModel(): array
196 {
197 switch (get_class($this->model)) {
198 case Folder::class:
199 return $this->fetchTagsForFolder($this->model->id);
200 case Media::class:
201 return $this->fetchTagsForMedia($this->model->id);
202 default:
203 return [];
204 }
205
206 return [];
207 }
208}