|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mechpave\ImgurClient\Mapper; |
|
4
|
|
|
|
|
5
|
|
|
use Mechpave\ImgurClient\Entity\AlbumInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class AlbumMapper |
|
9
|
|
|
* @package Mechpave\ImgurClient\Mapper |
|
10
|
|
|
*/ |
|
11
|
|
|
class AlbumMapper |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var ObjectMapper |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $objectMapper; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ImageMapper |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $imageMapper; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* AlbumMapper constructor. |
|
25
|
|
|
* @param ObjectMapper $objectMapper |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public function __construct(ObjectMapper $objectMapper, ImageMapper $imageMapper) |
|
28
|
|
|
{ |
|
29
|
2 |
|
$this->objectMapper = $objectMapper; |
|
30
|
2 |
|
$this->imageMapper = $imageMapper; |
|
31
|
2 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Build Album object from raw data |
|
35
|
|
|
* @param array $albumData |
|
36
|
|
|
* @return AlbumInterface |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function buildAlbum(array $albumData) |
|
39
|
|
|
{ |
|
40
|
2 |
|
if (empty($albumData['id'])) { |
|
41
|
|
|
throw new \InvalidArgumentException(); |
|
42
|
|
|
} |
|
43
|
2 |
|
$insertedIntoGallery = null; |
|
44
|
2 |
|
if (!empty($albumData['datetime'])) { |
|
45
|
2 |
|
$insertedIntoGallery = new \DateTime(); |
|
46
|
2 |
|
$insertedIntoGallery->setTimestamp((int)$albumData['datetime']); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
$albumClass = $this->objectMapper->getAlbumClass(); |
|
50
|
|
|
|
|
51
|
|
|
/** @var AlbumInterface $album */ |
|
52
|
2 |
|
$album = new $albumClass(); |
|
53
|
|
|
$album |
|
54
|
2 |
|
->setAlbumId($albumData['id']) |
|
55
|
2 |
|
->setTitle(!empty($albumData['title']) ? $albumData['title'] : null) |
|
56
|
2 |
|
->setDescription(!empty($albumData['description']) ? $albumData['description'] : null) |
|
57
|
2 |
|
->setInsertedIntoGallery($insertedIntoGallery) |
|
|
|
|
|
|
58
|
2 |
|
->setCover(!empty($albumData['cover']) ? $albumData['cover'] : null) |
|
59
|
2 |
|
->setCoverWidth(!empty($albumData['cover_width']) ? $albumData['cover_width'] : null) |
|
60
|
2 |
|
->setCoverHeight(!empty($albumData['cover_height']) ? $albumData['cover_height'] : null) |
|
61
|
2 |
|
->setAccountUsername(!empty($albumData['account_url']) ? $albumData['account_url'] : null) |
|
62
|
2 |
|
->setAccountId(!empty($albumData['account_id']) ? $albumData['account_id'] : null) |
|
63
|
2 |
|
->setPrivacy(!empty($albumData['privacy']) ? $albumData['privacy'] : null) |
|
64
|
2 |
|
->setLayout(!empty($albumData['layout']) ? $albumData['layout'] : null) |
|
65
|
2 |
|
->setViews(!empty($albumData['views']) ? $albumData['views'] : null) |
|
66
|
2 |
|
->setLink(!empty($albumData['link']) ? $albumData['link'] : null) |
|
67
|
2 |
|
->setFavorite(!empty($albumData['favorite']) ? $albumData['favorite'] : null) |
|
68
|
2 |
|
->setNsfw(!empty($albumData['nsfw']) ? $albumData['nsfw'] : null) |
|
69
|
2 |
|
->setSection(!empty($albumData['section']) ? $albumData['section'] : null) |
|
70
|
2 |
|
->setOrder(!empty($albumData['order']) ? $albumData['order'] : null) |
|
71
|
2 |
|
->setDeleteHash(!empty($albumData['deletehash']) ? $albumData['deletehash'] : null) |
|
72
|
2 |
|
->setImagesCount(!empty($albumData['images_count']) ? $albumData['images_count'] : null); |
|
73
|
|
|
|
|
74
|
2 |
|
$images = []; |
|
75
|
2 |
|
if (!empty($albumData['images'])) { |
|
76
|
|
|
foreach ($albumData['images'] as $imageData) { |
|
77
|
|
|
$image = $this->imageMapper->buildImage($imageData); |
|
78
|
|
|
$images[] = $image; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
$album->setImages($images); |
|
83
|
|
|
|
|
84
|
2 |
|
return $album; |
|
85
|
|
|
} |
|
86
|
|
|
} |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: