1
0
mirror of synced 2024-11-24 05:26:03 +03:00

use PST timezone as default for all DateTime instances from AliExpress API (#6)

This commit is contained in:
Pavel 2020-12-31 13:59:03 +03:00 committed by GitHub
parent 1206a69bef
commit 8887145377
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,54 @@
<?php
/**
* PHP version 7.3
*
* @category TimezoneDeserializeSubscriber
* @package RetailCrm\Component\JMS\EventSubscriber
*/
namespace RetailCrm\Component\JMS\EventSubscriber;
use DateTime;
use DateTimeInterface;
use DateTimeZone;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
/**
* Class TimezoneDeserializeSubscriber
*
* @category TimezoneDeserializeSubscriber
* @package RetailCrm\Component\JMS\EventSubscriber
*/
class TimezoneDeserializeSubscriber implements EventSubscriberInterface
{
private const RFC3339_WITHOUT_TZ = 'Y-m-d\TH:i:s';
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [[
'event' => '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')
);
}
}
}
}

View File

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

View File

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