1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mechpave\ImgurClient\Api; |
4
|
|
|
|
5
|
|
|
use Mechpave\ImgurClient\Entity\AlbumInterface; |
6
|
|
|
use Mechpave\ImgurClient\Http\HttpClientInterface; |
7
|
|
|
use Mechpave\ImgurClient\Mapper\AlbumMapper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Album |
11
|
|
|
* @package Mechpave\ImgurClient\Api |
12
|
|
|
* @see http://api.imgur.com/endpoints/album |
13
|
|
|
*/ |
14
|
|
|
class Album extends AbstractApi |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var AlbumMapper |
18
|
|
|
*/ |
19
|
|
|
protected $albumMapper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Album constructor. |
23
|
|
|
* @param HttpClientInterface $httpClient |
24
|
|
|
* @param AlbumMapper $albumMapper |
25
|
|
|
*/ |
26
|
|
|
public function __construct(HttpClientInterface $httpClient, AlbumMapper $albumMapper) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($httpClient); |
29
|
|
|
$this->albumMapper = $albumMapper; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get information about a specific album |
34
|
|
|
* |
35
|
|
|
* @param $id |
36
|
|
|
* @return AlbumInterface |
37
|
|
|
*/ |
38
|
|
|
public function get($id) |
39
|
|
|
{ |
40
|
|
|
$response = $this->httpClient->get( |
41
|
|
|
'album/' . $id |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$album = $this->albumMapper->buildAlbum($response->getBody()['data']); |
45
|
|
|
|
46
|
|
|
return $album; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create a new album |
51
|
|
|
* |
52
|
|
|
* @param array $ids Ids of the images you want to add to the album |
53
|
|
|
* @param string $title |
54
|
|
|
* @param string $description |
55
|
|
|
* @param string $privacy |
56
|
|
|
* @param string $layout |
57
|
|
|
* @param string $cover |
58
|
|
|
* @return AlbumInterface |
59
|
|
|
*/ |
60
|
|
|
public function create( |
61
|
|
|
$ids = [], |
62
|
|
|
$title = null, |
63
|
|
|
$description = null, |
64
|
|
|
$privacy = null, |
65
|
|
|
$layout = null, |
66
|
|
|
$cover = null |
67
|
|
|
) |
68
|
|
|
{ |
69
|
|
|
$postData = []; |
70
|
|
|
|
71
|
|
|
if (!empty($ids)) { |
72
|
|
|
$postData['ids'] = implode(',', $ids); |
73
|
|
|
} |
74
|
|
|
if($title) { |
|
|
|
|
75
|
|
|
$postData['title'] = $title; |
76
|
|
|
} |
77
|
|
|
if($description) { |
|
|
|
|
78
|
|
|
$postData['description'] = $description; |
79
|
|
|
} |
80
|
|
|
if($privacy) { |
|
|
|
|
81
|
|
|
$postData['privacy'] = $privacy; |
82
|
|
|
} |
83
|
|
|
if($layout) { |
|
|
|
|
84
|
|
|
$postData['layout'] = $layout; |
85
|
|
|
} |
86
|
|
|
if($cover) { |
|
|
|
|
87
|
|
|
$postData['cover'] = $cover; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$response = $this->httpClient->post( |
91
|
|
|
'album', |
92
|
|
|
$postData |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$album = $this->albumMapper->buildAlbum($response->getBody()['data']); |
96
|
|
|
$album->setTitle($title); |
97
|
|
|
$album->setDescription($description); |
98
|
|
|
$album->setPrivacy($privacy); |
99
|
|
|
$album->setLayout($layout); |
100
|
|
|
$album->setCover($cover); |
101
|
|
|
|
102
|
|
|
return $album; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Update album |
107
|
|
|
* |
108
|
|
|
* @param string $id Imgur Id of the album |
109
|
|
|
* @param array $ids Ids of the images you want to add to the album |
110
|
|
|
* @param string $title |
111
|
|
|
* @param string $description |
112
|
|
|
* @param string $privacy |
113
|
|
|
* @param string $layout |
114
|
|
|
* @param string $cover |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function update( |
118
|
|
|
$id, |
119
|
|
|
$ids = [], |
120
|
|
|
$title = null, |
121
|
|
|
$description = null, |
122
|
|
|
$privacy = null, |
123
|
|
|
$layout = null, |
124
|
|
|
$cover = null |
125
|
|
|
) |
126
|
|
|
{ |
127
|
|
|
$postData = []; |
128
|
|
|
|
129
|
|
|
if (!empty($ids)) { |
130
|
|
|
$postData['ids'] = implode(',', $ids); |
131
|
|
|
} |
132
|
|
|
if($title) { |
|
|
|
|
133
|
|
|
$postData['title'] = $title; |
134
|
|
|
} |
135
|
|
|
if($description) { |
|
|
|
|
136
|
|
|
$postData['description'] = $description; |
137
|
|
|
} |
138
|
|
|
if($privacy) { |
|
|
|
|
139
|
|
|
$postData['privacy'] = $privacy; |
140
|
|
|
} |
141
|
|
|
if($layout) { |
|
|
|
|
142
|
|
|
$postData['layout'] = $layout; |
143
|
|
|
} |
144
|
|
|
if($cover) { |
|
|
|
|
145
|
|
|
$postData['cover'] = $cover; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$response = $this->httpClient->post( |
149
|
|
|
'album/' . $id, |
150
|
|
|
$postData |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
return $response->getBody()['success']; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Delete an album |
158
|
|
|
* |
159
|
|
|
* @param string $id Imgur Id or deletehash of the album |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function delete($id) |
163
|
|
|
{ |
164
|
|
|
$response = $this->httpClient->delete( |
165
|
|
|
'album/' . $id |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
return $response->getBody()['success']; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Favorite an album |
173
|
|
|
* |
174
|
|
|
* @param string $id |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function favorite($id) |
178
|
|
|
{ |
179
|
|
|
$response = $this->httpClient->post( |
180
|
|
|
'album/' . $id . '/favorite' |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
return $response->getBody()['success']; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Sets the images for an album, removes all other images and only uses the images in this request. |
188
|
|
|
* |
189
|
|
|
* @param string $id |
190
|
|
|
* @param array $ids |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
View Code Duplication |
public function setImages($id, array $ids) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
if (empty($ids)) { |
196
|
|
|
throw new \InvalidArgumentException('Images ids must be not empty array'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$postData = []; |
200
|
|
|
$postData['ids'] = implode(',', $ids); |
201
|
|
|
|
202
|
|
|
$response = $this->httpClient->post( |
203
|
|
|
'album/' . $id, |
204
|
|
|
$postData |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
return $response->getBody()['success']; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Adds images to an album |
212
|
|
|
* |
213
|
|
|
* @param string $id |
214
|
|
|
* @param array $ids |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
|
View Code Duplication |
public function addImages($id, array $ids) |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
if (empty($ids)) { |
220
|
|
|
throw new \InvalidArgumentException('Images ids must be not empty array'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$postData = []; |
224
|
|
|
$postData['ids'] = implode(',', $ids); |
225
|
|
|
|
226
|
|
|
$response = $this->httpClient->post( |
227
|
|
|
'album/' . $id . '/add', |
228
|
|
|
$postData |
229
|
|
|
); |
230
|
|
|
|
231
|
|
|
return $response->getBody()['success']; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Removes images from an album |
236
|
|
|
* |
237
|
|
|
* @param string $id |
238
|
|
|
* @param array $ids |
239
|
|
|
* @return bool |
240
|
|
|
*/ |
241
|
|
View Code Duplication |
public function removeImages($id, array $ids) |
|
|
|
|
242
|
|
|
{ |
243
|
|
|
if (empty($ids)) { |
244
|
|
|
throw new \InvalidArgumentException('Images ids must be not empty array'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$postData = []; |
248
|
|
|
$postData['ids'] = implode(',', $ids); |
249
|
|
|
|
250
|
|
|
$response = $this->httpClient->delete( |
251
|
|
|
'album/' . $id . '/remove_images', |
252
|
|
|
$postData |
|
|
|
|
253
|
|
|
); |
254
|
|
|
|
255
|
|
|
return $response->getBody()['success']; |
256
|
|
|
} |
257
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: