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
bf3d05c
commit 7c6d664
Showing
3 changed files
with
170 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
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,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()); | ||
} | ||
} |