Skip to content

Commit e1de3e8

Browse files
committed
full implementation for block creation + tests
- implemented static creation-function for all modifiable blocks - implemented append-function to Block-Endpoint - implemented tests-function, in which all block-creation-functions are tested - added todo/warning for notion-version-change to test-function
1 parent 367cd32 commit e1de3e8

File tree

13 files changed

+285
-90
lines changed

13 files changed

+285
-90
lines changed

src/Endpoints/Block.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
77
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
88
use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection;
9+
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block as BaseBlockEntity;
910

1011
/**
1112
* Class Block
@@ -33,7 +34,7 @@ public function __construct(Notion $notion, string $blockId)
3334

3435
/**
3536
* Retrieve block children
36-
* url: https://api.notion.com/{version}/blocks/{block_id}/children
37+
* url: https://api.notion.com/{version}/blocks/{block_id}/children [get]
3738
* notion-api-docs: https://developers.notion.com/reference/get-block-children
3839
*
3940
* @return BlockCollection
@@ -50,11 +51,37 @@ public function children(): BlockCollection
5051
}
5152

5253
/**
53-
* @return array
54+
* Append one Block or an array of Blocks
55+
* url: https://api.notion.com/{version}/blocks/{block_id}/children [patch]
56+
* notion-api-docs: https://developers.notion.com/reference/patch-block-children
57+
*
58+
* @return FiveamCode\LaravelNotionApi\Entities\Blocks\Block
5459
* @throws HandlingException
5560
*/
56-
public function create(): array
61+
public function append(array|BaseBlockEntity $appendices): BaseBlockEntity
5762
{
58-
throw new HandlingException('Not implemented');
63+
if (!is_array($appendices)) {
64+
$appendices = [$appendices];
65+
}
66+
$children = [];
67+
68+
foreach ($appendices as $block) {
69+
array_push($children, [
70+
"object" => "block",
71+
"type" => $block->getType(),
72+
$block->getType() => $block->getRawContent()
73+
]);
74+
}
75+
76+
$body = [
77+
"children" => $children
78+
];
79+
80+
$response = $this->patch(
81+
$this->url(Endpoint::BLOCKS . '/' . $this->blockId . '/children' . ""),
82+
$body
83+
);
84+
85+
return new BaseBlockEntity($response->json());
5986
}
6087
}

src/Entities/Blocks/Block.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ public function asText() : string
161161
return $this->text;
162162
}
163163

164+
public function setContent($content){
165+
$this->content = $content;
166+
}
167+
164168
/**
165169
* @param $rawContent
166170
* @return Block
@@ -173,6 +177,7 @@ public static function fromResponse($rawContent): Block
173177
return $block;
174178
}
175179

180+
176181
/**
177182
* Maps the type of a block to the corresponding package class by converting the type name.
178183
*

src/Entities/Blocks/BulletedListItem.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313
*/
1414
class BulletedListItem extends TextBlock
1515
{
16+
public static function create(array|string $textContent): BulletedListItem
17+
{
18+
$bulletedListItem = new BulletedListItem();
19+
TextBlock::createTextBlock($bulletedListItem, $textContent);
20+
return $bulletedListItem;
21+
}
22+
23+
function __construct(array $responseData = null){
24+
$this->type = "bulleted_list_item";
25+
parent::__construct($responseData);
26+
}
1627
}

src/Entities/Blocks/HeadingOne.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313
*/
1414
class HeadingOne extends TextBlock
1515
{
16+
public static function create(array|string $textContent): HeadingOne
17+
{
18+
$headingOne = new HeadingOne();
19+
TextBlock::createTextBlock($headingOne, $textContent);
20+
return $headingOne;
21+
}
22+
23+
function __construct(array $responseData = null){
24+
$this->type = "heading_1";
25+
parent::__construct($responseData);
26+
}
1627
}

src/Entities/Blocks/HeadingThree.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313
*/
1414
class HeadingThree extends TextBlock
1515
{
16+
public static function create(array|string $textContent): HeadingThree
17+
{
18+
$headingThree = new HeadingThree();
19+
HeadingThree::createTextBlock($headingThree, $textContent);
20+
return $headingThree;
21+
}
22+
23+
function __construct(array $responseData = null){
24+
$this->type = "heading_3";
25+
parent::__construct($responseData);
26+
}
1627
}

src/Entities/Blocks/HeadingTwo.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313
*/
1414
class HeadingTwo extends TextBlock
1515
{
16+
public static function create(array|string $textContent): HeadingTwo
17+
{
18+
$headingTwo = new HeadingTwo();
19+
HeadingTwo::createTextBlock($headingTwo, $textContent);
20+
return $headingTwo;
21+
}
22+
23+
function __construct(array $responseData = null){
24+
$this->type = "heading_2";
25+
parent::__construct($responseData);
26+
}
1627
}

src/Entities/Blocks/NumberedListItem.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313
*/
1414
class NumberedListItem extends TextBlock
1515
{
16+
public static function create(array|string $textContent): NumberedListItem
17+
{
18+
$numberedListItem = new NumberedListItem();
19+
TextBlock::createTextBlock($numberedListItem, $textContent);
20+
return $numberedListItem;
21+
}
22+
23+
function __construct(array $responseData = null){
24+
$this->type = "numbered_list_item";
25+
parent::__construct($responseData);
26+
}
1627
}

src/Entities/Blocks/Paragraph.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,15 @@
1414
*/
1515
class Paragraph extends TextBlock
1616
{
17+
public static function create(array|string $textContent): Paragraph
18+
{
19+
$paragraph = new Paragraph();
20+
TextBlock::createTextBlock($paragraph, $textContent);
21+
return $paragraph;
22+
}
23+
24+
function __construct(array $responseData = null){
25+
$this->type = "paragraph";
26+
parent::__construct($responseData);
27+
}
1728
}

src/Entities/Blocks/TextBlock.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FiveamCode\LaravelNotionApi\Entities\Blocks;
44

55
use DateTime;
6+
use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable;
67
use Illuminate\Support\Arr;
78
use FiveamCode\LaravelNotionApi\Entities\Entity;
89
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
@@ -12,8 +13,31 @@
1213
* Class TextBlock
1314
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
1415
*/
15-
class TextBlock extends Block
16+
class TextBlock extends Block implements Modifiable
1617
{
18+
protected static function createTextBlock(TextBlock $textBlock, array|string $textContent): TextBlock
19+
{
20+
if (is_string($textContent)) {
21+
$textContent = [$textContent];
22+
}
23+
24+
$text = [];
25+
foreach ($textContent as $textItem) {
26+
array_push($text, [
27+
"type" => "text",
28+
"text" => [
29+
"content" => $textItem
30+
]
31+
]);
32+
}
33+
34+
$textBlock->rawContent = [
35+
"text" => $text
36+
];
37+
38+
return $textBlock;
39+
}
40+
1741
/**
1842
*
1943
*/

src/Entities/Blocks/ToDo.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313
*/
1414
class ToDo extends TextBlock
1515
{
16+
public static function create(array|string $textContent): ToDo
17+
{
18+
$toDo = new ToDo();
19+
TextBlock::createTextBlock($toDo, $textContent);
20+
return $toDo;
21+
}
22+
23+
function __construct(array $responseData = null){
24+
$this->type = "to_do";
25+
parent::__construct($responseData);
26+
}
1627
}

0 commit comments

Comments
 (0)