-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add UniqueId page and database property (#268)
- Loading branch information
1 parent
b140a87
commit a71c275
Showing
12 changed files
with
241 additions
and
2 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
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,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; | ||
} | ||
} |
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
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,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; | ||
} | ||
} |
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,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()); | ||
} | ||
} |
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,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); | ||
} | ||
} |