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/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}