feat: add deserilazation

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-07-03 21:33:28 -04:00
parent 8f61c4d2d5
commit fe78426a5d
4 changed files with 41 additions and 2 deletions
+3 -1
View File
@@ -9,7 +9,9 @@ declare(strict_types=1);
namespace KTXF\Resource\Range; namespace KTXF\Resource\Range;
interface IRange { use KTXF\Json\JsonDeserializable;
interface IRange extends JsonDeserializable {
/** /**
* Gets the type of this range * Gets the type of this range
+4
View File
@@ -11,6 +11,10 @@ namespace KTXF\Resource\Range;
class Range implements IRange { class Range implements IRange {
public function jsonDeserialize(array|string $data): static {
return $this;
}
/** /**
* Returns the type of the range * Returns the type of the range
* *
+16 -1
View File
@@ -9,7 +9,7 @@ declare(strict_types=1);
namespace KTXF\Resource\Range; namespace KTXF\Resource\Range;
use DateTime; use DateTimeImmutable;
use DateTimeInterface; use DateTimeInterface;
class RangeDate extends Range implements IRangeDate { class RangeDate extends Range implements IRangeDate {
@@ -17,6 +17,21 @@ class RangeDate extends Range implements IRangeDate {
protected DateTimeInterface $start; protected DateTimeInterface $start;
protected DateTimeInterface $end; 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 * Returns the type of the range
* *
+18
View File
@@ -15,6 +15,24 @@ class RangeTally extends Range implements IRangeTally {
protected string|int $position = 0; protected string|int $position = 0;
protected int $tally = 32; 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 * Returns the type of the range
* *