-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
bfcedac
commit c18e5d3
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
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 CreatedTimeJson = array{ | ||
* id: string, | ||
* type: "created_time", | ||
* created_time: string, | ||
* } | ||
*/ | ||
class CreatedTime implements PropertyInterface | ||
{ | ||
private const TYPE = Property::TYPE_CREATED_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 CreatedTimeJson $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
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\CreatedTime; | ||
use Notion\Pages\Properties\Factory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CreatedTimeTest extends TestCase | ||
{ | ||
public function test_create(): void | ||
{ | ||
$date = new DateTimeImmutable("2021-01-01T00:00:00.000000Z"); | ||
$time = CreatedTime::create($date); | ||
|
||
$this->assertTrue($time->property()->isCreatedTime()); | ||
$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 = CreatedTime::create($date1)->withTime($date2); | ||
|
||
$this->assertEquals($date2, $time->time()); | ||
} | ||
|
||
public function test_array_conversion(): void | ||
{ | ||
$array = [ | ||
"id" => "abc", | ||
"type" => "created_time", | ||
"created_time" => "2021-01-01T00:00:00.000000Z", | ||
]; | ||
|
||
$time = CreatedTime::fromArray($array); | ||
$fromFactory = Factory::fromArray($array); | ||
|
||
$this->assertEquals($array, $time->toArray()); | ||
$this->assertEquals($array, $fromFactory->toArray()); | ||
} | ||
} |