diff --git a/src/Entities/Blocks/BaseFileBlock.php b/src/Entities/Blocks/BaseFileBlock.php new file mode 100644 index 0000000..6f4f679 --- /dev/null +++ b/src/Entities/Blocks/BaseFileBlock.php @@ -0,0 +1,79 @@ +rawContent = [ + 'type' => 'external', + 'caption' => [ + [ + 'type' => 'text', + 'text' => [ + 'content' => $caption + ] + ] + ], + 'external' => [ + 'url' => $url, + ] + ]; + + $fileBlock->fillContent(); + + return $fileBlock; + } + + private string $hostingType = ""; + private string $url = ""; + private RichText $caption; + + + /** + * + */ + protected function fillFromRaw(): void + { + parent::fillFromRaw(); + $this->fillContent(); + } + + /** + * + */ + protected function fillContent(): void + { + $this->hostingType = $this->rawContent['type']; + $this->url = $this->rawContent[$this->hostingType]['url']; + $this->caption = new RichText($this->rawContent['caption']); + $this->content = $this->url; + } + + public function getUrl() + { + return $this->url; + } + + public function getHostingType() + { + return $this->hostingType; + } + + public function getCaption() + { + return $this->caption; + } +} diff --git a/src/Entities/Blocks/Block.php b/src/Entities/Blocks/Block.php index c8a97f7..4ec1be5 100644 --- a/src/Entities/Blocks/Block.php +++ b/src/Entities/Blocks/Block.php @@ -69,7 +69,7 @@ protected function fillFromRaw(): void { $this->fillId(); $this->fillType(); - $this->fillContent(); + $this->fillRawContent(); $this->fillHasChildren(); $this->fillCreatedTime(); $this->fillLastEditedTime(); @@ -88,7 +88,7 @@ private function fillType(): void /** * */ - private function fillContent(): void + private function fillRawContent(): void { if (Arr::exists($this->responseData, $this->getType())) { $this->rawContent = $this->responseData[$this->getType()]; @@ -156,12 +156,13 @@ public function getContent() /** * @return string */ - public function asText() : string + public function asText(): string { return $this->text; } - public function setContent($content){ + public function setContent($content) + { $this->content = $content; } @@ -194,6 +195,11 @@ private static function mapTypeToClass(string $type): string case 'paragraph': case 'to_do': case 'toggle': + case 'embed': + case 'image': + case 'video': + case 'file': + case 'pdf': $class = str_replace('_', '', ucwords($type, '_')); return "FiveamCode\\LaravelNotionApi\\Entities\\Blocks\\" . $class; case 'heading_1': diff --git a/src/Entities/Blocks/ChildPage.php b/src/Entities/Blocks/ChildPage.php index f1577ee..c7ef831 100644 --- a/src/Entities/Blocks/ChildPage.php +++ b/src/Entities/Blocks/ChildPage.php @@ -13,6 +13,11 @@ */ class ChildPage extends Block { + function __construct(array $responseData = null){ + $this->type = "child_page"; + parent::__construct($responseData); + } + /** * */ diff --git a/src/Entities/Blocks/Embed.php b/src/Entities/Blocks/Embed.php new file mode 100644 index 0000000..8b0b859 --- /dev/null +++ b/src/Entities/Blocks/Embed.php @@ -0,0 +1,76 @@ +rawContent = [ + 'url' => $url, + 'caption' => [ + [ + 'type' => 'text', + 'text' => [ + 'content' => $caption + ] + ] + ] + ]; + + $embed->fillContent(); + + return $embed; + } + + function __construct(array $responseData = null) + { + $this->type = "embed"; + parent::__construct($responseData); + } + + /** + * + */ + protected function fillFromRaw(): void + { + parent::fillFromRaw(); + $this->fillContent(); + } + + /** + * + */ + protected function fillContent(): void + { + $this->url = $this->rawContent['url']; + $this->caption = new RichText($this->rawContent['caption']); + $this->content = $this->url; + } + + public function getUrl() + { + return $this->url; + } + + public function getCaption() + { + return $this->caption; + } +} diff --git a/src/Entities/Blocks/File.php b/src/Entities/Blocks/File.php new file mode 100644 index 0000000..238d19b --- /dev/null +++ b/src/Entities/Blocks/File.php @@ -0,0 +1,30 @@ +type = "file"; + parent::__construct($responseData); + } + +} diff --git a/src/Entities/Blocks/Image.php b/src/Entities/Blocks/Image.php new file mode 100644 index 0000000..84ff24b --- /dev/null +++ b/src/Entities/Blocks/Image.php @@ -0,0 +1,30 @@ +type = "image"; + parent::__construct($responseData); + } + +} diff --git a/src/Entities/Blocks/Pdf.php b/src/Entities/Blocks/Pdf.php new file mode 100644 index 0000000..2846e01 --- /dev/null +++ b/src/Entities/Blocks/Pdf.php @@ -0,0 +1,30 @@ +type = "pdf"; + parent::__construct($responseData); + } + +} diff --git a/src/Entities/Blocks/Video.php b/src/Entities/Blocks/Video.php new file mode 100644 index 0000000..4fdbb73 --- /dev/null +++ b/src/Entities/Blocks/Video.php @@ -0,0 +1,30 @@ +type = "video"; + parent::__construct($responseData); + } + +} diff --git a/tests/EndpointBlocksTest.php b/tests/EndpointBlocksTest.php index 0b7cb18..24e0497 100644 --- a/tests/EndpointBlocksTest.php +++ b/tests/EndpointBlocksTest.php @@ -6,13 +6,18 @@ use Illuminate\Support\Facades\Http; use FiveamCode\LaravelNotionApi\Entities\Blocks\Block; use FiveamCode\LaravelNotionApi\Entities\Blocks\BulletedListItem; +use FiveamCode\LaravelNotionApi\Entities\Blocks\Embed; +use FiveamCode\LaravelNotionApi\Entities\Blocks\File; use FiveamCode\LaravelNotionApi\Entities\Blocks\HeadingOne; use FiveamCode\LaravelNotionApi\Entities\Blocks\HeadingThree; use FiveamCode\LaravelNotionApi\Entities\Blocks\HeadingTwo; +use FiveamCode\LaravelNotionApi\Entities\Blocks\Image; use FiveamCode\LaravelNotionApi\Entities\Blocks\NumberedListItem; use FiveamCode\LaravelNotionApi\Entities\Blocks\Paragraph; +use FiveamCode\LaravelNotionApi\Entities\Blocks\Pdf; use FiveamCode\LaravelNotionApi\Entities\Blocks\ToDo; use FiveamCode\LaravelNotionApi\Entities\Blocks\Toggle; +use FiveamCode\LaravelNotionApi\Entities\Blocks\Video; use FiveamCode\LaravelNotionApi\Exceptions\NotionException; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection; @@ -99,7 +104,7 @@ public function it_returns_block_collection_with_children_as_correct_instances() $blockChildrenCollection = $blockChildren->asCollection(); $this->assertContainsOnly(Block::class, $blockChildrenCollection); $this->assertIsIterable($blockChildrenCollection); - $this->assertCount(8, $blockChildrenCollection); + $this->assertCount(13, $blockChildrenCollection); # check paragraph $blockChild = $blockChildrenCollection[0]; @@ -156,6 +161,50 @@ public function it_returns_block_collection_with_children_as_correct_instances() $this->assertEquals('toggle', $blockChild->getType()); $this->assertFalse($blockChild->hasChildren()); $this->assertEquals('toggle_block', $blockChild->getContent()->getPlainText()); + + # check embed + $blockChild = $blockChildrenCollection[8]; + $this->assertInstanceOf(Embed::class, $blockChild); + $this->assertEquals('embed', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('Testcaption', $blockChild->getCaption()->getPlainText()); + $this->assertEquals('https://notion.so', $blockChild->getUrl()); + + # check image + $blockChild = $blockChildrenCollection[9]; + $this->assertInstanceOf(Image::class, $blockChild); + $this->assertEquals('image', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('test', $blockChild->getCaption()->getPlainText()); + $this->assertEquals('external', $blockChild->getHostingType()); + $this->assertEquals('https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb', $blockChild->getUrl()); + + # check file + $blockChild = $blockChildrenCollection[10]; + $this->assertInstanceOf(File::class, $blockChild); + $this->assertEquals('file', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('TestCaption', $blockChild->getCaption()->getPlainText()); + $this->assertEquals('external', $blockChild->getHostingType()); + $this->assertEquals('https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb', $blockChild->getUrl()); + + # check video + $blockChild = $blockChildrenCollection[11]; + $this->assertInstanceOf(Video::class, $blockChild); + $this->assertEquals('video', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('TestCaption', $blockChild->getCaption()->getPlainText()); + $this->assertEquals('external', $blockChild->getHostingType()); + $this->assertEquals('https://www.w3schools.com/html/mov_bbb.mp4', $blockChild->getUrl()); + + # check pdf + $blockChild = $blockChildrenCollection[12]; + $this->assertInstanceOf(Pdf::class, $blockChild); + $this->assertEquals('pdf', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('TestCaption', $blockChild->getCaption()->getPlainText()); + $this->assertEquals('external', $blockChild->getHostingType()); + $this->assertEquals('https://notion.so/testpdf.pdf', $blockChild->getUrl()); } /** @test */ @@ -202,6 +251,10 @@ public function it_returns_parent_block_in_which_new_blocks_have_been_successful $numberedListItem = NumberedListItem::create("New TextBlock"); $toDo = ToDo::create("New TextBlock"); $toggle = Toggle::create(["New TextBlock"]); + $embed = Embed::create("https://5amco.de", "Testcaption"); + $image = Image::create("https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb", "Testcaption"); + $video = Image::create("https://www.w3schools.com/html/mov_bbb.mp4", "TestCaption"); + $pdf = Image::create("https://notion.so/testpdf.pdf", "TestCaption"); $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($paragraph); $this->assertInstanceOf(Block::class, $parentBlock); @@ -227,8 +280,19 @@ public function it_returns_parent_block_in_which_new_blocks_have_been_successful $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($toggle); $this->assertInstanceOf(Block::class, $parentBlock); + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($embed); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($image); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($video); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($pdf); + $this->assertInstanceOf(Block::class, $parentBlock); - $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append([$paragraph, $bulletedListItem, $headingOne, $headingTwo, $headingThree, $numberedListItem, $toDo, $toggle]); + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append([$paragraph, $bulletedListItem, $headingOne, $headingTwo, $headingThree, $numberedListItem, $toDo, $toggle, $embed, $image, $video, $pdf]); $this->assertInstanceOf(Block::class, $parentBlock); } } diff --git a/tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json b/tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json index 54096f0..2c743f1 100644 --- a/tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json +++ b/tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json @@ -233,6 +233,173 @@ } ] } + }, + { + "object": "block", + "id": "1750f17f-d2bd-105a-bedc-c6b5808b98be", + "created_time": "2021-09-11T18:52:00.000Z", + "last_edited_time": "2021-09-11T19:01:00.000Z", + "has_children": false, + "archived": false, + "type": "embed", + "embed": { + "caption": [ + { + "type": "text", + "text": { + "content": "Testcaption", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "Testcaption", + "href": null + } + ], + "url": "https://notion.so" + } + }, + { + "object": "block", + "id": "1a14a61f-0d09-45c2-b97b-07b209474ecf", + "created_time": "2021-09-11T22:18:00.000Z", + "last_edited_time": "2021-09-11T22:21:00.000Z", + "has_children": false, + "archived": false, + "type": "image", + "image": { + "caption": [ + { + "type": "text", + "text": { + "content": "test", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "test", + "href": null + } + ], + "type": "external", + "external": { + "url": "https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb" + } + } + }, + { + "object": "block", + "id": "5e506978-6e1f-10c3-8374-63eaf27eaecc", + "created_time": "2021-09-11T22:38:00.000Z", + "last_edited_time": "2021-09-11T22:38:00.000Z", + "has_children": false, + "archived": false, + "type": "file", + "file": { + "caption": [ + { + "type": "text", + "text": { + "content": "TestCaption", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "TestCaption", + "href": null + } + ], + "type": "external", + "external": { + "url": "https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb" + } + } + }, + { + "object": "block", + "id": "3762ce35-e825-19e0-b024-4f5e172cdfe2", + "created_time": "2021-09-11T23:00:00.000Z", + "last_edited_time": "2021-09-11T23:00:00.000Z", + "has_children": false, + "archived": false, + "type": "video", + "video": { + "caption": [ + { + "type": "text", + "text": { + "content": "TestCaption", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "TestCaption", + "href": null + } + ], + "type": "external", + "external": { + "url": "https://www.w3schools.com/html/mov_bbb.mp4" + } + } + }, + { + "object": "block", + "id": "51a8e0ef-570a-4e14-bc03-16db98ad45db", + "created_time": "2021-09-11T22:58:00.000Z", + "last_edited_time": "2021-09-11T22:58:00.000Z", + "has_children": false, + "archived": false, + "type": "pdf", + "pdf": { + "caption": [ + { + "type": "text", + "text": { + "content": "TestCaption", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "TestCaption", + "href": null + } + ], + "type": "external", + "external": { + "url": "https://notion.so/testpdf.pdf" + } + } } ], "next_cursor": null,