Skip to content

Commit 21b907c

Browse files
committed
Merge branch 'dev' into feature/more-filter-types
2 parents d749b01 + 6bd1ee1 commit 21b907c

20 files changed

+700
-13
lines changed

src/Endpoints/Block.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function retrieve(): BlockEntity
5454

5555
/**
5656
* Retrieve block children
57-
* url: https://api.notion.com/{version}/blocks/{block_id}/children
57+
* url: https://api.notion.com/{version}/blocks/{block_id}/children [get]
5858
* notion-api-docs: https://developers.notion.com/reference/get-block-children
5959
*
6060
* @return BlockCollection
@@ -71,11 +71,37 @@ public function children(): BlockCollection
7171
}
7272

7373
/**
74-
* @return array
74+
* Append one Block or an array of Blocks
75+
* url: https://api.notion.com/{version}/blocks/{block_id}/children [patch]
76+
* notion-api-docs: https://developers.notion.com/reference/patch-block-children
77+
*
78+
* @return FiveamCode\LaravelNotionApi\Entities\Blocks\Block
7579
* @throws HandlingException
7680
*/
77-
public function create(): array
81+
public function append(array|BlockEntity $appendices): BlockEntity
7882
{
79-
throw new HandlingException('Not implemented');
83+
if (!is_array($appendices)) {
84+
$appendices = [$appendices];
85+
}
86+
$children = [];
87+
88+
foreach ($appendices as $block) {
89+
$children[] = [
90+
"object" => "block",
91+
"type" => $block->getType(),
92+
$block->getType() => $block->getRawContent()
93+
];
94+
}
95+
96+
$body = [
97+
"children" => $children
98+
];
99+
100+
$response = $this->patch(
101+
$this->url(Endpoint::BLOCKS . '/' . $this->blockId . '/children' . ""),
102+
$body
103+
);
104+
105+
return new BlockEntity($response->json());
80106
}
81107
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Blocks;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable;
6+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
7+
8+
/**
9+
* Class TextBlock
10+
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
11+
*/
12+
class BaseFileBlock extends Block implements Modifiable
13+
{
14+
protected static final function createFileBlock(BaseFileBlock $fileBlock, string $url, string $caption = ""): BaseFileBlock
15+
{
16+
$fileBlock->rawContent = [
17+
'type' => 'external',
18+
'caption' => [
19+
[
20+
'type' => 'text',
21+
'text' => [
22+
'content' => $caption
23+
]
24+
]
25+
],
26+
'external' => [
27+
'url' => $url,
28+
]
29+
];
30+
31+
$fileBlock->fillContent();
32+
33+
return $fileBlock;
34+
}
35+
36+
private string $hostingType = "";
37+
private string $url = "";
38+
private RichText $caption;
39+
40+
41+
/**
42+
*
43+
*/
44+
protected function fillFromRaw(): void
45+
{
46+
parent::fillFromRaw();
47+
$this->fillContent();
48+
}
49+
50+
/**
51+
*
52+
*/
53+
protected function fillContent(): void
54+
{
55+
$this->hostingType = $this->rawContent['type'];
56+
$this->url = $this->rawContent[$this->hostingType]['url'];
57+
$this->caption = new RichText($this->rawContent['caption']);
58+
$this->content = $this->url;
59+
}
60+
61+
public function getUrl()
62+
{
63+
return $this->url;
64+
}
65+
66+
public function getHostingType()
67+
{
68+
return $this->hostingType;
69+
}
70+
71+
public function getCaption()
72+
{
73+
return $this->caption;
74+
}
75+
}

