Skip to content

Commit

Permalink
Add last edited by database property
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Nov 1, 2021
1 parent b8a6de7 commit 69239eb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Databases/Properties/Factory.php
Expand Up @@ -29,6 +29,7 @@ public static function fromArray(array $array): PropertyInterface
Property::TYPE_CREATED_TIME => CreatedTime::fromArray($array),
Property::TYPE_CREATED_BY => CreatedBy::fromArray($array),
Property::TYPE_LAST_EDITED_TIME => LastEditedTime::fromArray($array),
Property::TYPE_LAST_EDITED_BY => LastEditedBy::fromArray($array),
default => throw new Exception("Invalid property type: '{$type}'"),
};
}
Expand Down
51 changes: 51 additions & 0 deletions src/Databases/Properties/LastEditedBy.php
@@ -0,0 +1,51 @@
<?php

namespace Notion\Databases\Properties;

/**
* @psalm-type LastEditedByJson = array{
* id: string,
* name: string,
* type: "last_edited_by",
* last_edited_by: array<empty, empty>,
* }
*/
class LastEditedBy implements PropertyInterface
{
private const TYPE = Property::TYPE_LAST_EDITED_BY;

private Property $property;

private function __construct(Property $property)
{
$this->property = $property;
}

public static function create(string $propertyName = "LastEditedBy"): self
{
$property = Property::create("", $propertyName, self::TYPE);

return new self($property);
}

public function property(): Property
{
return $this->property;
}

public static function fromArray(array $array): self
{
/** @psalm-var LastEditedByJson $array */
$property = Property::fromArray($array);

return new self($property);
}

public function toArray(): array
{
$array = $this->property->toArray();
$array[self::TYPE] = [];

return $array;
}
}
8 changes: 4 additions & 4 deletions src/Databases/Properties/Property.php
Expand Up @@ -170,8 +170,8 @@ public function isLastEditedTime(): bool
return $this->type === self::TYPE_LAST_EDITED_TIME;
}

// public function isLastEditedBy(): bool
// {
// return $this->type === self::TYPE_LAST_EDITED_BY;
// }
public function isLastEditedBy(): bool
{
return $this->type === self::TYPE_LAST_EDITED_BY;
}
}
33 changes: 33 additions & 0 deletions tests/Unit/Databases/Properties/LastEditedByTest.php
@@ -0,0 +1,33 @@
<?php

namespace Notion\Test\Unit\Databases\Properties;

use Notion\Databases\Properties\Factory;
use Notion\Databases\Properties\LastEditedBy;
use PHPUnit\Framework\TestCase;

class LastEditedByTest extends TestCase
{
public function test_create(): void
{
$lastEditedBy = LastEditedBy::create("Dummy prop name");

$this->assertEquals("Dummy prop name", $lastEditedBy->property()->name());
$this->assertTrue($lastEditedBy->property()->isLastEditedBy());
}

public function test_array_conversion(): void
{
$array = [
"id" => "abc",
"name" => "dummy",
"type" => "last_edited_by",
"last_edited_by" => [],
];
$lastEditedBy = LastEditedBy::fromArray($array);
$fromFactory = Factory::fromArray($array);

$this->assertEquals($array, $lastEditedBy->toArray());
$this->assertEquals($array, $fromFactory->toArray());
}
}

0 comments on commit 69239eb

Please sign in to comment.