mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-05 06:14:59 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,115 +1,112 @@
|
||||
<?php
|
||||
/*
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use IntlDateFormatter;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use function is_array;
|
||||
|
||||
class DateNormalizer implements ContextAwareNormalizerInterface, DenormalizerInterface
|
||||
{
|
||||
private RequestStack $requestStack;
|
||||
private ParameterBagInterface $parameterBag;
|
||||
|
||||
private RequestStack $requestStack;
|
||||
|
||||
public function __construct(RequestStack $requestStack, ParameterBagInterface $parameterBag)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->parameterBag = $parameterBag;
|
||||
}
|
||||
|
||||
public function normalize($date, string $format = null, array $context = array())
|
||||
public function denormalize($data, string $type, ?string $format = null, array $context = [])
|
||||
{
|
||||
/** @var \DateTimeInterface $date */
|
||||
switch($format) {
|
||||
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 normalize($date, ?string $format = null, array $context = [])
|
||||
{
|
||||
/** @var DateTimeInterface $date */
|
||||
switch ($format) {
|
||||
case 'json':
|
||||
return [
|
||||
'datetime' => $date->format(\DateTimeInterface::ISO8601)
|
||||
'datetime' => $date->format(DateTimeInterface::ISO8601),
|
||||
];
|
||||
case 'docgen':
|
||||
|
||||
case 'docgen':
|
||||
if (null === $date) {
|
||||
return [
|
||||
'long' => '', 'short' => ''
|
||||
'long' => '', 'short' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$hasTime = $date->format('His') !== "000000";
|
||||
$hasTime = $date->format('His') !== '000000';
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
$locale = null !== $request ? $request->getLocale() : $this->parameterBag->get('kernel.default_locale');
|
||||
$formatterLong = \IntlDateFormatter::create(
|
||||
$formatterLong = IntlDateFormatter::create(
|
||||
$locale,
|
||||
\IntlDateFormatter::LONG,
|
||||
$hasTime ? \IntlDateFormatter::SHORT: \IntlDateFormatter::NONE
|
||||
IntlDateFormatter::LONG,
|
||||
$hasTime ? IntlDateFormatter::SHORT : IntlDateFormatter::NONE
|
||||
);
|
||||
$formatterShort = \IntlDateFormatter::create(
|
||||
$formatterShort = IntlDateFormatter::create(
|
||||
$locale,
|
||||
\IntlDateFormatter::SHORT,
|
||||
$hasTime ? \IntlDateFormatter::SHORT: \IntlDateFormatter::NONE
|
||||
IntlDateFormatter::SHORT,
|
||||
$hasTime ? IntlDateFormatter::SHORT : IntlDateFormatter::NONE
|
||||
);
|
||||
|
||||
return [
|
||||
'short' => $formatterShort->format($date),
|
||||
'long' => $formatterLong->format($date)
|
||||
'long' => $formatterLong->format($date),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, string $format = null, array $context = []): bool
|
||||
public function supportsDenormalization($data, string $type, ?string $format = null): bool
|
||||
{
|
||||
if ($format === 'json') {
|
||||
return $data instanceof \DateTimeInterface;
|
||||
} elseif ($format === 'docgen') {
|
||||
return $data instanceof \DateTimeInterface || (
|
||||
$data === null
|
||||
return DateTimeInterface::class === $type
|
||||
|| DateTime::class === $type
|
||||
|| DateTimeImmutable::class === $type
|
||||
|| (is_array($data) && array_key_exists('datetime', $data));
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
|
||||
{
|
||||
if ('json' === $format) {
|
||||
return $data instanceof DateTimeInterface;
|
||||
}
|
||||
|
||||
if ('docgen' === $format) {
|
||||
return $data instanceof DateTimeInterface || (
|
||||
null === $data
|
||||
&& \array_key_exists('docgen:expects', $context)
|
||||
&& (
|
||||
$context['docgen:expects'] === \DateTimeInterface::class
|
||||
|| $context['docgen:expects'] === \DateTime::class
|
||||
|| $context['docgen:expects'] === \DateTimeImmutable::class
|
||||
DateTimeInterface::class === $context['docgen:expects']
|
||||
|| DateTime::class === $context['docgen:expects']
|
||||
|| DateTimeImmutable::class === $context['docgen:expects']
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user