From 2c91d2e10feda258d8b24f7acc367169998e6a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 10 Mar 2025 21:07:54 +0100 Subject: [PATCH] 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. --- .../Service/RollingDate/RollingDate.php | 27 +++++++++++ .../Services/RollingDate/RollingDateTest.php | 45 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/Tests/Services/RollingDate/RollingDateTest.php diff --git a/src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php b/src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php index 1f7f01dbc..38b75f3a6 100644 --- a/src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php +++ b/src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php @@ -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'); + } } diff --git a/src/Bundle/ChillMainBundle/Tests/Services/RollingDate/RollingDateTest.php b/src/Bundle/ChillMainBundle/Tests/Services/RollingDate/RollingDateTest.php new file mode 100644 index 000000000..3c7d01360 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Services/RollingDate/RollingDateTest.php @@ -0,0 +1,45 @@ +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()); + } +}