1 | <?php |
||
2 | |||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
3 | namespace DMT\Insolvency\Soap\Serializer; |
||
4 | |||
5 | use JMS\Serializer\EventDispatcher\EventSubscriberInterface; |
||
6 | use JMS\Serializer\EventDispatcher\PreDeserializeEvent; |
||
7 | use JMS\Serializer\Exception\RuntimeException; |
||
8 | |||
9 | /** |
||
10 | * Class SoapDateTimeSanitizerEventSubscriber |
||
11 | */ |
||
0 ignored issues
–
show
|
|||
12 | class SoapDateTimeSanitizerEventSubscriber implements EventSubscriberInterface |
||
13 | { |
||
14 | /** |
||
15 | * {@inheritDoc} |
||
16 | */ |
||
0 ignored issues
–
show
|
|||
17 | 12 | public static function getSubscribedEvents() |
|
18 | { |
||
19 | return [ |
||
20 | [ |
||
21 | 12 | 'event' => 'serializer.pre_deserialize', |
|
22 | 'method' => 'sanitizeDateTime', |
||
23 | 'format' => 'soap', |
||
24 | ] |
||
25 | ]; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Fix the datetime format, before it is deserialized. |
||
30 | * |
||
31 | * @param PreDeserializeEvent $event |
||
0 ignored issues
–
show
|
|||
32 | * @throws RuntimeException |
||
0 ignored issues
–
show
|
|||
33 | */ |
||
0 ignored issues
–
show
|
|||
34 | 13 | public function sanitizeDateTime(PreDeserializeEvent $event) |
|
35 | { |
||
36 | 13 | if ($event->getType()['name'] !== 'DateTime' || empty($event->getData())) { |
|
37 | 11 | return; |
|
38 | } |
||
39 | |||
40 | try { |
||
41 | 13 | $dateTime = new \DateTime($event->getData()[0]); |
|
42 | |||
43 | 13 | $element = simplexml_load_string( |
|
44 | 13 | sprintf('<x attr="%s"/>', $dateTime->format($event->getType()['params'][0])) |
|
45 | ); |
||
46 | |||
47 | 13 | $event->setData($element->attributes()['attr']); |
|
48 | } catch (\Exception $exception) { |
||
49 | throw new RuntimeException(sprintf('"%s" is not a datetime', $event->getData()[0])); |
||
50 | } |
||
51 | } |
||
52 | } |