1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace CCT\Component\Rest\EventListener; |
6
|
|
|
|
7
|
|
|
use CCT\Component\Rest\Collection\CollectionInterface; |
8
|
|
|
use CCT\Component\Rest\Model\Structure\ExtraFieldsInterface; |
9
|
|
|
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; |
10
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
11
|
|
|
use JMS\Serializer\EventDispatcher\PreDeserializeEvent; |
12
|
|
|
|
13
|
|
|
class DeserializeExtraFieldsSubscriber implements EventSubscriberInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $data = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected static $config; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* It sets the default configuration for the DeserializeExtraFields. |
27
|
|
|
* The array must be sent in following structure: |
28
|
|
|
* $config = [ |
29
|
|
|
* 'FQDN' => [list of fields to ignore], |
30
|
|
|
* ]; |
31
|
|
|
* |
32
|
|
|
* Example: |
33
|
|
|
* $config = [ |
34
|
|
|
* 'Kong\\Model\\Kong' => ["tagline"], |
35
|
|
|
* ]; |
36
|
|
|
* |
37
|
|
|
* @param array $config |
38
|
|
|
*/ |
39
|
|
|
public function __construct(array $config) |
40
|
|
|
{ |
41
|
|
|
self::$config = $config; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public static function getSubscribedEvents() |
48
|
|
|
{ |
49
|
|
|
$subscribers = []; |
50
|
|
|
|
51
|
|
|
foreach (self::$config as $class => $ignoreFields) { |
52
|
|
|
if (!class_exists($class)) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$subscribers[] = self::createPreDeserializeSubscriber($class); |
57
|
|
|
$subscribers[] = self::createPostDeserializeSubscriber($class); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $subscribers; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create pre deserialize subscriber |
65
|
|
|
* |
66
|
|
|
* @param $class |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
protected static function createPreDeserializeSubscriber($class) |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
'event' => 'serializer.pre_deserialize', |
74
|
|
|
'method' => 'onPreDeserialize', |
75
|
|
|
'class' => $class, |
76
|
|
|
'format' => 'json', |
77
|
|
|
'priority' => 0, |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create post deserialize subscriber |
83
|
|
|
* |
84
|
|
|
* @param $class |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
protected static function createPostDeserializeSubscriber($class) |
89
|
|
|
{ |
90
|
|
|
return [ |
91
|
|
|
'event' => 'serializer.post_deserialize', |
92
|
|
|
'method' => 'onPostDeserialize', |
93
|
|
|
'class' => $class, |
94
|
|
|
'format' => 'json', |
95
|
|
|
'priority' => 0, |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param PreDeserializeEvent $event |
101
|
|
|
*/ |
102
|
|
|
public function onPreDeserialize(PreDeserializeEvent $event) |
103
|
|
|
{ |
104
|
|
|
$this->data = []; |
105
|
|
|
|
106
|
|
|
if (!is_array($event->getData())) { |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->data = $event->getData(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param ObjectEvent $event |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function onPostDeserialize(ObjectEvent $event) |
119
|
|
|
{ |
120
|
|
|
$object = $event->getObject(); |
121
|
|
|
|
122
|
|
|
if (!is_object($object)) { |
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->populateFields($object); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param object $object |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
protected function populateFields($object) |
135
|
|
|
{ |
136
|
|
|
|
137
|
|
|
if (!$this->isObjectAllowed($object)) { |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$ignoreFields = $this->getIgnoreFields($object); |
142
|
|
|
|
143
|
|
|
foreach ($this->data as $property => $value) { |
144
|
|
|
if (in_array($property, $ignoreFields)) { |
145
|
|
|
continue; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$object->getExtraFields()->set($property, $value); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Is object allowed |
154
|
|
|
* |
155
|
|
|
* @param object $object |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
protected function isObjectAllowed($object) |
160
|
|
|
{ |
161
|
|
|
$config = static::$config; |
162
|
|
|
$objectClass = get_class($object); |
163
|
|
|
|
164
|
|
|
return ($object instanceof ExtraFieldsInterface && |
165
|
|
|
$object->getExtraFields() instanceof CollectionInterface && |
166
|
|
|
isset($config[$objectClass])); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get ignore fields for object |
171
|
|
|
* |
172
|
|
|
* @param object $object |
173
|
|
|
* |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
|
|
protected function getIgnoreFields($object) |
177
|
|
|
{ |
178
|
|
|
$config = static::$config; |
179
|
|
|
$objectClass = get_class($object); |
180
|
|
|
|
181
|
|
|
return $config[$objectClass]; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|