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
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+30-5
Changes to src/Models/Folder.php
@@ -71,8 +71,6 @@ class Folder
7171 {
7272 $response = $this->client->request('GET', "folders/${id}");
7373
74 $this->id = $response['data']['id'];
75
7674 return new Response($this, $response);
7775 }
7876
@@ -176,15 +174,16 @@ class Folder
176174 }
177175
178176 /**
179 * Add folder to media.
177 * Delete folder from media.
180178 *
181179 * @param int $id
180 * @param int $folderId
182181 *
183182 * @return null
184183 */
185 public function deleteFolderFromMedia($mediaId, $folderId)
184 public function deleteFolderFromMedia($id, $folderId)
186185 {
187 return $this->client->request('DELETE', "media/{$mediaId}/folders/{$folderId}");
186 return $this->client->request('DELETE', "media/{$id}/folders/{$folderId}");
188187 }
189188
190189 /**
@@ -201,6 +200,20 @@ class Folder
201200 return $this;
202201 }
203202
203 /**
204 * Initialize media model.
205 *
206 * @param Meema\MeemaApi\Models\Folder $folder
207 *
208 * @return self
209 */
210 public function setTag($tag): self
211 {
212 $this->model = $tag;
213
214 return $this;
215 }
216
204217 /**
205218 * Get the protected id.
206219 *
@@ -222,4 +235,16 @@ class Folder
222235
223236 return (new Media($this->client))->setFolder($this);
224237 }
238
239 /**
240 * Initialize the tags model.
241 *
242 * @return Meema\MeemaApi\Models\Tag
243 */
244 public function tags($id = null): Tag
245 {
246 $this->id = $id;
247
248 return (new Tag($this->client))->setFolder($this);
249 }
225250}
src/Models/Media.phpmodified+68-6
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,16 +189,30 @@ class Media
189189 return (new Folder($this->client))->setMedia($this);
190190 }
191191
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
192204 /**
193205 * Fetch the media for the folder.
194206 *
195207 * @param int $id
196208 *
197 * @return array
209 * @return self
198210 */
199 public function fetchMediaForFolder($id): array
211 public function setFolder($folder): self
200212 {
201 return $this->client->request('GET', "folders/{$id}/media");
213 $this->model = $folder;
214
215 return $this;
202216 }
203217
204218 /**
@@ -208,9 +222,9 @@ class Media
208222 *
209223 * @return self
210224 */
211 public function setFolder($folder): self
225 public function setTag($tag): self
212226 {
213 $this->model = $folder;
227 $this->model = $tag;
214228
215229 return $this;
216230 }
@@ -224,4 +238,52 @@ class Media
224238 {
225239 return $this->id;
226240 }
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 }
227289}
src/Models/Folder.php src/Models/Tag.phpcopied+60-57
Changes to src/Models/Tag.php
@@ -5,7 +5,7 @@ namespace Meema\MeemaApi\Models;
55use Meema\MeemaApi\Client;
66use Meema\MeemaApi\Response\Response;
77
8class Folder
8class Tag
99{
1010 /**
1111 * @var \Meema\MeemaApi\Client
@@ -33,35 +33,39 @@ class Folder
3333 }
3434
3535 /**
36 * List all folders.
36 * List all tags.
3737 *
3838 * @return array
3939 */
4040 public function all(): array
4141 {
42 return $this->client->request('GET', 'folders');
42 return $this->client->request('GET', 'tags');
4343 }
4444
4545 /**
46 * Get specific folders.
46 * Get specific tags.
4747 *
4848 * @param int $id
4949 *
5050 * @return array
5151 */
52 public function get($id = null)
52 public function get($id = null): array
5353 {
54 if ($this->model) {
55 return $this->fetchForModel();
56 }
57
5458 if (! $id) {
5559 return $this->all();
5660 }
5761
5862 $ids = is_array($id) ? $id : func_get_args();
5963
60 return $this->client->request('GET', 'folders', ['folder_ids' => $ids]);
64 return $this->client->request('GET', 'tags', ['tag_ids' => $ids]);
6165 }
6266
6367 /**
64 * Get specific folders.
68 * Get specific tags.
6569 *
6670 * @param int $id
6771 *
@@ -69,9 +73,7 @@ class Folder
6973 */
7074 public function find($id): Response
7175 {
72 $response = $this->client->request('GET', "folders/${id}");
73
74 $this->id = $response['data']['id'];
76 $response = $this->client->request('GET', "tags/${id}");
7577
7678 return new Response($this, $response);
7779 }
formatting only · 8 hidden lines
Changes to src/Models/Tag.php
@@ -79,7 +81,7 @@ class Folder formatting only
7981 /**
8082 * Create folder.
8183 *
82 * @param string $name
84 * @param string $name
8385 *
8486 * @return array
8587 */
Changes to src/Models/Tag.php
@@ -87,13 +89,7 @@ class Folder
8789 {
8890 $name = is_array($name) ? $name : compact('name');
8991
90 if ($this->model) {
91 $folderName = ['folder_name' => $name['name']];
92
93 return $this->addFolderToMedia($this->model->getId(), $folderName);
94 }
95
96 return $this->client->request('POST', "folders/{$this->client->getAccessKey()}", $name);
92 return $this->client->request('POST', "tags/{$this->client->getAccessKey()}", $name);
9793 }
9894
9995 /**
@@ -104,11 +100,9 @@ class Folder
104100 *
105101 * @return array
106102 */
107 public function update($id, $name): array
103 public function update($id, $data): array
108104 {
109 $name = is_array($name) ? $name : compact('name');
110
111 return $this->client->request('PATCH', "folders/{$id}", $name);
105 return $this->client->request('PATCH', 'tags/color', $data);
112106 }
113107
114108 /**
@@ -116,75 +110,73 @@ class Folder
116110 *
117111 * @param int $id
118112 *
119 * @return null
113 * @return Response
120114 */
121115 public function delete($id)
122116 {
123 if ($this->model) {
124 return $this->deleteFolderFromMedia($this->model->getId(), $id);
125 }
126
127 return $this->client->request('DELETE', "folders/{$id}");
117 return $this->client->request('DELETE', "tags/{$id}");
128118 }
129119
130120 /**
131 * Archive a folder.
132 *
133 * @param int $id
121 * Initialize the media model.
134122 *
135 * @return array
123 * @return Meema\MeemaApi\Models\Media
136124 */
137 public function archive($id): array
125 public function media($id = null): Media
138126 {
139 return $this->client->request('POST', "folders/{$id}/archive");
127 $this->id = $id;
128
129 return (new Media($this->client))->setTag($this);
140130 }
141131
142132 /**
143 * Unarchive a folder.
144 *
145 * @param int $id
133 * Initialize the folder model.
146134 *
147 * @return array
135 * @return Meema\MeemaApi\Models\Folder
148136 */
149 public function unarchive($id): array
137 public function folders($id = null): Folder
150138 {
151 return $this->client->request('POST', "folders/{$id}/unarchive");
139 $this->id = $id;
140
141 return (new Folder($this->client))->setTag($this);
152142 }
153143
154144 /**
155 * Duplicate a folder.
145 * Fetch the tags for media.
156146 *
157147 * @param int $id
158148 *
159149 * @return array
160150 */
161 public function duplicate($id): array
151 public function fetchTagsForMedia($id): array
162152 {
163 return $this->client->request('POST', "folders/{$id}/duplicate");
153 return $this->client->request('GET', "media/{$id}/tags");
164154 }
165155
166156 /**
167 * Add folder to media.
157 * Fetch the tags for folders.
168158 *
169159 * @param int $id
170160 *
171161 * @return array
172162 */
173 public function addFolderToMedia($id, $name): array
163 public function fetchTagsForFolder($id): array
174164 {
175 return $this->client->request('POST', "media/{$id}/folders", $name);
165 return $this->client->request('GET', "folders/{$id}/tags");
176166 }
177167
178168 /**
179 * Add folder to media.
169 * Initialize media model.
180170 *
181 * @param int $id
171 * @param Meema\MeemaApi\Models\Folder $folder
182172 *
183 * @return null
173 * @return self
184174 */
185 public function deleteFolderFromMedia($mediaId, $folderId)
175 public function setFolder($folder): self
186176 {
187 return $this->client->request('DELETE', "media/{$mediaId}/folders/{$folderId}");
177 $this->model = $folder;
178
179 return $this;
188180 }
189181
190182 /**
@@ -206,20 +198,31 @@ class Folder
206198 *
207199 * @return int
208200 */
209 public function getId(): int
201 public function getId()
210202 {
211203 return $this->id;
212204 }
213205
214206 /**
215 * Initialize the media model.
207 * Fetch child relations for this instance.
216208 *
217 * @return Meema\MeemaApi\Models\Media
209 * @return array
218210 */
219 public function media($id = null): Media
211 public function fetchForModel(): array
220212 {
221 $this->id = $id;
213 $data = [];
214
215 switch (get_class($this->model)) {
216 case Folder::class:
217 $data = $this->fetchTagsForFolder($this->model->getId());
218 break;
219 case Media::class:
220 $data = $this->fetchTagsForMedia($this->model->getId());
221 break;
222 default:
223 $data = [];
224 }
222225
223 return (new Media($this->client))->setFolder($this);
226 return $data;
224227 }
225228}
src/Response/Response.phpmodified+2-1
Changes to src/Response/Response.php
@@ -45,7 +45,7 @@ class Response
4545 *
4646 * @return string
4747 */
48 public function getKeyName()
48 public function getKeyName(): string
4949 {
5050 return 'id';
5151 }
@@ -74,6 +74,7 @@ class Response
7474 * Get response content.
7575 *
7676 * @param mixed $key
77 *
7778 * @return array
7879 */
7980 public function getContent($key = null)