mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 21:34:25 +00:00
Add DateTimeImmutable support in UserNormalizer
This commit introduces the use of DateTimeImmutable in the UserNormalizer class to ensure immutability of datetime objects. A check is also added to convert DateTime instances to DateTimeImmutable when normalizing data. This enhances the safety and predictability of datetimes used in the application.
This commit is contained in:
parent
350d991a85
commit
77c53972c8
@ -18,6 +18,8 @@ use Chill\MainBundle\Entity\Scope;
|
|||||||
use Chill\MainBundle\Entity\User;
|
use Chill\MainBundle\Entity\User;
|
||||||
use Chill\MainBundle\Entity\UserJob;
|
use Chill\MainBundle\Entity\UserJob;
|
||||||
use Chill\MainBundle\Templating\Entity\UserRender;
|
use Chill\MainBundle\Templating\Entity\UserRender;
|
||||||
|
use DateTime;
|
||||||
|
use DateTimeImmutable;
|
||||||
use libphonenumber\PhoneNumber;
|
use libphonenumber\PhoneNumber;
|
||||||
use Symfony\Component\Clock\ClockInterface;
|
use Symfony\Component\Clock\ClockInterface;
|
||||||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
|
||||||
@ -45,8 +47,12 @@ class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAware
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \Symfony\Component\Serializer\Exception\ExceptionInterface
|
||||||
|
*/
|
||||||
public function normalize($object, $format = null, array $context = [])
|
public function normalize($object, $format = null, array $context = [])
|
||||||
{
|
{
|
||||||
|
/** @var array{"chill:user:at_date"?: DateTimeImmutable|DateTime} $context */
|
||||||
/** @var User $object */
|
/** @var User $object */
|
||||||
$userJobContext = array_merge(
|
$userJobContext = array_merge(
|
||||||
$context,
|
$context,
|
||||||
@ -78,6 +84,9 @@ class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAware
|
|||||||
}
|
}
|
||||||
|
|
||||||
$at = $context[self::AT_DATE] ?? $this->clock->now();
|
$at = $context[self::AT_DATE] ?? $this->clock->now();
|
||||||
|
if ($at instanceof DateTime) {
|
||||||
|
$at = DateTimeImmutable::createFromMutable($at);
|
||||||
|
}
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
'type' => 'user',
|
'type' => 'user',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user