|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\EventListener; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\Event; |
|
6
|
|
|
use AppBundle\Entity\Image; |
|
7
|
|
|
use AppBundle\Exception\UnsupportedEntityException; |
|
8
|
|
|
use JMS\Serializer\GraphNavigator; |
|
9
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
|
10
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Vehsamrak |
|
15
|
|
|
*/ |
|
16
|
|
|
class SerializerListener implements SubscribingHandlerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** @var Router */ |
|
20
|
|
|
private $router; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(Router $router) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->router = $router; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** {@inheritDoc} */ |
|
28
|
|
|
public static function getSubscribingMethods(): array |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
31
|
|
|
[ |
|
32
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
|
33
|
|
|
'format' => 'json', |
|
34
|
|
|
'type' => Event::class, |
|
35
|
|
|
'method' => 'serialize', |
|
36
|
|
|
], |
|
37
|
|
|
]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param object $entity |
|
42
|
|
|
* @throws UnsupportedEntityException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function serialize( |
|
45
|
|
|
JsonSerializationVisitor $visitor, |
|
|
|
|
|
|
46
|
|
|
$entity |
|
47
|
|
|
): array |
|
48
|
|
|
{ |
|
49
|
|
|
if ($entity instanceof Event) { |
|
50
|
|
|
$serializedData = [ |
|
51
|
|
|
'id' => $entity->getId(), |
|
52
|
|
|
'date' => $entity->getDate(), |
|
53
|
|
|
'name' => $entity->getName(), |
|
54
|
|
|
'description' => $entity->getDescription(), |
|
55
|
|
|
'creator' => $entity->getCreatorLogin(), |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
$images = $this->getImages($entity); |
|
59
|
|
|
|
|
60
|
|
|
if ($images) { |
|
61
|
|
|
$serializedData['images'] = $images; |
|
62
|
|
|
} |
|
63
|
|
|
} else { |
|
64
|
|
|
throw new UnsupportedEntityException(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $serializedData; |
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string[]|null |
|
73
|
|
|
*/ |
|
74
|
|
|
private function getImages(Event $event) |
|
75
|
|
|
{ |
|
76
|
|
|
$images = $event->getImages(); |
|
77
|
|
|
|
|
78
|
|
|
if ($images) { |
|
79
|
|
|
$eventId = $event->getId(); |
|
80
|
|
|
|
|
81
|
|
|
return array_map( |
|
82
|
|
|
function (Image $image) use ($eventId) { |
|
83
|
|
|
return $this->generateImageUrl($eventId, $image->getName()); |
|
84
|
|
|
}, |
|
85
|
|
|
$images->toArray() |
|
86
|
|
|
); |
|
87
|
|
|
} else { |
|
88
|
|
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function generateImageUrl(string $eventId, string $imageName) |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->router->generate( |
|
95
|
|
|
'event_image_view', |
|
96
|
|
|
[ |
|
97
|
|
|
'eventId' => $eventId, |
|
98
|
|
|
'imageName' => $imageName, |
|
99
|
|
|
], |
|
100
|
|
|
Router::ABSOLUTE_URL |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.