src/Entities/Blocks/Block.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected function fillFromRaw(): void
6969
{
7070
$this->fillId();
7171
$this->fillType();
72-
$this->fillContent();
72+
$this->fillRawContent();
7373
$this->fillHasChildren();
7474
$this->fillCreatedTime();
7575
$this->fillLastEditedTime();
@@ -88,7 +88,7 @@ private function fillType(): void
8888
/**
8989
*
9090
*/
91-
private function fillContent(): void
91+
private function fillRawContent(): void
9292
{
9393
if (Arr::exists($this->responseData, $this->getType())) {
9494
$this->rawContent = $this->responseData[$this->getType()];
@@ -161,6 +161,11 @@ public function asText(): string
161161
return $this->text;
162162
}
163163

164+
public function setContent($content)
165+
{
166+
$this->content = $content;
167+
}
168+
164169
/**
165170
* @param $rawContent
166171
* @return Block
@@ -173,6 +178,7 @@ public static function fromResponse($rawContent): Block
173178
return $block;
174179
}
175180

181+
176182
/**
177183
* Maps the type of a block to the corresponding package class by converting the type name.
178184
*
@@ -189,6 +195,11 @@ private static function mapTypeToClass(string $type): string
189195
case 'paragraph':
190196
case 'to_do':
191197
case 'toggle':
198+
case 'embed':
199+
case 'image':
200+
case 'video':
201+
case 'file':
202+
case 'pdf':
192203
$class = str_replace('_', '', ucwords($type, '_'));
193204
return "FiveamCode\\LaravelNotionApi\\Entities\\Blocks\\" . $class;
194205
case 'heading_1':

src/Entities/Blocks/BulletedListItem.php

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

src/Entities/Blocks/ChildPage.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
*/
99
class ChildPage extends Block
1010
{
11+
function __construct(array $responseData = null)
12+
{
13+
$this->type = "child_page";
14+
parent::__construct($responseData);
15+
}
16+
1117
/**
1218
*
1319
*/

src/Entities/Blocks/Embed.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Blocks;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable;
6+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
7+
8+
/**
9+
* Class Paragraph
10+
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
11+
*/
12+
class Embed extends Block implements Modifiable
13+
{
14+
private RichText $caption;
15+
private string $url = "";
16+
17+
public static function create(string $url, string $caption = ""): Embed
18+
{
19+
$embed = new Embed();
20+
21+
$embed->rawContent = [
22+
'url' => $url,
23+
'caption' => [
24+
[
25+
'type' => 'text',
26+
'text' => [
27+
'content' => $caption
28+
]
29+
]
30+
]
31+
];
32+
33+
$embed->fillContent();
34+
35+
return $embed;
36+
}
37+
38+
function __construct(array $responseData = null)
39+
{
40+
$this->type = "embed";
41+
parent::__construct($responseData);
42+
}
43+
44+
/**
45+
*
46+
*/
47+
protected function fillFromRaw(): void
48+
{
49+
parent::fillFromRaw();
50+
$this->fillContent();
51+
}
52+
53+
/**
54+
*
55+
*/
56+
protected function fillContent(): void
57+
{
58+
$this->url = $this->rawContent['url'];
59+
$this->caption = new RichText($this->rawContent['caption']);
60+
$this->content = $this->url;
61+
}
62+
63+
public function getUrl()
64+
{
65+
return $this->url;
66+
}
67+
68+
public function getCaption()
69+
{
70+
return $this->caption;
71+
}
72+
}

src/Entities/Blocks/File.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Blocks;
4+
5+
/**
6+
* Class Paragraph
7+
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
8+
*/
9+
class File extends BaseFileBlock
10+
{
11+
public static function create(string $url, string $caption = ""): File
12+
{
13+
$file = new File();
14+
BaseFileBlock::createFileBlock($file, $url, $caption);
15+
return $file;
16+
}
17+
18+
function __construct(array $responseData = null)
19+
{
20+
$this->type = "file";
21+
parent::__construct($responseData);
22+
}
23+
24+
}

src/Entities/Blocks/HeadingOne.php

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

src/Entities/Blocks/HeadingThree.php

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

src/Entities/Blocks/HeadingTwo.php

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

0 commit comments

Comments
 (0)