Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(block): add support to tables #224

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Blocks/BlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ enum BlockType: string
case Bookmark = "bookmark";
case Equation = "equation";
case Divider = "divider";
case Table = "table";
case TableRow = "table_row";
case TableOfContents = "table_of_contents";
case Breadcrumb = "breadcrumb";
case Column = "column";
Expand Down
191 changes: 191 additions & 0 deletions src/Blocks/Table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php

namespace Notion\Blocks;

use Notion\Exceptions\BlockException;

/**
* @psalm-import-type BlockMetadataJson from BlockMetadata
* @psalm-import-type RichTextJson from \Notion\Common\RichText
*
* @psalm-type TableJson = array{
* table: array{
* table_width: int,
* has_column_header: bool,
* has_row_header: bool,
* children: list<BlockMetadataJson>,
* },
* }
*
* @psalm-immutable
*/
class Table implements BlockInterface
{
/** @param TableRow[] $rows */
private function __construct(
private readonly BlockMetadata $metadata,
public readonly int $tableWidth,
public readonly bool $hasColumnHeader,
public readonly bool $hasRowHeader,
public readonly array $rows,
) {
$metadata->checkType(BlockType::Table);
}

public static function create(): self
{
$block = BlockMetadata::create(BlockType::Table);

return new self($block, 1, false, false, []);
}

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

/** @psalm-var TableJson $array */
$table = $array["table"];

$tableWidth = $table["table_width"];
$hasColumnHeader = $table["has_column_header"];
$hasRowHeader = $table["has_row_header"];
$rows = array_map(fn(array $row) => TableRow::fromArray($row), $table["children"]);

return new self($block, $tableWidth, $hasColumnHeader, $hasRowHeader, $rows);
}

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

$array["table"] = [
"table_width" => $this->tableWidth,
"has_column_header" => $this->hasColumnHeader,
"has_row_header" => $this->hasRowHeader,
"children" => array_map(fn(TableRow $row) => $row->toArray(), $this->rows),
];

return $array;
}

public function metadata(): BlockMetadata
{
return $this->metadata;
}

public function changeWidth(int $tableWidth): self
{
return new self(
$this->metadata->update(),
$tableWidth,
$this->hasColumnHeader,
$this->hasRowHeader,
$this->rows,
);
}

public function enableColumnHeader(): self
{
return new self(
$this->metadata->update(),
$this->tableWidth,
true,
$this->hasRowHeader,
$this->rows,
);
}

public function disableColumnHeader(): self
{
return new self(
$this->metadata->update(),
$this->tableWidth,
false,
$this->hasRowHeader,
$this->rows,
);
}

public function enableRowHeader(): self
{
return new self(
$this->metadata->update(),
$this->tableWidth,
$this->hasColumnHeader,
true,
$this->rows,
);
}

public function disableRowHeader(): self
{
return new self(
$this->metadata->update(),
$this->tableWidth,
$this->hasColumnHeader,
false,
$this->rows,
);
}

public function changeRows(TableRow ...$rows): self
{
$hasChildren = (count($rows) > 0);

return new self(
$this->metadata->updateHasChildren($hasChildren),
$this->tableWidth,
$this->hasColumnHeader,
$this->hasRowHeader,
$rows,
);
}

public function addRow(TableRow $row): self
{
$rows = $this->rows;
$rows[] = $row;

return new self(
$this->metadata->updateHasChildren(true),
$this->tableWidth,
$this->hasColumnHeader,
$this->hasRowHeader,
$rows,
);
}

public function changeChildren(BlockInterface ...$children): self
{
foreach ($children as $child) {
if ($child::class !== TableRow::class) {
throw BlockException::wrongType(BlockType::TableRow);
}
}

/** @psalm-var TableRow[] $children */
return $this->changeRows(...$children);
}

public function addChild(BlockInterface $child): self
{
if ($child::class !== TableRow::class) {
throw BlockException::wrongType(BlockType::TableRow);
}

/** @psalm-var TableRow $child */
return $this->addRow($child);
}

public function archive(): BlockInterface
{
return new self(
$this->metadata->archive(),
$this->tableWidth,
$this->hasColumnHeader,
$this->hasRowHeader,
$this->rows,
);
}
}
95 changes: 95 additions & 0 deletions src/Blocks/TableRow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Notion\Blocks;

use Notion\Common\RichText;
use Notion\Exceptions\BlockException;

/**
* @psalm-import-type BlockMetadataJson from BlockMetadata
* @psalm-import-type RichTextJson from \Notion\Common\RichText
*
* @psalm-type TableRowJson = array{
* table_row: array{
* cells: list<list<RichTextJson>>
* },
* }
*
* @psalm-immutable
*/
class TableRow implements BlockInterface
{
/** @param RichText[][] $cells */
private function __construct(
private readonly BlockMetadata $metadata,
public readonly array $cells,
) {
$metadata->checkType(BlockType::TableRow);
}

public static function create(): self
{
$block = BlockMetadata::create(BlockType::TableRow);

return new self($block, []);
}

public static function fromArray(array $array): self
{
/** @psalm-var BlockMetadataJson $array */
$metadata = BlockMetadata::fromArray($array);

/** @psalm-var TableRowJson $array */
$cells = array_map(
fn(array $cell) => array_map(fn(array $text) => RichText::fromArray($text), $cell),
$array["table_row"]["cells"],
);

return new self($metadata, $cells);
}

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

$array["table_row"] = [
"cells" => array_map(
fn(array $c) => array_map(fn(RichText $t) => $t->toArray(), $c),
$this->cells
),
];

return $array;
}

public function addCell(RichText ...$cell): self
{
$cells = $this->cells;
$cells[] = $cell;

return new self($this->metadata, $cells);
}

public function metadata(): BlockMetadata
{
return $this->metadata;
}

public function changeChildren(BlockInterface ...$children): self
{
throw BlockException::noChindrenSupport();
}

public function addChild(BlockInterface $child): self
{
throw BlockException::noChindrenSupport();
}

public function archive(): BlockInterface
{
return new self(
$this->metadata->archive(),
$this->cells,
);
}
}
Loading
Loading