mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-05 06:14:59 +00:00
Add date normalization and denormalization functionality
Introduce methods to normalize and denormalize DateTime objects using specific formats. Also, implement corresponding unit tests to ensure correct handling of single and multiple Doctrine entities, as well as date normalization.
This commit is contained in:
@@ -39,4 +39,30 @@ trait ExportDataNormalizerTrait
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
public function normalizeDate(\DateTimeImmutable|\DateTime $date): string
|
||||
{
|
||||
return sprintf(
|
||||
'%s,%s',
|
||||
$date instanceof \DateTimeImmutable ? 'imm1' : 'mut1',
|
||||
$date->format('d-m-Y-H:i:s.u e'),
|
||||
);
|
||||
}
|
||||
|
||||
public function denormalizeDate(string $date): \DateTimeImmutable|\DateTime
|
||||
{
|
||||
$format = 'd-m-Y-H:i:s.u e';
|
||||
|
||||
$denormalized = match (substr($date, 0, 4)) {
|
||||
'imm1' => \DateTimeImmutable::createFromFormat($format, substr($date, 5)),
|
||||
'mut1' => \DateTime::createFromFormat($format, substr($date, 5)),
|
||||
default => throw new \UnexpectedValueException(sprintf('Unexpected format for the kind selector: %s', substr($date, 0, 4))),
|
||||
};
|
||||
|
||||
if (false === $denormalized) {
|
||||
throw new \UnexpectedValueException(sprintf('Unexpected date format: %s', substr($date, 5)));
|
||||
}
|
||||
|
||||
return $denormalized;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user