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()