Skip to content

Commit

Permalink
Document bulleted list item
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Nov 3, 2021
1 parent 99135e6 commit a64c105
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
57 changes: 57 additions & 0 deletions 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"),
);
```
30 changes: 30 additions & 0 deletions src/Blocks/BulletedListItem.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -46,13 +48,19 @@ private function __construct(
$this->children = $children;
}

/**
* Create empty bulleted list item
*/
public static function create(): self
{
$block = Block::create(self::TYPE);

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

/**
* Create bulleted list item from a string
*/
public static function fromString(string $content): self
{
$block = Block::create(self::TYPE);
Expand All @@ -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 */
Expand All @@ -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();
Expand All @@ -88,6 +98,7 @@ public function toArray(): array
return $array;
}

/** Get item content as string */
public function toString(): string
{
$string = "";
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -137,6 +166,7 @@ public function withChildren(BlockInterface ...$children): self
);
}

/** Append child block */
public function appendChild(BlockInterface $child): self
{
$children = $this->children;
Expand Down

0 comments on commit a64c105

Please sign in to comment.