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:
Julien Fastré 2024-06-11 16:33:54 +02:00
parent 350d991a85
commit 77c53972c8
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB

View File

@ -18,6 +18,8 @@ use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserJob;
use Chill\MainBundle\Templating\Entity\UserRender;
use DateTime;
use DateTimeImmutable;
use libphonenumber\PhoneNumber;
use Symfony\Component\Clock\ClockInterface;
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 = [])
{
/** @var array{"chill:user:at_date"?: DateTimeImmutable|DateTime} $context */
/** @var User $object */
$userJobContext = array_merge(
$context,
@ -78,6 +84,9 @@ class UserNormalizer implements ContextAwareNormalizerInterface, NormalizerAware
}
$at = $context[self::AT_DATE] ?? $this->clock->now();
if ($at instanceof DateTime) {
$at = DateTimeImmutable::createFromMutable($at);
}
$data = [
'type' => 'user',