1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Service\Extractor; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Event; |
6
|
|
|
use AppBundle\Entity\Image; |
7
|
|
|
use AppBundle\Entity\Link; |
8
|
|
|
use AppBundle\Exception\UnsupportedEntityException; |
9
|
|
|
use AppBundle\Service\Extractor\Infrastructure\AbstractExtractor; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Vehsamrak |
13
|
|
|
*/ |
14
|
|
|
class EventExtractor extends AbstractExtractor |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Event $entity |
19
|
|
|
* @throws UnsupportedEntityException |
20
|
|
|
*/ |
21
|
9 |
|
public function extract($entity): array |
22
|
|
|
{ |
23
|
9 |
|
if ($entity instanceof Event) { |
24
|
|
|
$serializedData = [ |
25
|
9 |
|
'id' => $entity->getId(), |
26
|
9 |
|
'date' => $entity->getDate(), |
27
|
9 |
|
'name' => $entity->getName(), |
28
|
9 |
|
'description' => $entity->getDescription(), |
29
|
9 |
|
'place' => $entity->getPlace(), |
30
|
9 |
|
'creator' => $entity->getCreatorLogin(), |
31
|
9 |
|
'images' => $this->getImages($entity), |
32
|
|
|
'links' => $this->getLinks($entity), |
33
|
|
|
]; |
34
|
|
|
} else { |
35
|
|
|
throw new UnsupportedEntityException(); |
36
|
|
|
} |
37
|
9 |
|
|
38
|
|
|
return $serializedData; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string[] |
43
|
9 |
|
*/ |
44
|
|
|
private function getImages(Event $event): array |
45
|
9 |
|
{ |
46
|
9 |
|
$images = $event->getImages(); |
47
|
|
|
$eventId = $event->getId(); |
48
|
9 |
|
|
49
|
|
|
return array_map( |
50
|
7 |
|
function (Image $image) use ($eventId) { |
51
|
9 |
|
return $this->generateImageUrl($eventId, $image->getName()); |
52
|
9 |
|
}, |
53
|
|
|
$images->toArray() |
54
|
|
|
); |
55
|
|
|
} |
56
|
9 |
|
|
57
|
|
|
private function getLinks(Event $event): array |
58
|
9 |
|
{ |
59
|
9 |
|
return array_map( |
60
|
|
|
function (Link $link) { |
61
|
7 |
|
return [ |
62
|
7 |
|
'id' => $link->getId(), |
63
|
7 |
|
'url' => $link->getUrl(), |
64
|
|
|
'description' => $link->getDescription(), |
65
|
9 |
|
]; |
66
|
9 |
|
}, |
67
|
|
|
$event->getLinks()->toArray() |
68
|
|
|
); |
69
|
|
|
} |
70
|
7 |
|
|
71
|
|
|
private function generateImageUrl(string $eventId, string $imageName): string |
72
|
7 |
|
{ |
73
|
7 |
|
return $this->generateUrl( |
74
|
|
|
'event_image_view', |
75
|
7 |
|
[ |
76
|
7 |
|
'id' => $eventId, |
77
|
|
|
'imageName' => $imageName, |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|