diff --git a/src/Pages/Properties/Factory.php b/src/Pages/Properties/Factory.php index 4a1ab794..0b2e7ed9 100644 --- a/src/Pages/Properties/Factory.php +++ b/src/Pages/Properties/Factory.php @@ -27,6 +27,7 @@ public static function fromArray(array $array): PropertyInterface Property::TYPE_EMAIL => Email::fromArray($array), Property::TYPE_PHONE_NUMBER => PhoneNumber::fromArray($array), Property::TYPE_CREATED_TIME => CreatedTime::fromArray($array), + Property::TYPE_LAST_EDITED_TIME => LastEditedTime::fromArray($array), default => throw new Exception("Invalid property type: '{$type}'"), }; } diff --git a/src/Pages/Properties/LastEditedTime.php b/src/Pages/Properties/LastEditedTime.php new file mode 100644 index 00000000..7bfd172a --- /dev/null +++ b/src/Pages/Properties/LastEditedTime.php @@ -0,0 +1,70 @@ +property = $property; + $this->time = $time; + } + + public static function create(DateTimeImmutable $time): self + { + $property = Property::create("", self::TYPE); + + return new self($property, $time); + } + + public static function fromArray(array $array): self + { + /** @psalm-var LastEditedTimeJson $array */ + + $property = Property::fromArray($array); + + $time = new DateTimeImmutable($array[self::TYPE]); + + return new self($property, $time); + } + + public function toArray(): array + { + $array = $this->property->toArray(); + + $array[self::TYPE] = $this->time->format(Date::FORMAT); + + return $array; + } + + public function property(): Property + { + return $this->property; + } + + public function time(): DateTimeImmutable + { + return $this->time; + } + + public function withTime(DateTimeImmutable $time): self + { + return new self($this->property, $time); + } +} diff --git a/src/Pages/Properties/Property.php b/src/Pages/Properties/Property.php index 7b66d946..cc1d0311 100644 --- a/src/Pages/Properties/Property.php +++ b/src/Pages/Properties/Property.php @@ -156,10 +156,10 @@ public function isCreatedTime(): bool // return $this->type === self::TYPE_CREATED_BY; // } - // public function isLastEditedTime(): bool - // { - // return $this->type === self::TYPE_LAST_EDITED_TIME; - // } + public function isLastEditedTime(): bool + { + return $this->type === self::TYPE_LAST_EDITED_TIME; + } // public function isLastEditedBy(): bool // { diff --git a/tests/Unit/Pages/Properties/LastEditedTimeTest.php b/tests/Unit/Pages/Properties/LastEditedTimeTest.php new file mode 100644 index 00000000..aef1d887 --- /dev/null +++ b/tests/Unit/Pages/Properties/LastEditedTimeTest.php @@ -0,0 +1,45 @@ +assertTrue($time->property()->isLastEditedTime()); + $this->assertEquals($date, $time->time()); + } + + public function test_change_time(): void + { + $date1 = new DateTimeImmutable("2021-01-01T00:00:00.000000Z"); + $date2 = new DateTimeImmutable("2022-01-01T00:00:00.000000Z"); + + $time = LastEditedTime::create($date1)->withTime($date2); + + $this->assertEquals($date2, $time->time()); + } + + public function test_array_conversion(): void + { + $array = [ + "id" => "abc", + "type" => "last_edited_time", + "last_edited_time" => "2021-01-01T00:00:00.000000Z", + ]; + + $time = LastEditedTime::fromArray($array); + $fromFactory = Factory::fromArray($array); + + $this->assertEquals($array, $time->toArray()); + $this->assertEquals($array, $fromFactory->toArray()); + } +}