Skip to content

Commit

Permalink
Add last edited time page property
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 31, 2021
1 parent c18e5d3 commit ca3720d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Pages/Properties/Factory.php
Expand Up @@ -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}'"),
};
}
Expand Down
70 changes: 70 additions & 0 deletions src/Pages/Properties/LastEditedTime.php
@@ -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);
}
}
8 changes: 4 additions & 4 deletions src/Pages/Properties/Property.php
Expand Up @@ -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
// {
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/Pages/Properties/LastEditedTimeTest.php
@@ -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());
}
}

0 comments on commit ca3720d

Please sign in to comment.