Skip to content

Commit

Permalink
Add child database block
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 21, 2021
1 parent bf3d05c commit 7c6d664
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Blocks/BlockFactory.php
Expand Up @@ -26,6 +26,7 @@ public static function fromArray(array $array): BlockInterface
Block::TYPE_TOGGLE => Toggle::fromArray($array),
Block::TYPE_CODE => Code::fromArray($array),
Block::TYPE_CHILD_PAGE => ChildPage::fromArray($array),
Block::TYPE_CHILD_DATABASE => ChildDatabase::fromArray($array),
default => throw new Exception("Invalid block type '{$type}'"),
};
}
Expand Down
79 changes: 79 additions & 0 deletions src/Blocks/ChildDatabase.php
@@ -0,0 +1,79 @@
<?php

namespace Notion\Blocks;

use Notion\Common\RichText;

/**
* @psalm-import-type BlockJson from Block
*
* @psalm-type ChildDatabaseJson = array{
* child_database: array{ title: string },
* }
*/
class ChildDatabase implements BlockInterface
{
private const TYPE = Block::TYPE_CHILD_DATABASE;

private Block $block;

private string $databaseTitle;

private function __construct(Block $block, string $databaseTitle) {
if (!$block->isChildDatabase()) {
throw new \Exception("Block must be of type " . self::TYPE);
}

$this->block = $block;
$this->databaseTitle = $databaseTitle;
}

public static function create(): self
{
$block = Block::create(self::TYPE);

return new self($block, "");
}

public static function fromString(string $databaseTitle): self
{
$block = Block::create(self::TYPE);

return new self($block, $databaseTitle);
}

public static function fromArray(array $array): self
{
/** @psalm-var BlockJson $array */
$block = Block::fromArray($array);

/** @psalm-var ChildDatabaseJson $array */
$databaseTitle = $array[self::TYPE]["title"];

return new self($block, $databaseTitle);
}

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

$array[self::TYPE] = [ "title" => $this->databaseTitle ];

return $array;
}

public function block(): Block
{
return $this->block;
}

public function databaseTitle(): string
{
return $this->databaseTitle;
}

public function withDatabaseTitle(string $databaseTitle): self
{
return new self($this->block, $databaseTitle);
}
}
90 changes: 90 additions & 0 deletions tests/Unit/Blocks/ChildDatabaseTest.php
@@ -0,0 +1,90 @@
<?php

namespace Notion\Test\Unit\Blocks;

use Notion\Blocks\BlockFactory;
use Notion\Blocks\ChildDatabase;
use Notion\Common\Date;
use PHPUnit\Framework\TestCase;

class ChildDatabaseTest extends TestCase
{
public function test_create_empty_heading(): void
{
$heading = ChildDatabase::create();

$this->assertEmpty($heading->databaseTitle());
}

public function test_create_from_string(): void
{
$heading = ChildDatabase::fromString("Database title");

$this->assertEquals("Database title", $heading->databaseTitle());
}

public function test_create_from_array(): void
{
$array = [
"object" => "block",
"id" => "04a13895-f072-4814-8af7-cd11af127040",
"created_time" => "2021-10-18T17:09:00.000Z",
"last_edited_time" => "2021-10-18T17:09:00.000Z",
"archived" => false,
"has_children" => false,
"type" => "child_database",
"child_database" => [ "title" => "Database title" ],
];

$childDatabase = ChildDatabase::fromArray($array);

$this->assertEquals("Database title", $childDatabase->databaseTitle());
$this->assertFalse($childDatabase->block()->archived());

$this->assertEquals($childDatabase, BlockFactory::fromArray($array));
}

public function test_error_on_wrong_type(): void
{
$this->expectException(\Exception::class);
$array = [
"object" => "block",
"id" => "04a13895-f072-4814-8af7-cd11af127040",
"created_time" => "2021-10-18T17:09:00.000Z",
"last_edited_time" => "2021-10-18T17:09:00.000Z",
"archived" => false,
"has_children" => false,
"type" => "wrong-type",
"child_database" => [ "title" => "Wrong array" ],
];

ChildDatabase::fromArray($array);
}

public function test_transform_in_array(): void
{
$childDatabase = ChildDatabase::fromString("Database title");

$expected = [
"object" => "block",
"created_time" => $childDatabase->block()->createdTime()->format(Date::FORMAT),
"last_edited_time" => $childDatabase->block()->createdTime()->format(Date::FORMAT),
"archived" => false,
"has_children" => false,
"type" => "child_database",
"child_database" => [ "title" => "Database title" ],
];

$this->assertEquals($expected, $childDatabase->toArray());
}

public function test_replace_page_title(): void
{
$oldHeading = ChildDatabase::fromString("Database 1");

$newHeading = $oldHeading->withDatabaseTitle("Database 2");

$this->assertEquals("Database 1", $oldHeading->databaseTitle());
$this->assertEquals("Database 2", $newHeading->databaseTitle());
}
}

0 comments on commit 7c6d664

Please sign in to comment.