mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 05:44:24 +00:00
61 lines
2.3 KiB
PHP
61 lines
2.3 KiB
PHP
<?php
|
|
/*
|
|
*
|
|
* Copyright (C) 2014-2021, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\Serializer\Normalizer;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
|
|
|
class DateNormalizer implements NormalizerInterface, DenormalizerInterface
|
|
{
|
|
public function normalize($date, string $format = null, array $context = array())
|
|
{
|
|
/** @var \DateTimeInterface $date */
|
|
return [
|
|
'datetime' => $date->format(\DateTimeInterface::ISO8601)
|
|
];
|
|
}
|
|
|
|
public function supportsNormalization($data, string $format = null): bool
|
|
{
|
|
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));
|
|
}
|
|
}
|