diff --git a/docs/blocks/BulletedListItem.md b/docs/blocks/BulletedListItem.md new file mode 100644 index 00000000..39a90884 --- /dev/null +++ b/docs/blocks/BulletedListItem.md @@ -0,0 +1,57 @@ +# Bulleted list item + +## Create empty item + +```php +$item = BulletedListItem::create(); +``` + +## Create from string + +Bulleted list items can be created from simple strings. + +```php +$item = BulletedListItem::fromString("Item content"); + +echo $item->toString(); // "Item content" +``` + +## Create from `RichText` + +```php +$text = RichText::createText("Item text")->italic(); + +$item = BulletedListItem::create()->withText($text); +``` + +## Add text + +```php +$item = BulletedListItem::fromString("Item text"); +$item = $item->withText( + RichText::createText(" can be extended!") +); + +echo $item->toString(); // "Item text can be extended!" +``` + +## Add child + +```php +$item = BulletedListItem::fromString("Item text"); + +$item = $item->appendChild( + Paragraph::fromString("A simple child paragraph.") +); +``` + +## Change children + +```php +$item = BulletedListItem::fromString("Item text"); + +$item = $item->withChildren( + Paragraph::fromString("Child paragraph 1"), + Paragraph::fromString("Child paragraph 2"), +); +``` \ No newline at end of file diff --git a/src/Blocks/BulletedListItem.php b/src/Blocks/BulletedListItem.php index 456f225d..4e876994 100644 --- a/src/Blocks/BulletedListItem.php +++ b/src/Blocks/BulletedListItem.php @@ -5,6 +5,8 @@ use Notion\Common\RichText; /** + * Bulleted list item + * * @psalm-import-type BlockJson from Block * @psalm-import-type RichTextJson from \Notion\Common\RichText * @@ -46,6 +48,9 @@ private function __construct( $this->children = $children; } + /** + * Create empty bulleted list item + */ public static function create(): self { $block = Block::create(self::TYPE); @@ -53,6 +58,9 @@ public static function create(): self return new self($block, [], []); } + /** + * Create bulleted list item from a string + */ public static function fromString(string $content): self { $block = Block::create(self::TYPE); @@ -61,6 +69,7 @@ public static function fromString(string $content): self return new self($block, $text, []); } + /** @internal */ public static function fromArray(array $array): self { /** @psalm-var BlockJson $array */ @@ -76,6 +85,7 @@ public static function fromArray(array $array): self return new self($block, $text, $children); } + /** @internal */ public function toArray(): array { $array = $this->block->toArray(); @@ -88,6 +98,7 @@ public function toArray(): array return $array; } + /** Get item content as string */ public function toString(): string { $string = ""; @@ -98,26 +109,43 @@ public function toString(): string return $string; } + /** Get block common object */ public function block(): Block { return $this->block; } + /** + * Get list item text + * + * @return RichText[] + */ public function text(): array { return $this->text; } + /** + * Get children blocks + * + * @return BlockInterface[] + */ public function children(): array { return $this->children; } + /** + * Change list item text + */ public function withText(RichText ...$text): self { return new self($this->block, $text, $this->children); } + /** + * Append text to list item + */ public function appendText(RichText $text): self { $texts = $this->text; @@ -126,6 +154,7 @@ public function appendText(RichText $text): self return new self($this->block, $texts, $this->children); } + /** Change list item children blocks */ public function withChildren(BlockInterface ...$children): self { $hasChildren = (count($children) > 0); @@ -137,6 +166,7 @@ public function withChildren(BlockInterface ...$children): self ); } + /** Append child block */ public function appendChild(BlockInterface $child): self { $children = $this->children;