Skip to content

Commit

Permalink
feat: add UniqueId page and database property (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Jul 26, 2023
1 parent b140a87 commit a71c275
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Databases/Properties/PropertyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ public function getTitleById(string $propertyId): Title
return $this->getTypedById($propertyId, Title::class);
}

public function getUniqueId(string $propertyName): UniqueId
{
return $this->getTyped($propertyName, UniqueId::class);
}

public function getUniqueIdById(string $propertyName): UniqueId
{
return $this->getTypedById($propertyName, UniqueId::class);
}

public function getUrl(string $propertyName): Url
{
return $this->getTyped($propertyName, Url::class);
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/PropertyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static function fromArray(array $array): PropertyInterface
PropertyType::Select => Select::fromArray($array),
PropertyType::Status => Status::fromArray($array),
PropertyType::Title => Title::fromArray($array),
PropertyType::UniqueId => UniqueId::fromArray($array),
PropertyType::Url => Url::fromArray($array),
default => Unknown::fromArray($array),
};
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/PropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ enum PropertyType: string
case Status = "status";
case Title = "title";
case Url = "url";
case UniqueId = "unique_id";
case Unknown = "unknown";
}
42 changes: 42 additions & 0 deletions src/Databases/Properties/UniqueId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Notion\Databases\Properties;

/**
* @psalm-type UniqueIdJson = array{
* id: string,
* name: string,
* type: "uniqueId",
* unique_id: \stdClass,
* }
*
* @psalm-immutable
*/
class UniqueId implements PropertyInterface
{
private function __construct(
private readonly PropertyMetadata $metadata,
) {
}

public function metadata(): PropertyMetadata
{
return $this->metadata;
}

public static function fromArray(array $array): self
{
/** @psalm-var UniqueIdJson $array */
$metadata = PropertyMetadata::fromArray($array);

return new self($metadata);
}

public function toArray(): array
{
$array = $this->metadata->toArray();
$array["unique_id"] = new \stdClass();

return $array;
}
}
14 changes: 12 additions & 2 deletions src/Pages/Properties/PropertyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,24 @@ public function getStatusById(string $propertyId): Status
return $this->getTypedById($propertyId, Status::class);
}

public function getUniqueId(string $propertyName): UniqueId
{
return $this->getTyped($propertyName, UniqueId::class);
}

public function getUniqueIdById(string $propertyId): UniqueId
{
return $this->getTypedById($propertyId, UniqueId::class);
}

public function getUrl(string $propertyName): Url
{
return $this->getTyped($propertyName, Url::class);
}

public function getUrlById(string $propertyName): Url
public function getUrlById(string $propertyId): Url
{
return $this->getTypedById($propertyName, Url::class);
return $this->getTypedById($propertyId, Url::class);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Pages/Properties/PropertyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static function fromArray(array $array): PropertyInterface
PropertyType::Select->value => Select::fromArray($array),
PropertyType::Status->value => Status::fromArray($array),
PropertyType::Title->value => Title::fromArray($array),
PropertyType::UniqueId->value => UniqueId::fromArray($array),
PropertyType::Url->value => Url::fromArray($array),
default => Unknown::fromArray($array),
};
Expand Down
1 change: 1 addition & 0 deletions src/Pages/Properties/PropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum PropertyType: string
case Select = "select";
case Status = "status";
case Title = "title";
case UniqueId = "unique_id";
case Url = "url";
case Unknown = "unknown";
}
53 changes: 53 additions & 0 deletions src/Pages/Properties/UniqueId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Notion\Pages\Properties;

