-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAlbumOrImage.php
42 lines (37 loc) · 1.2 KB
/
AlbumOrImage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace Imgur\Api;
use Imgur\Exception\ErrorException;
use Imgur\Exception\ExceptionInterface;
/**
* This is a special endpoint.
* When you got an Imgur link it's almost impossible to be 100% sure if it's an image or an album.
* This endpoint aim to fix that by first checking an id as an image and if it's fail, test it as an album.
*/
class AlbumOrImage extends AbstractApi
{
/**
* Try to find an image or an album using the given parameter.
*
* @param string $imageIdOrAlbumId
*
* @return array Album (@see https://api.imgur.com/models/album) OR Image (@see https://api.imgur.com/models/image)
*/
public function find($imageIdOrAlbumId)
{
try {
return $this->get('image/' . $imageIdOrAlbumId);
} catch (ExceptionInterface $e) {
if (404 !== $e->getCode()) {
throw $e;
}
}
try {
return $this->get('album/' . $imageIdOrAlbumId);
} catch (ExceptionInterface $e) {
if (404 !== $e->getCode()) {
throw $e;
}
}
throw new ErrorException('Unable to find an album OR an image with the id, ' . $imageIdOrAlbumId);
}
}