Skip to content

Commit

Permalink
Add table of contents block
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 21, 2021
1 parent 4f95996 commit a276a3d
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Blocks/BlockFactory.php
Expand Up @@ -35,6 +35,7 @@ public static function fromArray(array $array): BlockInterface
Block::TYPE_BOOKMARK => Bookmark::fromArray($array),
Block::TYPE_EQUATION => EquationBlock::fromArray($array),
Block::TYPE_DIVIDER => Divider::fromArray($array),
Block::TYPE_TABLE_OF_CONTENTS => TableOfContents::fromArray($array),
default => throw new Exception("Invalid block type '{$type}'"),
};
}
Expand Down
54 changes: 54 additions & 0 deletions src/Blocks/TableOfContents.php
@@ -0,0 +1,54 @@
<?php

namespace Notion\Blocks;

/**
* @psalm-import-type BlockJson from Block
*
* @psalm-type TableOfContentsJson = array{
* table_of_contents: array<empty, empty>
* }
*/
class TableOfContents implements BlockInterface
{
private const TYPE = Block::TYPE_TABLE_OF_CONTENTS;

private Block $block;

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

$this->block = $block;
}

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

return new self($block);
}

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

return new self($block);
}

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

$array[self::TYPE] = [];

return $array;
}

public function block(): Block
{
return $this->block;
}
}
72 changes: 72 additions & 0 deletions tests/Unit/Blocks/TableOfContentsTest.php
@@ -0,0 +1,72 @@
<?php

namespace Notion\Test\Unit\Blocks;

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

class TableOfContentsTest extends TestCase
{
public function test_create_table_of_contents(): void
{
$tableOfContents = TableOfContents::create();

$this->assertEquals("table_of_contents", $tableOfContents->block()->type());
}

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" => "table_of_contents",
"table_of_contents" => [],
];

$tableOfContents = TableOfContents::fromArray($array);

$this->assertTrue($tableOfContents->block()->isTableOfContents());

$this->assertEquals($tableOfContents, 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",
"table_of_contents" => [],
];

TableOfContents::fromArray($array);
}

public function test_transform_in_array(): void
{
$tableOfContents = TableOfContents::create();

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

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

0 comments on commit a276a3d

Please sign in to comment.