From 8887145377f9d911931668a37b3a189cad4e69b7 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Thu, 31 Dec 2020 13:59:03 +0300 Subject: [PATCH] use PST timezone as default for all DateTime instances from AliExpress API (#6) --- .../TimezoneDeserializeSubscriber.php | 54 +++++++++++++++++++ src/Factory/SerializerFactory.php | 7 +++ .../RetailCrm/Tests/TopClient/ClientTest.php | 1 + 3 files changed, 62 insertions(+) create mode 100644 src/Component/JMS/EventSubscriber/TimezoneDeserializeSubscriber.php diff --git a/src/Component/JMS/EventSubscriber/TimezoneDeserializeSubscriber.php b/src/Component/JMS/EventSubscriber/TimezoneDeserializeSubscriber.php new file mode 100644 index 0000000..1bbf5b7 --- /dev/null +++ b/src/Component/JMS/EventSubscriber/TimezoneDeserializeSubscriber.php @@ -0,0 +1,54 @@ + 'serializer.post_deserialize', + 'method' => 'onPostDeserialize', + ]]; + } + + public function onPostDeserialize(ObjectEvent $event): void + { + $object = $event->getObject(); + + foreach (get_object_vars($object) as $property => $value) { + if ($value instanceof DateTimeInterface + && $value->getTimezone()->getName() === 'UTC' + ) { + $object->$property = DateTime::createFromFormat( + self::RFC3339_WITHOUT_TZ, + $value->format(self::RFC3339_WITHOUT_TZ), + new DateTimeZone('PST') + ); + } + } + } +} diff --git a/src/Factory/SerializerFactory.php b/src/Factory/SerializerFactory.php index 80a5aa2..c8622af 100644 --- a/src/Factory/SerializerFactory.php +++ b/src/Factory/SerializerFactory.php @@ -8,6 +8,9 @@ */ namespace RetailCrm\Factory; +use JMS\Serializer\Annotation\PostDeserialize; +use JMS\Serializer\EventDispatcher\EventDispatcher; +use JMS\Serializer\EventDispatcher\ObjectEvent; use JMS\Serializer\GraphNavigatorInterface; use JMS\Serializer\Handler\HandlerRegistry; use JMS\Serializer\Serializer; @@ -16,6 +19,7 @@ use JMS\Serializer\SerializerInterface; use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory; use Psr\Container\ContainerInterface; use RetailCrm\Component\Constants; +use RetailCrm\Component\JMS\EventSubscriber\TimezoneDeserializeSubscriber; use RetailCrm\Component\JMS\Factory\JsonDeserializationVisitorFactory; use RetailCrm\Interfaces\FactoryInterface; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; @@ -100,6 +104,9 @@ class SerializerFactory implements FactoryInterface $returnNull ); })->addDefaultHandlers() + ->configureListeners(function (EventDispatcher $dispatcher) { + $dispatcher->addSubscriber(new TimezoneDeserializeSubscriber()); + }) ->setSerializationVisitor('json', new JsonSerializationVisitorFactory()) ->setDeserializationVisitor('json', new JsonDeserializationVisitorFactory()) ->setSerializationContextFactory(new SerializationContextFactory()) diff --git a/tests/RetailCrm/Tests/TopClient/ClientTest.php b/tests/RetailCrm/Tests/TopClient/ClientTest.php index a2a1508..476da4c 100644 --- a/tests/RetailCrm/Tests/TopClient/ClientTest.php +++ b/tests/RetailCrm/Tests/TopClient/ClientTest.php @@ -619,6 +619,7 @@ EOF; OfflinePickupTypes::RU_OFFLINE_SELF_PICK_UP_EXPRESSION, $result->targetList->orderDto[0]->offlinePickupType ); + self::assertEquals('PST', $result->targetList->orderDto[0]->gmtCreate->getTimezone()->getName()); } public function testAliexpressSolutionOrderReceiptInfoGet()