also looking at this
Favorites model
#11
3 files
+131
-0
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.
| @@ -4,6 +4,7 @@ namespace Meema\MeemaApi; | ||
| 4 | 4 | |
| 5 | 5 | use Dotenv\Dotenv; |
| 6 | 6 | use GuzzleHttp\Client as GuzzleClient; |
| 7 | use Meema\MeemaApi\Models\Favorite; | |
| 7 | 8 | use Meema\MeemaApi\Models\Folder; |
| 8 | 9 | use Meema\MeemaApi\Models\Media; |
| 9 | 10 | use Meema\MeemaApi\Models\Tag; |
| @@ -116,4 +117,14 @@ class Client | ||
| 116 | 117 | { |
| 117 | 118 | return new Tag($this); |
| 118 | 119 | } |
| 120 | ||
| 121 | /** | |
| 122 | * Initialize the favorite model. | |
| 123 | * | |
| 124 | * @return Meema\MeemaApi\Models\Favorite | |
| 125 | */ | |
| 126 | public function favorites(): Favorite | |
| 127 | { | |
| 128 | return new Favorite($this); | |
| 129 | } | |
| 119 | 130 | } |
| @@ -0,0 +1,130 @@ | ||
| 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 Favorite | |
| 10 | { | |
| 11 | use SerializesResponse; | |
| 12 | ||
| 13 | /** | |
| 14 | * @var Meema\MeemaApi\Client | |
| 15 | */ | |
| 16 | protected $client; | |
| 17 | ||
| 18 | /** | |
| 19 | * @var int | |
| 20 | */ | |
| 21 | protected $id; | |
| 22 | ||
| 23 | /** | |
| 24 | * @var int | |
| 25 | */ | |
| 26 | protected $favoriteId; | |
| 27 | ||
| 28 | /** | |
| 29 | * @var array | |
| 30 | */ | |
| 31 | protected $content; | |
| 32 | ||
| 33 | /** | |
| 34 | * Construct Favorite model. | |
| 35 | * | |
| 36 | * @param Meema\MeemaApi\Client $client | |
| 37 | */ | |
| 38 | public function __construct(Client $client, $favoriteId = null) | |
| 39 | { | |
| 40 | $this->client = $client; | |
| 41 | ||
| 42 | $this->favoriteId = $favoriteId; | |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * List all favorites. | |
| 47 | * | |
| 48 | * @return array | |
| 49 | */ | |
| 50 | public function all(): array | |
| 51 | { | |
| 52 | return $this->client->request('GET', 'favorites'); | |
| 53 | } | |
| 54 | ||
| 55 | /** | |
| 56 | * Get specific favorites. | |
| 57 | * | |
| 58 | * @param int $id | |
| 59 | * | |
| 60 | * @return array | |
| 61 | */ | |
| 62 | public function get($id = null) | |
| 63 | { | |
| 64 | if (! $id) { | |
| 65 | return $this->all(); | |
| 66 | } | |
| 67 | ||
| 68 | $ids = is_array($id) ? $id : func_get_args(); | |
| 69 | ||
| 70 | return $this->client->request('GET', 'favorites', ['favorite_ids' => $ids]); | |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * Get specific favorites. | |
| 75 | * | |
| 76 | * @param int $id | |
| 77 | * | |
| 78 | * @return array | |
| 79 | */ | |
| 80 | public function find($id) | |
| 81 | { | |
| 82 | $response = $this->client->request('GET', "favorites/${id}"); | |
| 83 | ||
| 84 | $this->content = $response; | |
| 85 | $this->id = $response['data']['id']; | |
| 86 | ||
| 87 | return new Response($this, $response); | |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * Create folder. | |
| 92 | * | |
| 93 | * @param string $name | |
| 94 | * | |
| 95 | * @return array | |
| 96 | */ | |
| 97 | public function create($data) | |
| 98 | { | |
| 99 | return $this->client->request('POST', "favorites/{$this->client->getAccessKey()}", $data); | |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * Update folder. | |
| 104 | * | |
| 105 | * @param string $name | |
| 106 | * @param int $id | |
| 107 | * | |
| 108 | * @return array | |
| 109 | */ | |
| 110 | public function update($data, $id = null): array | |
| 111 | { | |
| 112 | $id = $this->id ?? $id; | |
| 113 | ||
| 114 | return $this->client->request('PATCH', "favorites/{$id}", $data); | |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * Delete a folder. | |
| 119 | * | |
| 120 | * @param int $id | |
| 121 | * | |
| 122 | * @return null | |
| 123 | */ | |
| 124 | public function delete($id = null) | |
| 125 | { | |
| 126 | $id = $this->id ?? $id; | |
| 127 | ||
| 128 | return $this->client->request('DELETE', "favorites/{$id}"); | |
| 129 | } | |
| 130 | } | |