Add normalization and denormalization for RollingDate

Introduce methods to normalize and recreate RollingDate objects, ensuring consistent serialization and deserialization. Includes corresponding tests for both cases with and without a pivot date.
This commit is contained in:
Julien Fastré 2025-03-10 21:07:54 +01:00
parent 1751c65731
commit 2c91d2e10f
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 72 additions and 0 deletions

View File

@ -11,6 +11,8 @@ declare(strict_types=1);
namespace Chill\MainBundle\Service\RollingDate;
use Doctrine\Instantiator\Exception\UnexpectedValueException;
class RollingDate
{
final public const ALL_T = [
@ -61,6 +63,8 @@ class RollingDate
final public const T_YEAR_PREVIOUS_START = 'year_previous_start';
private const NORMALIZATION_FORMAT = 'd-m-Y-H:i:s.u T';
/**
* @param string|self::T_* $roll
* @param \DateTimeImmutable|null $fixedDate Only to insert if $roll equals @see{self::T_FIXED_DATE}
@ -85,4 +89,27 @@ class RollingDate
{
return $this->roll;
}
public function normalize(): array
{
return [
'roll' => $this->getRoll(),
'fixed_date' => $this->getFixedDate()?->format(self::NORMALIZATION_FORMAT),
'pivot_date' => $this->getPivotDate()?->format(self::NORMALIZATION_FORMAT),
'v' => 1,
];
}
public static function fromNormalized(array $data): self
{
if (1 === $data['v']) {
return new self(
$data['roll'],
null !== $data['fixed_date'] ? \DateTimeImmutable::createFromFormat(self::NORMALIZATION_FORMAT, (string) $data['fixed_date']) : null,
null !== $data['pivot_date'] ? \DateTimeImmutable::createFromFormat(self::NORMALIZATION_FORMAT, (string) $data['pivot_date']) : null,
);
}
throw new UnexpectedValueException('Format of the rolling date unknow, no version information');
}
}

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Tests\Services\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use PHPUnit\Framework\TestCase;
/**
* @internal
*
* @coversNothing
*/
class RollingDateTest extends TestCase
{
public function testNormalizationDenormalizationProcessWithoutPivotDate(): void
{
$date = new RollingDate(RollingDate::T_YEAR_PREVIOUS_START);
$actual = RollingDate::fromNormalized($date->normalize());
self::assertEquals(RollingDate::T_YEAR_PREVIOUS_START, $actual->getRoll());
self::assertNull($actual->getFixedDate());
self::assertEquals($date->getPivotDate()->getTimestamp(), $actual->getPivotDate()->getTimestamp());
}
public function testNormalizationDenormalizationProcessWithPivotDate(): void
{
$date = new RollingDate(RollingDate::T_FIXED_DATE, $pivot = new \DateTimeImmutable('now'));
$actual = RollingDate::fromNormalized($date->normalize());
self::assertEquals(RollingDate::T_FIXED_DATE, $actual->getRoll());
self::assertEquals($pivot, $actual->getFixedDate());
self::assertEquals($date->getPivotDate()->getTimestamp(), $actual->getPivotDate()->getTimestamp());
}
}