Skip to content

Commit

Permalink
Add relation page propety
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 31, 2021
1 parent f82a15c commit cc65b9e
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Pages/Properties/Factory.php
Expand Up @@ -20,6 +20,7 @@ public static function fromArray(array $array): PropertyInterface
Property::TYPE_SELECT => Select::fromArray($array),
Property::TYPE_MULTI_SELECT => MultiSelect::fromArray($array),
Property::TYPE_FORMULA => Formula::fromArray($array),
Property::TYPE_RELATION => Relation::fromArray($array),
default => throw new Exception("Invalid property type: '{$type}'"),
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/Pages/Properties/Property.php
Expand Up @@ -101,10 +101,10 @@ public function isMultiSelect(): bool
// return $this->type === self::TYPE_FORMULA;
// }

// public function isRelation(): bool
// {
// return $this->type === self::TYPE_RELATION;
// }
public function isRelation(): bool
{
return $this->type === self::TYPE_RELATION;
}

// public function isRollup(): bool
// {
Expand Down
87 changes: 87 additions & 0 deletions src/Pages/Properties/Relation.php
@@ -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);
}
}
59 changes: 59 additions & 0 deletions tests/Unit/Pages/Properties/RelationTest.php
@@ -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());
}
}

0 comments on commit cc65b9e

Please sign in to comment.