/**
* @psalm-type UniqueIdJson = array{
* id: string,
* type: "unique_id",
* unique_id: array{
* number: int,
* prefix: string|null,
* },
* }
*
* @psalm-immutable
*/
class UniqueId implements PropertyInterface
{
private function __construct(
private readonly PropertyMetadata $metadata,
public readonly int $number,
public readonly string|null $prefix,
) {
}

public static function fromArray(array $array): self
{
/** @psalm-var UniqueIdJson $array */
$metadata = PropertyMetadata::fromArray($array);

$number = $array["unique_id"]["number"];
$prefix = $array["unique_id"]["prefix"];

return new self($metadata, $number, $prefix);
}

public function toArray(): array
{
$array = $this->metadata->toArray();

$array["unique_id"] = [
"number" => $this->number,
"prefix" => $this->prefix,
];

return $array;
}

public function metadata(): PropertyMetadata
{
return $this->metadata;
}
}
28 changes: 28 additions & 0 deletions tests/Unit/Databases/Properties/PropertyCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,32 @@ public function test_get_url_by_id(): void

$this->assertSame($p, $c->getUrlById("abc"));
}

public function test_get_unique_id(): void
{
$p = Properties\UniqueId::fromArray([
"id" => "abc",
"name" => "dummy",
"type" => "unique_id",
"unique_id" => new \stdClass(),
]);

$c = PropertyCollection::create($p);

$this->assertSame($p, $c->getUniqueId("dummy"));
}

public function test_get_unique_id_by_id(): void
{
$p = Properties\UniqueId::fromArray([
"id" => "abc",
"name" => "dummy",
"type" => "unique_id",
"unique_id" => new \stdClass(),
]);

$c = PropertyCollection::create($p);

$this->assertSame($p, $c->getUniqueIdById("abc"));
}
}
27 changes: 27 additions & 0 deletions tests/Unit/Databases/Properties/UniqueIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Notion\Test\Unit\Databases\Properties;

use Notion\Databases\Properties\PropertyFactory;
use Notion\Databases\Properties\PropertyType;
use Notion\Databases\Properties\UniqueId;
use PHPUnit\Framework\TestCase;

class UniqueIdTest extends TestCase
{
public function test_array_conversion(): void
{
$array = [
"id" => "abc",
"name" => "dummy",
"type" => "unique_id",
"unique_id" => new \stdClass(),
];
$prop = UniqueId::fromArray($array);
$fromFactory = PropertyFactory::fromArray($array);

$this->assertEquals($array, $prop->toArray());
$this->assertSame(PropertyType::UniqueId, $prop->metadata()->type);
$this->assertEquals($array, $fromFactory->toArray());
}
}
32 changes: 32 additions & 0 deletions tests/Unit/Pages/Properties/PropertyCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,4 +620,36 @@ public function test_get_url(): void

$this->assertSame($prop, $c->getUrl("Name"));
}

public function test_get_unique_id_by_id(): void
{
$prop = Properties\UniqueId::fromArray([
"id" => "abc",
"type" => "unique_id",
"unique_id" => [
"number" => 123,
"prefix" => "ISSUE",
],
]);

$c = PropertyCollection::create([ "Name" => $prop ]);

$this->assertSame($prop, $c->getUniqueIdById("abc"));
}

public function test_get_unique_id(): void
{
$prop = Properties\UniqueId::fromArray([
"id" => "abc",
"type" => "unique_id",
"unique_id" => [
"number" => 123,
"prefix" => "ISSUE",
],
]);

$c = PropertyCollection::create([ "Name" => $prop ]);

$this->assertSame($prop, $c->getUniqueId("Name"));
}
}
33 changes: 33 additions & 0 deletions tests/Unit/Pages/Properties/UniqueIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Notion\Test\Unit\Pages\Properties;

use Notion\Pages\Properties\PropertyFactory;
use Notion\Pages\Properties\PropertyType;
use Notion\Pages\Properties\UniqueId;
use PHPUnit\Framework\TestCase;

class UniqueIdTest extends TestCase
{
public function test_array_conversion(): void
{
$array = [
"id" => "abc",
"type" => "unique_id",
"unique_id" => [
"number" => 3,
"prefix" => "ISSUE"
]
];

$prop = UniqueId::fromArray($array);
$fromFactory = PropertyFactory::fromArray($array);

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

$this->assertSame("ISSUE", $prop->prefix);
$this->assertSame(3, $prop->number);
$this->assertSame(PropertyType::UniqueId, $prop->metadata()->type);
}
}

0 comments on commit a71c275

Please sign in to comment.