Skip to content

Commit

Permalink
feat(database): inline database (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippe-pixieset committed Jan 10, 2024
1 parent edbfcba commit 5c50917
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/Databases/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* properties: array<string, PropertyMetadataJson>,
* parent: DatabaseParentJson,
* url: string,
* is_inline: bool,
* }
*
* @psalm-immutable
Expand All @@ -56,6 +57,7 @@ private function __construct(
public readonly array $properties,
public readonly DatabaseParent $parent,
public readonly string $url,
public readonly bool $isInline,
) {
if ($cover !== null && $cover->isInternal()) {
throw DatabaseException::internalCover();
Expand All @@ -80,7 +82,8 @@ public static function create(DatabaseParent $parent): self
null,
[ "Title" => Title::create() ],
$parent,
""
"",
false,
);
}

Expand Down Expand Up @@ -142,6 +145,7 @@ function (array $descriptionArray): RichText {
$properties,
$parent,
$array["url"],
$array["is_inline"],
);
}

Expand All @@ -159,6 +163,7 @@ public function toArray(): array
"properties" => $this->propertiesToArray(),
"parent" => $this->parent->toArray(),
"url" => $this->url,
"is_inline" => $this->isInline,
];
}

Expand All @@ -183,6 +188,7 @@ public function changeTitle(string $title): self
$this->properties,
$this->parent,
$this->url,
$this->isInline,
);
}

Expand All @@ -199,6 +205,7 @@ public function changeAdvancedTitle(RichText ...$title): self
$this->properties,
$this->parent,
$this->url,
$this->isInline,
);
}

Expand All @@ -223,6 +230,7 @@ public function changeIcon(Emoji|File|Icon $icon): self
$this->properties,
$this->parent,
$this->url,
$this->isInline,
);
}

Expand All @@ -239,6 +247,7 @@ public function removeIcon(): self
$this->properties,
$this->parent,
$this->url,
$this->isInline,
);
}

Expand All @@ -255,6 +264,7 @@ public function changeCover(File $cover): self
$this->properties,
$this->parent,
$this->url,
$this->isInline,
);
}

Expand All @@ -271,6 +281,7 @@ public function removeCover(): self
$this->properties,
$this->parent,
$this->url,
$this->isInline,
);
}

Expand All @@ -292,6 +303,7 @@ public function addProperty(PropertyInterface $property): self
$this->properties()->add($property)->getAll(),
$this->parent,
$this->url,
$this->isInline,
);
}

Expand All @@ -308,6 +320,7 @@ public function removePropertyByName(string $propertyName): self
$this->properties()->remove($propertyName)->getAll(),
$this->parent,
$this->url,
$this->isInline,
);
}

Expand All @@ -324,6 +337,7 @@ public function changeProperty(PropertyInterface $property): self
$this->properties()->change($property)->getAll(),
$this->parent,
$this->url,
$this->isInline,
);
}

Expand All @@ -341,6 +355,7 @@ public function changeProperties(array $properties): self
PropertyCollection::create(...$properties)->getAll(),
$this->parent,
$this->url,
$this->isInline,
);
}

Expand All @@ -357,6 +372,41 @@ public function changeParent(DatabaseParent $parent): self
$this->properties,
$parent,
$this->url,
$this->isInline,
);
}

public function enableInline(): self
{
return new self(
$this->id,
$this->createdTime,
$this->lastEditedTime,
$this->title,
$this->description,
$this->icon,
$this->cover,
$this->properties,
$this->parent,
$this->url,
true,
);
}

public function disableInline(): self
{
return new self(
$this->id,
$this->createdTime,
$this->lastEditedTime,
$this->title,
$this->description,
$this->icon,
$this->cover,
$this->properties,
$this->parent,
$this->url,
false,
);
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Integration/DatabasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ public function test_create_empty_database(): void
$client->databases()->delete($database);
}

public function test_create_inline_database(): void
{
$client = Helper::client();

$database = Database::create(DatabaseParent::page(Helper::testPageId()))
->changeTitle("Inline database")
->enableInline();

$database = $client->databases()->create($database);

$databaseFound = $client->databases()->find($database->id);

$this->assertEquals("Inline database", $database->title[0]->plainText);
$this->assertTrue($databaseFound->isInline);

$client->databases()->delete($database);
}

public function test_update_database(): void
{
$client = Helper::client();
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Databases/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public function test_array_conversion(): void
"page_id" => "1ce62b6f-b7f3-4201-afd0-08acb02e61c6",
],
"url" => "https://notion.so/a7e80c0ba76643c3a9e921ce94595e0e",
"is_inline" => true,
];
$database = Database::fromArray($array);

Expand All @@ -231,6 +232,7 @@ public function test_array_conversion(): void
"2020-12-08T12:00:00.000000Z",
$database->lastEditedTime->format(Date::FORMAT),
);
$this->assertTrue($database->isInline);
}

public function test_from_array_change_emoji_icon(): void
Expand Down Expand Up @@ -273,6 +275,7 @@ public function test_from_array_change_emoji_icon(): void
"page_id" => "1ce62b6f-b7f3-4201-afd0-08acb02e61c6",
],
"url" => "https://notion.so/a7e80c0ba76643c3a9e921ce94595e0e",
"is_inline" => false,
];
$database = Database::fromArray($array);

Expand Down Expand Up @@ -321,11 +324,25 @@ public function test_from_array_change_file_icon(): void
"page_id" => "1ce62b6f-b7f3-4201-afd0-08acb02e61c6",
],
"url" => "https://notion.so/a7e80c0ba76643c3a9e921ce94595e0e",
"is_inline" => false,
];
$database = Database::fromArray($array);

if ($database->icon?->isFile()) {
$this->assertEquals("https://my-site.com/image.png", $database->icon->file?->url);
}
}

public function test_inline(): void
{
$parent = DatabaseParent::page("1ce62b6f-b7f3-4201-afd0-08acb02e61c6");
$database = Database::create($parent);
$this->assertFalse($database->isInline);

$database = $database->enableInline();
$this->assertTrue($database->isInline);

$database = $database->disableInline();
$this->assertFalse($database->isInline);
}
}

0 comments on commit 5c50917

Please sign in to comment.