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
f82a15c
commit cc65b9e
Showing
4 changed files
with
151 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
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,87 @@ | ||
<?php | ||
|
||
namespace Notion\Pages\Properties; | ||
|
||
/** | ||
* @psalm-type RelationJson = array{ | ||
* id: string, | ||
* type: "relation", | ||
* relation: array{ id: string }[], | ||
* } | ||
*/ | ||
class Relation implements PropertyInterface | ||
{ | ||
private const TYPE = Property::TYPE_RELATION; | ||
|
||
private Property $property; | ||
|
||
/** @var string[] */ | ||
private array $pageIds; | ||
|
||
private function __construct(Property $property, string ...$pageIds) | ||
{ | ||
$this->property = $property; | ||
$this->pageIds = $pageIds; | ||
} | ||
|
||
public static function create(string ...$pageIds): self | ||
{ | ||
$property = Property::create("", self::TYPE); | ||
|
||
return new self($property, ...$pageIds); | ||
} | ||
|
||
public static function fromArray(array $array): self | ||
{ | ||
/** @psalm-var RelationJson $array */ | ||
|
||
$property = Property::fromArray($array); | ||
|
||
$pageIds = array_map( | ||
function (array $pageReference): string { | ||
return $pageReference["id"]; | ||
}, | ||
$array[self::TYPE], | ||
); | ||
|
||
return new self($property, ...$pageIds); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$array = $this->property->toArray(); | ||
|
||
$array[self::TYPE] = array_map( | ||
function (string $pageId): array { | ||
return [ "id" => $pageId ]; | ||
}, | ||
$this->pageIds, | ||
); | ||
|
||
return $array; | ||
} | ||
|
||
public function property(): Property | ||
{ | ||
return $this->property; | ||
} | ||
|
||
/** @return string[] */ | ||
public function pageIds(): array | ||
{ | ||
return $this->pageIds; | ||
} | ||
|
||
public function withRelations(string ...$pageIds): self | ||
{ | ||
return new self($this->property, ...$pageIds); | ||
} | ||
|
||
public function addRelation(string $pageId): self | ||
{ | ||
$pageIds = $this->pageIds; | ||
$pageIds[] = $pageId; | ||
|
||
return new self($this->property, ...$pageIds); | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
namespace Notion\Test\Unit\Pages\Properties; | ||
|
||
use Notion\Pages\Properties\Factory; | ||
use Notion\Pages\Properties\Relation; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RelationTest extends TestCase | ||
{ | ||
public function test_create(): void | ||
{ | ||
$id1 = "5604389a-8de1-4ba6-a07f-ca346ff98f00"; | ||
$id2 = "03d71291-5ca3-4daa-a1e8-f012d513e8c8"; | ||
|
||
$relation = Relation::create($id1, $id2); | ||
|
||
$this->assertEquals([$id1, $id2], $relation->pageIds()); | ||
$this->assertTrue($relation->property()->isRelation()); | ||
} | ||
|
||
public function test_replace_relation(): void | ||
{ | ||
$id1 = "5604389a-8de1-4ba6-a07f-ca346ff98f00"; | ||
$id2 = "03d71291-5ca3-4daa-a1e8-f012d513e8c8"; | ||
|
||
$relation = Relation::create($id1)->withRelations($id2); | ||
|
||
$this->assertEquals([$id2], $relation->pageIds()); | ||
} | ||
|
||
public function test_add_relation(): void | ||
{ | ||
$id1 = "5604389a-8de1-4ba6-a07f-ca346ff98f00"; | ||
$id2 = "03d71291-5ca3-4daa-a1e8-f012d513e8c8"; | ||
|
||
$relation = Relation::create($id1)->addRelation($id2); | ||
|
||
$this->assertEquals([$id1, $id2], $relation->pageIds()); | ||
} | ||
|
||
public function test_array_conversion(): void | ||
{ | ||
$array = [ | ||
"id" => "abc", | ||
"type" => "relation", | ||
"relation" => [ | ||
[ "id" => "264f3f43-3d87-4bb2-bb66-11812ab74eae" ], | ||
[ "id" => "f3902b7f-e9e2-4406-8c3f-5d07dbc87d66" ], | ||
], | ||
]; | ||
|
||
$relation = Relation::fromArray($array); | ||
$fromFactory = Factory::fromArray($array); | ||
|
||
$this->assertEquals($array, $relation->toArray()); | ||
$this->assertEquals($array, $fromFactory->toArray()); | ||
} | ||
} |