1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mechpave\ImgurClient\Mapper; |
4
|
|
|
|
5
|
|
|
use Mechpave\ImgurClient\Entity\ImageInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ImageMapper |
9
|
|
|
* @package Mechpave\ImgurClient\Mapper |
10
|
|
|
*/ |
11
|
|
|
class ImageMapper |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ObjectMapper |
15
|
|
|
*/ |
16
|
|
|
protected $objectMapper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* TokenMapper constructor. |
20
|
|
|
* @param ObjectMapper $objectMapper |
21
|
|
|
*/ |
22
|
4 |
|
public function __construct(ObjectMapper $objectMapper) |
23
|
|
|
{ |
24
|
4 |
|
$this->objectMapper = $objectMapper; |
25
|
4 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Builds Image object from raw data |
29
|
|
|
* |
30
|
|
|
* @param array $imageData |
31
|
|
|
* @return ImageInterface |
32
|
|
|
*/ |
33
|
2 |
|
public function buildImage(array $imageData) |
34
|
|
|
{ |
35
|
2 |
|
if (empty($imageData['id'])) { |
36
|
|
|
throw new \InvalidArgumentException(); |
37
|
|
|
} |
38
|
2 |
|
$uploadedAt = new \DateTime(); |
39
|
2 |
|
$uploadedAt->setTimestamp((int)$imageData['datetime']); |
40
|
|
|
|
41
|
2 |
|
$imageClass = $this->objectMapper->getImageClass(); |
42
|
|
|
|
43
|
|
|
/** @var ImageInterface $image */ |
44
|
2 |
|
$image = new $imageClass(); |
45
|
|
|
$image |
46
|
2 |
|
->setImageId($imageData['id']) |
47
|
2 |
|
->setTitle($imageData['title']) |
48
|
2 |
|
->setDescription($imageData['description']) |
49
|
2 |
|
->setUploadedAt($uploadedAt) |
50
|
2 |
|
->setType($imageData['type']) |
51
|
2 |
|
->setAnimated($imageData['animated']) |
52
|
2 |
|
->setWidth($imageData['width']) |
53
|
2 |
|
->setHeight($imageData['height']) |
54
|
2 |
|
->setSize($imageData['size']) |
55
|
2 |
|
->setViews($imageData['views']) |
56
|
2 |
|
->setBandwith(!empty($imageData['bandwith']) ? $imageData['bandwith'] : null) |
57
|
2 |
|
->setDeleteHash(!empty($imageData['deletehash']) ? $imageData['deletehash'] : null) |
58
|
2 |
|
->setName(!empty($imageData['name']) ? $imageData['name'] : null) |
59
|
2 |
|
->setSection(!empty($imageData['section']) ? $imageData['section'] : null) |
60
|
2 |
|
->setLink($imageData['link']) |
61
|
2 |
|
->setGifv(!empty($imageData['gifv']) ? $imageData['gifv'] : null) |
62
|
2 |
|
->setMp4(!empty($imageData['mp4']) ? $imageData['mp4'] : null) |
63
|
2 |
|
->setWebm(!empty($imageData['webm']) ? $imageData['webm'] : null) |
64
|
2 |
|
->setLooping(!empty($imageData['looping']) ? $imageData['looping'] : null) |
65
|
2 |
|
->setFavorite($imageData['favorite']) |
66
|
2 |
|
->setNsfw($imageData['nsfw']) |
67
|
2 |
|
->setVote($imageData['vote']); |
68
|
|
|
|
69
|
2 |
|
return $image; |
70
|
|
|
} |
71
|
|
|
} |