|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OroTutorial\Bundle\PrestashopBundle\ImportExport\Serializer; |
| 4 | + |
| 5 | +use Symfony\Component\Serializer\Exception\RuntimeException; |
| 6 | + |
| 7 | +use Oro\Bundle\ImportExportBundle\Serializer\Normalizer\DateTimeNormalizer as BaseNormalizer; |
| 8 | +use Oro\Bundle\ImportExportBundle\Serializer\Normalizer\DenormalizerInterface; |
| 9 | +use Oro\Bundle\ImportExportBundle\Serializer\Normalizer\NormalizerInterface; |
| 10 | +use Oro\Bundle\ImportExportBundle\Serializer\Serializer; |
| 11 | + |
| 12 | +class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface |
| 13 | +{ |
| 14 | + public function __construct() |
| 15 | + { |
| 16 | + $this->prestashopNormalizer = new BaseNormalizer('Y-m-d H:i:s', 'Y-m-d', 'H:i:s', 'UTC'); |
| 17 | + $this->isoNormalizer = new BaseNormalizer(\DateTime::ISO8601, 'Y-m-d', 'H:i:s', 'UTC'); |
| 18 | + $this->dateNormalizer = new BaseNormalizer('Y-m-d', 'Y-m-d', 'H:i:s', 'UTC'); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * {@inheritdoc} |
| 23 | + */ |
| 24 | + public function denormalize($data, $class, $format = null, array $context = array()) |
| 25 | + { |
| 26 | + try { |
| 27 | + return $this->prestashopNormalizer->denormalize($data, $class, $format, $context); |
| 28 | + } catch (RuntimeException $e) { |
| 29 | + return $this->dateNormalizer->denormalize($data, $class, $format, $context); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * {@inheritdoc} |
| 35 | + */ |
| 36 | + public function normalize($object, $format = null, array $context = array()) |
| 37 | + { |
| 38 | + return $this->prestashopNormalizer->normalize($object, $format, $context); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + public function supportsDenormalization($data, $type, $format = null, array $context = array()) |
| 45 | + { |
| 46 | + return $this->prestashopNormalizer->supportsDenormalization($data, $type, $format, $context) |
| 47 | + && !empty($context[Serializer::PROCESSOR_ALIAS_KEY]) |
| 48 | + && strpos($context[Serializer::PROCESSOR_ALIAS_KEY], 'presta_shop') !== false; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritdoc} |
| 53 | + */ |
| 54 | + public function supportsNormalization($data, $format = null, array $context = array()) |
| 55 | + { |
| 56 | + return $this->prestashopNormalizer->supportsNormalization($data, $format, $context) |
| 57 | + && !empty($context[Serializer::PROCESSOR_ALIAS_KEY]) |
| 58 | + && strpos($context[Serializer::PROCESSOR_ALIAS_KEY], 'presta_shop') !== false; |
| 59 | + } |
| 60 | +} |
0 commit comments