also looking at this
Media model
#6
7 files
+1700
-76
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.
| @@ -3,27 +3,43 @@ | ||
| 3 | 3 | namespace Meema\MeemaApi\Models; |
| 4 | 4 | |
| 5 | 5 | use Meema\MeemaApi\Client; |
| 6 | use Meema\MeemaApi\Response\Response; | |
| 7 | use Meema\MeemaApi\Traits\SerializesResponse; | |
| 6 | 8 | |
| 7 | 9 | class Folder |
| 8 | 10 | { |
| 11 | use SerializesResponse; | |
| 12 | ||
| 9 | 13 | /** |
| 10 | 14 | * @var Meema\MeemaApi\Client |
| 11 | 15 | */ |
| 12 | public $client; | |
| 16 | protected $client; | |
| 17 | ||
| 18 | /** | |
| 19 | * @var int | |
| 20 | */ | |
| 21 | protected $id; | |
| 13 | 22 | |
| 14 | 23 | /** |
| 15 | 24 | * @var int |
| 16 | 25 | */ |
| 17 | public $id; | |
| 26 | protected $mediaId; | |
| 27 | ||
| 28 | /** | |
| 29 | * @var array | |
| 30 | */ | |
| 31 | protected $content; | |
| 18 | 32 | |
| 19 | 33 | /** |
| 20 | 34 | * Construct Folder model. |
| 21 | 35 | * |
| 22 | 36 | * @param Meema\MeemaApi\Client $client |
| 23 | 37 | */ |
| 24 | public function __construct(Client $client) | |
| 38 | public function __construct(Client $client, $mediaId = null) | |
| 25 | 39 | { |
| 26 | 40 | $this->client = $client; |
| 41 | ||
| 42 | $this->mediaId = $mediaId; | |
| 27 | 43 | } |
| 28 | 44 | |
| 29 | 45 | /** |
| @@ -39,6 +55,8 @@ class Folder | ||
| 39 | 55 | /** |
| 40 | 56 | * Get specific folders. |
| 41 | 57 | * |
| 58 | * @param int $id | |
| 59 | * | |
| 42 | 60 | * @return array |
| 43 | 61 | */ |
| 44 | 62 | public function get($id = null) |
| @@ -49,7 +67,24 @@ class Folder | ||
| 49 | 67 | |
| 50 | 68 | $ids = is_array($id) ? $id : func_get_args(); |
| 51 | 69 | |
| 52 | return $this->client->request('GET', 'folders/show', ['folder_ids' => $ids]); | |
| 70 | return $this->client->request('GET', 'folders', ['folder_ids' => $ids]); | |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * Get specific folders. | |
| 75 | * | |
| 76 | * @param int $id | |
| 77 | * | |
| 78 | * @return array | |
| 79 | */ | |
| 80 | public function find($id) | |
| 81 | { | |
| 82 | $response = $this->client->request('GET', "folders/${id}"); | |
| 83 | ||
| 84 | $this->content = $response; | |
| 85 | $this->id = $response['data']['id']; | |
| 86 | ||
| 87 | return new Response($this, $response); | |
| 53 | 88 | } |
| 54 | 89 | |
| 55 | 90 | /** |
| @@ -59,23 +94,31 @@ class Folder | ||
| 59 | 94 | * |
| 60 | 95 | * @return array |
| 61 | 96 | */ |
| 62 | public function create($name): array | |
| 97 | public function create($name) | |
| 63 | 98 | { |
| 64 | 99 | $name = is_array($name) ? $name : compact('name'); |
| 65 | 100 | |
| 101 | if ($this->mediaId) { | |
| 102 | $folderName = ['folder_name' => $name['name']]; | |
| 103 | ||
| 104 | return $this->addFolderToMedia($this->mediaId, $folderName); | |
| 105 | } | |
| 106 | ||
| 66 | 107 | return $this->client->request('POST', "folders/{$this->client->getAccessKey()}", $name); |
| 67 | 108 | } |
| 68 | 109 | |
| 69 | 110 | /** |
| 70 | 111 | * Update folder. |
| 71 | 112 | * |
| 72 | * @param int $id | |
| 73 | 113 | * @param string $name |
| 114 | * @param int $id | |
| 74 | 115 | * |
| 75 | 116 | * @return array |
| 76 | 117 | */ |
| 77 | public function update($id, $name): array | |
| 118 | public function update($name, $id = null): array | |
| 78 | 119 | { |
| 120 | $id = $this->id ?? $id; | |
| 121 | ||
| 79 | 122 | $name = is_array($name) ? $name : compact('name'); |
| 80 | 123 | |
| 81 | 124 | return $this->client->request('PATCH', "folders/{$id}", $name); |
| @@ -84,42 +127,96 @@ class Folder | ||
| 84 | 127 | /** |
| 85 | 128 | * Delete a folder. |
| 86 | 129 | * |
| 87 | * @param int|array $id | |
| 130 | * @param int $id | |
| 88 | 131 | * |
| 89 | 132 | * @return null |
| 90 | 133 | */ |
| 91 | public function delete($id) | |
| 134 | public function delete($id = null) | |
| 92 | 135 | { |
| 136 | if ($this->mediaId) { | |
| 137 | return $this->deleteFolderFromMedia($this->mediaId, $id); | |
| 138 | } | |
| 139 | ||
| 140 | $id = $this->id ?? $id; | |
| 141 | ||
| 93 | 142 | return $this->client->request('DELETE', "folders/{$id}"); |
| 94 | 143 | } |
| 95 | 144 | |
| 96 | 145 | /** |
| 97 | 146 | * Archive a folder. |
| 98 | 147 | * |
| 148 | * @param int $id | |
| 149 | * | |
| 99 | 150 | * @return array |
| 100 | 151 | */ |
| 101 | public function archive($id): array | |
| 152 | public function archive($id = null): array | |
| 102 | 153 | { |
| 154 | $id = $this->id ?? $id; | |
| 155 | ||
| 103 | 156 | return $this->client->request('POST', "folders/{$id}/archive"); |
| 104 | 157 | } |
| 105 | 158 | |
| 106 | 159 | /** |
| 107 | 160 | * Unarchive a folder. |
| 108 | 161 | * |
| 162 | * @param int $id | |
| 163 | * | |
| 109 | 164 | * @return array |
| 110 | 165 | */ |
| 111 | public function unarchive($id): array | |
| 166 | public function unarchive($id = null): array | |
| 112 | 167 | { |
| 168 | $id = $this->id ?? $id; | |
| 169 | ||
| 113 | 170 | return $this->client->request('POST', "folders/{$id}/unarchive"); |
| 114 | 171 | } |
| 115 | 172 | |
| 116 | 173 | /** |
| 117 | 174 | * Duplicate a folder. |
| 118 | 175 | * |
| 176 | * @param int $id | |
| 177 | * | |
| 119 | 178 | * @return array |
| 120 | 179 | */ |
| 121 | public function duplicate($id): array | |
| 180 | public function duplicate($id = null): array | |
| 122 | 181 | { |
| 182 | $id = $this->id ?? $id; | |
| 183 | ||
| 123 | 184 | return $this->client->request('POST', "folders/{$id}/duplicate"); |
| 124 | 185 | } |
| 186 | ||
| 187 | /** | |
| 188 | * Initialize the media model. | |
| 189 | * | |
| 190 | * @return Meema\MeemaApi\Models\Media | |
| 191 | */ | |
| 192 | public function media(): Media | |
| 193 | { | |
| 194 | $client = new Client($this->client->getAccessKey()); | |
| 195 | ||
| 196 | return new Media($client, $this->id); | |
| 197 | } | |
| 198 | ||
| 199 | /** | |
| 200 | * Add folder to media. | |
| 201 | * | |
| 202 | * @param int $id | |
| 203 | * | |
| 204 | * @return array | |
| 205 | */ | |
| 206 | public function addFolderToMedia($id, $name) | |
| 207 | { | |
| 208 | return $this->client->request('POST', "media/{$id}/folders", $name); | |
| 209 | } | |
| 210 | ||
| 211 | /** | |
| 212 | * Add folder to media. | |
| 213 | * | |
| 214 | * @param int $id | |
| 215 | * | |
| 216 | * @return array | |
| 217 | */ | |
| 218 | public function deleteFolderFromMedia($mediaId, $folderId) | |
| 219 | { | |
| 220 | return $this->client->request('DELETE', "media/{$mediaId}/folders/{$folderId}"); | |
| 221 | } | |
| 125 | 222 | } |
| @@ -3,27 +3,43 @@ | ||
| 3 | 3 | namespace Meema\MeemaApi\Models; |
| 4 | 4 | |
| 5 | 5 | use Meema\MeemaApi\Client; |
| 6 | use Meema\MeemaApi\Response\Response; | |
| 7 | use Meema\MeemaApi\Traits\SerializesResponse; | |
| 6 | 8 | |
| 7 | 9 | class Media |
| 8 | 10 | { |
| 11 | use SerializesResponse; | |
| 12 | ||
| 9 | 13 | /** |
| 10 | 14 | * @var Meema\MeemaApi\Client |
| 11 | 15 | */ |
| 12 | public $client; | |
| 16 | protected $client; | |
| 17 | ||
| 18 | /** | |
| 19 | * @var int | |
| 20 | */ | |
| 21 | protected $id; | |
| 13 | 22 | |
| 14 | 23 | /** |
| 15 | 24 | * @var int |
| 16 | 25 | */ |
| 17 | public $id; | |
| 26 | protected $folderId; | |
| 27 | ||
| 28 | /** | |
| 29 | * @var array | |
| 30 | */ | |
| 31 | protected $content; | |
| 18 | 32 | |
| 19 | 33 | /** |
| 20 | 34 | * Construct media model. |
| 21 | 35 | * |
| 22 | 36 | * @param Meema\MeemaApi\Client $client |
| 23 | 37 | */ |
| 24 | public function __construct(Client $client) | |
| 38 | public function __construct(Client $client, $folderId = null) | |
| 25 | 39 | { |
| 26 | 40 | $this->client = $client; |
| 41 | ||
| 42 | $this->folderId = $folderId; | |
| 27 | 43 | } |
| 28 | 44 | |
| 29 | 45 | /** |
| @@ -43,6 +59,10 @@ class Media | ||
| 43 | 59 | */ |
| 44 | 60 | public function get($id = null) |
| 45 | 61 | { |
| 62 | if ($this->folderId) { | |
| 63 | return $this->fetchMediaForFolder($this->folderId); | |
| 64 | } | |
| 65 | ||
| 46 | 66 | if (! $id) { |
| 47 | 67 | return $this->all(); |
| 48 | 68 | } |
| @@ -52,10 +72,27 @@ class Media | ||
| 52 | 72 | return $this->client->request('GET', 'media/show', ['media_ids' => $ids]); |
| 53 | 73 | } |
| 54 | 74 | |
| 75 | /** | |
| 76 | * Get specific folders. | |
| 77 | * | |
| 78 | * @param int $id | |
| 79 | * | |
| 80 | * @return array | |
| 81 | */ | |
| 82 | public function find($id) | |
| 83 | { | |
| 84 | $response = $this->client->request('GET', "folders/${id}"); | |
| 85 | ||
| 86 | $this->content = $response; | |
| 87 | $this->id = $response['data']['id']; | |
| 88 | ||
| 89 | return new Response($this, $response); | |
| 90 | } | |
| 91 | ||
| 55 | 92 | /** |
| 56 | 93 | * Create media. |
| 57 | 94 | * |
| 58 | * @param string $name | |
| 95 | * @param string $name | |
| 59 | 96 | * |
| 60 | 97 | * @return array |
| 61 | 98 | */ |
| @@ -74,8 +111,10 @@ class Media | ||
| 74 | 111 | * |
| 75 | 112 | * @return array |
| 76 | 113 | */ |
| 77 | public function update($id, $name): array | |
| 114 | public function update($name, $id = null): array | |
| 78 | 115 | { |
| 116 | $id = $this->id ?? $id; | |
| 117 | ||
| 79 | 118 | $name = is_array($name) ? $name : compact('name'); |
| 80 | 119 | |
| 81 | 120 | return $this->client->request('PATCH', "media/{$id}/file-name", $name); |
| @@ -84,62 +123,108 @@ class Media | ||
| 84 | 123 | /** |
| 85 | 124 | * Delete a media. |
| 86 | 125 | * |
| 87 | * @param int|array $id | |
| 126 | * @param int $id | |
| 88 | 127 | * |
| 89 | 128 | * @return null |
| 90 | 129 | */ |
| 91 | public function delete($id) | |
| 130 | public function delete($id = null) | |
| 92 | 131 | { |
| 132 | $id = $this->id ?? $id; | |
| 133 | ||
| 93 | 134 | return $this->client->request('DELETE', "media/{$id}", ['media_id' => $id]); |
| 94 | 135 | } |
| 95 | 136 | |
| 96 | 137 | /** |
| 97 | 138 | * Archive a media. |
| 98 | 139 | * |
| 140 | * @param int $id | |
| 141 | * | |
| 99 | 142 | * @return array |
| 100 | 143 | */ |
| 101 | public function archive($id): array | |
| 144 | public function archive($id = null): array | |
| 102 | 145 | { |
| 146 | $id = $this->id ?? $id; | |
| 147 | ||
| 103 | 148 | return $this->client->request('POST', "media/{$id}/archive"); |
| 104 | 149 | } |
| 105 | 150 | |
| 106 | 151 | /** |
| 107 | 152 | * Unarchive a media. |
| 108 | 153 | * |
| 154 | * @param int $id | |
| 155 | * | |
| 109 | 156 | * @return array |
| 110 | 157 | */ |
| 111 | public function unarchive($id): array | |
| 158 | public function unarchive($id = null): array | |
| 112 | 159 | { |
| 160 | $id = $this->id ?? $id; | |
| 161 | ||
| 113 | 162 | return $this->client->request('POST', "media/{$id}/unarchive"); |
| 114 | 163 | } |
| 115 | 164 | |
| 116 | 165 | /** |
| 117 | 166 | * Make a media private. |
| 118 | 167 | * |
| 168 | * @param int $id | |
| 169 | * | |
| 119 | 170 | * @return array |
| 120 | 171 | */ |
| 121 | public function makePrivate($id): array | |
| 172 | public function makePrivate($id = null): array | |
| 122 | 173 | { |
| 174 | $id = $this->id ?? $id; | |
| 175 | ||
| 123 | 176 | return $this->client->request('PATCH', "media/{$id}/make-private"); |
| 124 | 177 | } |
| 125 | 178 | |
| 126 | 179 | /** |
| 127 | 180 | * Make a media public. |
| 128 | 181 | * |
| 182 | * @param int $id | |
| 183 | * | |
| 129 | 184 | * @return array |
| 130 | 185 | */ |
| 131 | public function makePublic($id): array | |
| 186 | public function makePublic($id = null): array | |
| 132 | 187 | { |
| 188 | $id = $this->id ?? $id; | |
| 189 | ||
| 133 | 190 | return $this->client->request('PATCH', "media/{$id}/make-public"); |
| 134 | 191 | } |
| 135 | 192 | |
| 136 | 193 | /** |
| 137 | 194 | * Duplicate a media. |
| 138 | 195 | * |
| 196 | * @param int $id | |
| 197 | * | |
| 139 | 198 | * @return array |
| 140 | 199 | */ |
| 141 | public function duplicate($id): array | |
| 200 | public function duplicate($id = null): array | |
| 142 | 201 | { |
| 202 | $id = $this->id ?? $id; | |
| 203 | ||
| 143 | 204 | return $this->client->request('POST', "media/{$id}/duplicate"); |
| 144 | 205 | } |
| 206 | ||
| 207 | /** | |
| 208 | * Initialize the folder model. | |
| 209 | * | |
| 210 | * @return Meema\MeemaApi\Models\Folder | |
| 211 | */ | |
| 212 | public function folders(): Folder | |
| 213 | { | |
| 214 | $client = new Client($this->client->getAccessKey()); | |
| 215 | ||
| 216 | return new Folder($client, $this->id); | |
| 217 | } | |
| 218 | ||
| 219 | /** | |
| 220 | * Fetch the media for the folder. | |
| 221 | * | |
| 222 | * @param int $id | |
| 223 | * | |
| 224 | * @return array | |
| 225 | */ | |
| 226 | public function fetchMediaForFolder($id): array | |
| 227 | { | |
| 228 | return $this->client->request('GET', "folders/{$id}/media"); | |
| 229 | } | |
| 145 | 230 | } |
| @@ -0,0 +1,28 @@ | ||
| 1 | <?php | |
| 2 | ||
| 3 | namespace Meema\MeemaApi\Response; | |
| 4 | ||
| 5 | use Meema\MeemaApi\Traits\ForwardsCalls; | |
| 6 | ||
| 7 | class Response | |
| 8 | { | |
| 9 | use ForwardsCalls; | |
| 10 | ||
| 11 | protected $api; | |
| 12 | ||
| 13 | public function __construct($api) | |
| 14 | { | |
| 15 | $this->api = $api; | |
| 16 | } | |
| 17 | /** | |
| 18 | * Dynamically handle calls into the query instance. | |
| 19 | * | |
| 20 | * @param string $method | |
| 21 | * @param array $parameters | |
| 22 | * @return mixed | |
| 23 | */ | |
| 24 | public function __call($method, $parameters) | |
| 25 | { | |
| 26 | return $this->forwardCallTo($this->api, $method, $parameters); | |
| 27 | } | |
| 28 | } | |
| @@ -0,0 +1,54 @@ | ||
| 1 | <?php | |
| 2 | ||
| 3 | namespace Meema\MeemaApi\Traits; | |
| 4 | ||
| 5 | use BadMethodCallException; | |
| 6 | use Error; | |
| 7 | ||
| 8 | trait ForwardsCalls | |
| 9 | { | |
| 10 | /** | |
| 11 | * Forward a method call to the given object. | |
| 12 | * | |
| 13 | * @param mixed $object | |
| 14 | * @param string $method | |
| 15 | * @param array $parameters | |
| 16 | * @return mixed | |
| 17 | * | |
| 18 | * @throws \BadMethodCallException | |
| 19 | */ | |
| 20 | protected function forwardCallTo($object, $method, $parameters) | |
| 21 | { | |
| 22 | try { | |
| 23 | return $object->{$method}(...$parameters); | |
| 24 | } catch (Error | BadMethodCallException $e) { | |
| 25 | $pattern = '~^Call to undefined method (?P<class>[^:]+)::(?P<method>[^\(]+)\(\)$~'; | |
| 26 | ||
| 27 | if (! preg_match($pattern, $e->getMessage(), $matches)) { | |
| 28 | throw $e; | |
| 29 | } | |
| 30 | ||
| 31 | if ($matches['class'] != get_class($object) || | |
| 32 | $matches['method'] != $method) { | |
| 33 | throw $e; | |
| 34 | } | |
| 35 | ||
| 36 | static::throwBadMethodCallException($method); | |
| 37 | } | |
| 38 | } | |
| 39 | ||
| 40 | /** | |
| 41 | * Throw a bad method call exception for the given method. | |
| 42 | * | |
| 43 | * @param string $method | |
| 44 | * @return void | |
| 45 | * | |
| 46 | * @throws \BadMethodCallException | |
| 47 | */ | |
| 48 | protected static function throwBadMethodCallException($method) | |
| 49 | { | |
| 50 | throw new BadMethodCallException(sprintf( | |
| 51 | 'Call to undefined method %s::%s()', static::class, $method | |
| 52 | )); | |
| 53 | } | |
| 54 | } | |