From fe78426a5dbedbec449da40abcedcbf3e78fd6b3 Mon Sep 17 00:00:00 2001 From: Sebastian Krupinski Date: Fri, 3 Jul 2026 21:33:28 -0400 Subject: [PATCH] feat: add deserilazation Signed-off-by: Sebastian Krupinski --- shared/lib/Resource/Range/IRange.php | 4 +++- shared/lib/Resource/Range/Range.php | 4 ++++ shared/lib/Resource/Range/RangeDate.php | 17 ++++++++++++++++- shared/lib/Resource/Range/RangeTally.php | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/shared/lib/Resource/Range/IRange.php b/shared/lib/Resource/Range/IRange.php index 4d3596a..745fa5b 100644 --- a/shared/lib/Resource/Range/IRange.php +++ b/shared/lib/Resource/Range/IRange.php @@ -9,7 +9,9 @@ declare(strict_types=1); namespace KTXF\Resource\Range; -interface IRange { +use KTXF\Json\JsonDeserializable; + +interface IRange extends JsonDeserializable { /** * Gets the type of this range diff --git a/shared/lib/Resource/Range/Range.php b/shared/lib/Resource/Range/Range.php index 49e38ac..4b09c07 100644 --- a/shared/lib/Resource/Range/Range.php +++ b/shared/lib/Resource/Range/Range.php @@ -11,6 +11,10 @@ namespace KTXF\Resource\Range; class Range implements IRange { + public function jsonDeserialize(array|string $data): static { + return $this; + } + /** * Returns the type of the range * diff --git a/shared/lib/Resource/Range/RangeDate.php b/shared/lib/Resource/Range/RangeDate.php index 069049b..03d27c1 100644 --- a/shared/lib/Resource/Range/RangeDate.php +++ b/shared/lib/Resource/Range/RangeDate.php @@ -9,7 +9,7 @@ declare(strict_types=1); namespace KTXF\Resource\Range; -use DateTime; +use DateTimeImmutable; use DateTimeInterface; class RangeDate extends Range implements IRangeDate { @@ -17,6 +17,21 @@ class RangeDate extends Range implements IRangeDate { protected DateTimeInterface $start; protected DateTimeInterface $end; + public function jsonDeserialize(array|string $data): static { + if (is_string($data)) { + $data = json_decode($data, true, flags: JSON_THROW_ON_ERROR); + } + + if (isset($data['start'])) { + $this->setStart(new DateTimeImmutable($data['start'])); + } + if (isset($data['end'])) { + $this->setEnd(new DateTimeImmutable($data['end'])); + } + + return $this; + } + /** * Returns the type of the range * diff --git a/shared/lib/Resource/Range/RangeTally.php b/shared/lib/Resource/Range/RangeTally.php index 4ce4e6d..02db49c 100644 --- a/shared/lib/Resource/Range/RangeTally.php +++ b/shared/lib/Resource/Range/RangeTally.php @@ -15,6 +15,24 @@ class RangeTally extends Range implements IRangeTally { protected string|int $position = 0; protected int $tally = 32; + public function jsonDeserialize(array|string $data): static { + if (is_string($data)) { + $data = json_decode($data, true, flags: JSON_THROW_ON_ERROR); + } + + if (isset($data['anchor'])) { + $this->setAnchor(RangeAnchorType::from($data['anchor'])); + } + if (isset($data['position'])) { + $this->setPosition($data['position']); + } + if (isset($data['tally'])) { + $this->setTally($data['tally']); + } + + return $this; + } + /** * Returns the type of the range *