Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c18e5d3
commit ca3720d
Showing
4 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Notion\Pages\Properties; | ||
|
||
use DateTimeImmutable; | ||
use Notion\Common\Date; | ||
|
||
/** | ||
* @psalm-type LastEditedTimeJson = array{ | ||
* id: string, | ||
* type: "last_edited_time", | ||
* last_edited_time: string, | ||
* } | ||
*/ | ||
class LastEditedTime implements PropertyInterface | ||
{ | ||
private const TYPE = Property::TYPE_LAST_EDITED_TIME; | ||
|
||
private Property $property; | ||
|
||
private DateTimeImmutable $time; | ||
|
||
private function __construct(Property $property, DateTimeImmutable $time) | ||
{ | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Notion\Test\Unit\Pages\Properties; | ||
|
||
use DateTimeImmutable; | ||
use Notion\Pages\Properties\LastEditedTime; | ||
use Notion\Pages\Properties\Factory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class LastEditedTimeTest extends TestCase | ||
{ | ||
public function test_create(): void | ||
{ | ||
$date = new DateTimeImmutable("2021-01-01T00:00:00.000000Z"); | ||
$time = LastEditedTime::create($date); | ||
|
||
$this->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()); | ||
} | ||
} |