also looking at this
Tags model
#14
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.
| @@ -6,7 +6,7 @@ use Dotenv\Dotenv; | ||
| 6 | 6 | use GuzzleHttp\Client as GuzzleClient; |
| 7 | 7 | use Meema\MeemaApi\Models\Folder; |
| 8 | 8 | use Meema\MeemaApi\Models\Media; |
| 9 | use Meema\MeemaApi\Models\Team; | |
| 9 | use Meema\MeemaApi\Models\Tag; | |
| 10 | 10 | |
| 11 | 11 | class Client |
| 12 | 12 | { |
| @@ -108,12 +108,12 @@ class Client | ||
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
| 111 | * Initialize the team model. | |
| 111 | * Initialize the media model. | |
| 112 | 112 | * |
| 113 | * @return Meema\MeemaApi\Models\Team | |
| 113 | * @return Meema\MeemaApi\Models\Tag | |
| 114 | 114 | */ |
| 115 | public function teams(): Team | |
| 115 | public function tags(): Tag | |
| 116 | 116 | { |
| 117 | return new Team($this); | |
| 117 | return new Tag($this); | |
| 118 | 118 | } |
| 119 | 119 | } |
| @@ -15,7 +15,7 @@ class Folder | ||
| 15 | 15 | /** |
| 16 | 16 | * @var int |
| 17 | 17 | */ |
| 18 | protected $id; | |
| 18 | public $id; | |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * @var object |
| @@ -176,15 +176,16 @@ class Folder | ||
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | /** |
| 179 | * Add folder to media. | |
| 179 | * Delete folder from media. | |
| 180 | 180 | * |
| 181 | 181 | * @param int $id |
| 182 | * @param int $folderId | |
| 182 | 183 | * |
| 183 | 184 | * @return null |
| 184 | 185 | */ |
| 185 | public function deleteFolderFromMedia($mediaId, $folderId) | |
| 186 | public function deleteFolderFromMedia($id, $folderId) | |
| 186 | 187 | { |
| 187 | return $this->client->request('DELETE', "media/{$mediaId}/folders/{$folderId}"); | |
| 188 | return $this->client->request('DELETE', "media/{$id}/folders/{$folderId}"); | |
| 188 | 189 | } |
| 189 | 190 | |
| 190 | 191 | /** |
| @@ -222,4 +223,16 @@ class Folder | ||
| 222 | 223 | |
| 223 | 224 | return (new Media($this->client))->setFolder($this); |
| 224 | 225 | } |
| 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 | } | |
| 225 | 238 | } |
| @@ -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,18 +189,6 @@ class Media | ||
| 189 | 189 | return (new Folder($this->client))->setMedia($this); |
| 190 | 190 | } |
| 191 | 191 | |
| 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 | ||
| 204 | 192 | /** |
| 205 | 193 | * Initialize media model. |
| 206 | 194 | * |
| @@ -224,4 +212,5 @@ class Media formatting only | ||
| 224 | 212 | { |
| 225 | 213 | return $this->id; |
| 226 | 214 | } |
| 215 | ||
| 227 | 216 | } |
| @@ -0,0 +1,208 @@ | ||
| 1 | <?php | |
| 2 | ||
| 3 | namespace Meema\MeemaApi\Models; | |
| 4 | ||
| 5 | use Meema\MeemaApi\Client; | |
| 6 | use Meema\MeemaApi\Response\Response; | |
| 7 | use Meema\MeemaApi\Traits\SerializesResponse; | |
| 8 | ||
| 9 | class 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 | } | |