use PST timezone as default for all DateTime instances from AliExpress API (#6)
This commit is contained in:
parent
1206a69bef
commit
8887145377
@ -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')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,9 @@
|
|||||||
*/
|
*/
|
||||||
namespace RetailCrm\Factory;
|
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\GraphNavigatorInterface;
|
||||||
use JMS\Serializer\Handler\HandlerRegistry;
|
use JMS\Serializer\Handler\HandlerRegistry;
|
||||||
use JMS\Serializer\Serializer;
|
use JMS\Serializer\Serializer;
|
||||||
@ -16,6 +19,7 @@ use JMS\Serializer\SerializerInterface;
|
|||||||
use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory;
|
use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory;
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
use RetailCrm\Component\Constants;
|
use RetailCrm\Component\Constants;
|
||||||
|
use RetailCrm\Component\JMS\EventSubscriber\TimezoneDeserializeSubscriber;
|
||||||
use RetailCrm\Component\JMS\Factory\JsonDeserializationVisitorFactory;
|
use RetailCrm\Component\JMS\Factory\JsonDeserializationVisitorFactory;
|
||||||
use RetailCrm\Interfaces\FactoryInterface;
|
use RetailCrm\Interfaces\FactoryInterface;
|
||||||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
||||||
@ -100,6 +104,9 @@ class SerializerFactory implements FactoryInterface
|
|||||||
$returnNull
|
$returnNull
|
||||||
);
|
);
|
||||||
})->addDefaultHandlers()
|
})->addDefaultHandlers()
|
||||||
|
->configureListeners(function (EventDispatcher $dispatcher) {
|
||||||
|
$dispatcher->addSubscriber(new TimezoneDeserializeSubscriber());
|
||||||
|
})
|
||||||
->setSerializationVisitor('json', new JsonSerializationVisitorFactory())
|
->setSerializationVisitor('json', new JsonSerializationVisitorFactory())
|
||||||
->setDeserializationVisitor('json', new JsonDeserializationVisitorFactory())
|
->setDeserializationVisitor('json', new JsonDeserializationVisitorFactory())
|
||||||
->setSerializationContextFactory(new SerializationContextFactory())
|
->setSerializationContextFactory(new SerializationContextFactory())
|
||||||
|
@ -619,6 +619,7 @@ EOF;
|
|||||||
OfflinePickupTypes::RU_OFFLINE_SELF_PICK_UP_EXPRESSION,
|
OfflinePickupTypes::RU_OFFLINE_SELF_PICK_UP_EXPRESSION,
|
||||||
$result->targetList->orderDto[0]->offlinePickupType
|
$result->targetList->orderDto[0]->offlinePickupType
|
||||||
);
|
);
|
||||||
|
self::assertEquals('PST', $result->targetList->orderDto[0]->gmtCreate->getTimezone()->getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAliexpressSolutionOrderReceiptInfoGet()
|
public function testAliexpressSolutionOrderReceiptInfoGet()
|
||||||
|
Loading…
Reference in New Issue
Block a user