1
|
|
|
<?php |
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
|
|
|
* @author Denis Golubovskiy <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class JmsPostSerializeListener extends JmsSerializeListenerAbstract |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array $postSerializedObjects Post Serialized objects |
28
|
|
|
*/ |
29
|
|
|
private $postSerializedObjects = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* On post serialize |
33
|
|
|
* |
34
|
|
|
* @param ObjectEvent $event Event |
35
|
|
|
*/ |
36
|
|
|
public function onPostSerialize(ObjectEvent $event) |
37
|
|
|
{ |
38
|
|
|
$object = $this->getObject($event); |
39
|
|
|
$objectUid = spl_object_hash($object); |
40
|
|
|
|
41
|
|
|
if (in_array($objectUid, $this->postSerializedObjects, true)) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$classAnnotation = $this->annotationReader->getClassAnnotation( |
46
|
|
|
new \ReflectionClass(ClassUtils::getClass($object)), |
47
|
|
|
LiipImagineSerializableClass::class |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
if ($classAnnotation instanceof LiipImagineSerializableClass) { |
51
|
|
|
$reflectionClass = ClassUtils::newReflectionClass(get_class($object)); |
52
|
|
|
|
53
|
|
|
foreach ($reflectionClass->getProperties() as $property) { |
54
|
|
|
$liipAnnotation = $this->annotationReader->getPropertyAnnotation($property, LiipImagineSerializableField::class); |
55
|
|
|
$property->setAccessible(true); |
56
|
|
|
|
57
|
|
|
if ($liipAnnotation instanceof LiipImagineSerializableField && ($value = $property->getValue($object)) && ($virtualField = $liipAnnotation->getVirtualField())) { |
58
|
|
|
if (array_key_exists('vichUploaderSerialize', $this->config) && $this->config['vichUploaderSerialize'] && $liipAnnotation->getVichUploaderField()) { |
59
|
|
|
$valueArray = explode('/', $value); |
60
|
|
|
$value = end($valueArray); |
61
|
|
|
$property->setValue($object, $value); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @var GenericSerializationVisitor $visitor */ |
65
|
|
|
$visitor = $event->getVisitor(); |
66
|
|
|
$visitor->addData($virtualField, $this->serializeValue($liipAnnotation, $object, $value)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->postSerializedObjects[$objectUid] = $objectUid; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|