1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Bukashk0zzzLiipImagineSerializationBundle |
4
|
|
|
* |
5
|
|
|
* (c) Denis Golubovskiy <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Bukashk0zzz\LiipImagineSerializationBundle\EventListener; |
12
|
|
|
|
13
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Annotation\LiipImagineSerializableClass; |
14
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Annotation\LiipImagineSerializableField; |
15
|
|
|
use Doctrine\Common\Util\ClassUtils; |
16
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
17
|
|
|
use JMS\Serializer\GenericSerializationVisitor; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* JmsPostSerializeListener |
21
|
|
|
*/ |
22
|
|
|
class JmsPostSerializeListener extends JmsSerializeListenerAbstract |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* On post serialize |
26
|
|
|
* |
27
|
|
|
* @param ObjectEvent $event Event |
28
|
|
|
*/ |
29
|
|
|
public function onPostSerialize(ObjectEvent $event): void |
30
|
|
|
{ |
31
|
|
|
$object = $this->getObject($event); |
32
|
|
|
|
33
|
|
|
$classAnnotation = $this->annotationReader->getClassAnnotation( |
34
|
|
|
new \ReflectionClass(ClassUtils::getClass($object)), |
35
|
|
|
LiipImagineSerializableClass::class |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
if ($classAnnotation instanceof LiipImagineSerializableClass) { |
39
|
|
|
$reflectionClass = ClassUtils::newReflectionClass(\get_class($object)); |
40
|
|
|
|
41
|
|
|
foreach ($reflectionClass->getProperties() as $property) { |
42
|
|
|
$liipAnnotation = $this->annotationReader->getPropertyAnnotation($property, LiipImagineSerializableField::class); |
43
|
|
|
$property->setAccessible(true); |
44
|
|
|
|
45
|
|
|
if ($liipAnnotation instanceof LiipImagineSerializableField) { |
46
|
|
|
$value = $property->getValue($object); |
47
|
|
|
$virtualField = $liipAnnotation->getVirtualField(); |
48
|
|
|
|
49
|
|
|
if ($value && !\is_array($value) && $virtualField) { |
50
|
|
|
if (\array_key_exists('vichUploaderSerialize', $this->config) && $this->config['vichUploaderSerialize'] && $liipAnnotation->getVichUploaderField()) { |
51
|
|
|
$valueArray = \explode('/', $value); |
52
|
|
|
$value = \end($valueArray); |
53
|
|
|
$property->setValue($object, $value); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @var GenericSerializationVisitor $visitor */ |
57
|
|
|
$visitor = $event->getVisitor(); |
58
|
|
|
$visitor->setData($virtualField, $this->serializeValue($liipAnnotation, $object, $value)); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|