mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
remove 'u' from date normalizer
This commit is contained in:
parent
061a7dd537
commit
3f64db3b3a
@ -20,15 +20,16 @@
|
|||||||
namespace Chill\MainBundle\Serializer\Normalizer;
|
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||||
|
|
||||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||||
|
|
||||||
class DateNormalizer implements NormalizerInterface
|
class DateNormalizer implements NormalizerInterface, DenormalizerInterface
|
||||||
{
|
{
|
||||||
public function normalize($date, string $format = null, array $context = array())
|
public function normalize($date, string $format = null, array $context = array())
|
||||||
{
|
{
|
||||||
/** @var \DateTimeInterface $date */
|
/** @var \DateTimeInterface $date */
|
||||||
return [
|
return [
|
||||||
'datetime' => $date->format(\DateTimeInterface::ISO8601),
|
'datetime' => $date->format(\DateTimeInterface::ISO8601)
|
||||||
'u' => $date->getTimestamp()
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,4 +37,24 @@ class DateNormalizer implements NormalizerInterface
|
|||||||
{
|
{
|
||||||
return $data instanceof \DateTimeInterface;
|
return $data instanceof \DateTimeInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||||
|
{
|
||||||
|
switch ($type) {
|
||||||
|
case \DateTime::class:
|
||||||
|
return \DateTime::createFromFormat(\DateTimeInterface::ISO8601, $data['datetime']);
|
||||||
|
case \DateTimeInterface::class:
|
||||||
|
case \DateTimeImmutable::class:
|
||||||
|
default:
|
||||||
|
return \DateTimeImmutable::createFromFormat(\DateTimeInterface::ISO8601, $data['datetime']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportsDenormalization($data, string $type, string $format = null): bool
|
||||||
|
{
|
||||||
|
return $type === \DateTimeInterface::class ||
|
||||||
|
$type === \DateTime::class ||
|
||||||
|
$type === \DateTimeImmutable::class ||
|
||||||
|
(\is_array($data) && array_key_exists('datetime', $data));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user