Skip to content

Commit 76a4298

Browse files
committed
add new blocks (file, image, video, pdf) + polish
- set up BaseFileBlock::class for file-related blocks (file, image, file, pdf) - implement Modifiable within embed and BaseFileBlock - mark caption as optional paramter in embed and BaseFileBlock with empty string as default - set up tests for all new blocks (creation and retreival)
1 parent dc294f7 commit 76a4298

File tree

9 files changed

+399
-7
lines changed

9 files changed

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

src/Entities/Blocks/Block.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,13 @@ public function getContent()
156156
/**
157157
* @return string
158158
*/
159-
public function asText() : string
159+
public function asText(): string
160160
{
161161
return $this->text;
162162
}
163163

164-
public function setContent($content){
164+
public function setContent($content)
165+
{
165166
$this->content = $content;
166167
}
167168

@@ -193,8 +194,12 @@ private static function mapTypeToClass(string $type): string
193194
case 'child_page':
194195
case 'paragraph':
195196
case 'to_do':
196-
case 'embed':
197197
case 'toggle':
198+
case 'embed':
199+
case 'image':
200+
case 'video':
201+
case 'file':
202+
case 'pdf':
198203
$class = str_replace('_', '', ucwords($type, '_'));
199204
return "FiveamCode\\LaravelNotionApi\\Entities\\Blocks\\" . $class;
200205
case 'heading_1':

src/Entities/Blocks/Embed.php

Lines changed: 3 additions & 2 deletions
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,12 +13,12 @@
1213
* Class Paragraph
1314
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
1415
*/
15-
class Embed extends Block
16+
class Embed extends Block implements Modifiable
1617
{
1718
private RichText $caption;
1819
private string $url = "";
1920

20-
public static function create(string $url, string $caption): Embed
21+
public static function create(string $url, string $caption = ""): Embed
2122
{
2223
$embed = new Embed();
2324

src/Entities/Blocks/File.php

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

src/Entities/Blocks/Image.php

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

src/Entities/Blocks/Pdf.php

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

src/Entities/Blocks/Video.php

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

tests/EndpointBlocksTest.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
88
use FiveamCode\LaravelNotionApi\Entities\Blocks\BulletedListItem;
99
use FiveamCode\LaravelNotionApi\Entities\Blocks\Embed;
10+
use FiveamCode\LaravelNotionApi\Entities\Blocks\File;
1011
use FiveamCode\LaravelNotionApi\Entities\Blocks\HeadingOne;
1112
use FiveamCode\LaravelNotionApi\Entities\Blocks\HeadingThree;
1213
use FiveamCode\LaravelNotionApi\Entities\Blocks\HeadingTwo;
14+
use FiveamCode\LaravelNotionApi\Entities\Blocks\Image;
1315
use FiveamCode\LaravelNotionApi\Entities\Blocks\NumberedListItem;
1416
use FiveamCode\LaravelNotionApi\Entities\Blocks\Paragraph;
17+
use FiveamCode\LaravelNotionApi\Entities\Blocks\Pdf;
1518
use FiveamCode\LaravelNotionApi\Entities\Blocks\ToDo;
1619
use FiveamCode\LaravelNotionApi\Entities\Blocks\Toggle;
20+
use FiveamCode\LaravelNotionApi\Entities\Blocks\Video;
1721
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
1822
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
1923
use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection;
@@ -100,7 +104,7 @@ public function it_returns_block_collection_with_children_as_correct_instances()
100104
$blockChildrenCollection = $blockChildren->asCollection();
101105
$this->assertContainsOnly(Block::class, $blockChildrenCollection);
102106
$this->assertIsIterable($blockChildrenCollection);
103-
$this->assertCount(9, $blockChildrenCollection);
107+
$this->assertCount(13, $blockChildrenCollection);
104108

105109
# check paragraph
106110
$blockChild = $blockChildrenCollection[0];
@@ -165,6 +169,42 @@ public function it_returns_block_collection_with_children_as_correct_instances()
165169
$this->assertFalse($blockChild->hasChildren());
166170
$this->assertEquals('Testcaption', $blockChild->getCaption()->getPlainText());
167171
$this->assertEquals('https://notion.so', $blockChild->getUrl());
172+
173+
# check image
174+
$blockChild = $blockChildrenCollection[9];
175+
$this->assertInstanceOf(Image::class, $blockChild);
176+
$this->assertEquals('image', $blockChild->getType());
177+
$this->assertFalse($blockChild->hasChildren());
178+
$this->assertEquals('test', $blockChild->getCaption()->getPlainText());
179+
$this->assertEquals('external', $blockChild->getHostingType());
180+
$this->assertEquals('https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb', $blockChild->getUrl());
181+
182+
# check file
183+
$blockChild = $blockChildrenCollection[10];
184+
$this->assertInstanceOf(File::class, $blockChild);
185+
$this->assertEquals('file', $blockChild->getType());
186+
$this->assertFalse($blockChild->hasChildren());
187+
$this->assertEquals('TestCaption', $blockChild->getCaption()->getPlainText());
188+
$this->assertEquals('external', $blockChild->getHostingType());
189+
$this->assertEquals('https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb', $blockChild->getUrl());
190+
191+
# check video
192+
$blockChild = $blockChildrenCollection[11];
193+
$this->assertInstanceOf(Video::class, $blockChild);
194+
$this->assertEquals('video', $blockChild->getType());
195+
$this->assertFalse($blockChild->hasChildren());
196+
$this->assertEquals('TestCaption', $blockChild->getCaption()->getPlainText());
197+
$this->assertEquals('external', $blockChild->getHostingType());
198+
$this->assertEquals('https://www.w3schools.com/html/mov_bbb.mp4', $blockChild->getUrl());
199+
200+
# check pdf
201+
$blockChild = $blockChildrenCollection[12];
202+
$this->assertInstanceOf(Pdf::class, $blockChild);
203+
$this->assertEquals('pdf', $blockChild->getType());
204+
$this->assertFalse($blockChild->hasChildren());
205+
$this->assertEquals('TestCaption', $blockChild->getCaption()->getPlainText());
206+
$this->assertEquals('external', $blockChild->getHostingType());
207+
$this->assertEquals('https://notion.so/testpdf.pdf', $blockChild->getUrl());
168208
}
169209

170210
/** @test */
@@ -212,6 +252,9 @@ public function it_returns_parent_block_in_which_new_blocks_have_been_successful
212252
$toDo = ToDo::create("New TextBlock");
213253
$toggle = Toggle::create(["New TextBlock"]);
214254
$embed = Embed::create("https://5amco.de", "Testcaption");
255+
$image = Image::create("https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb", "Testcaption");
256+
$video = Image::create("https://www.w3schools.com/html/mov_bbb.mp4", "TestCaption");
257+
$pdf = Image::create("https://notion.so/testpdf.pdf", "TestCaption");
215258

216259
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($paragraph);
217260
$this->assertInstanceOf(Block::class, $parentBlock);
@@ -240,8 +283,16 @@ public function it_returns_parent_block_in_which_new_blocks_have_been_successful
240283
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($embed);
241284
$this->assertInstanceOf(Block::class, $parentBlock);
242285

286+
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($image);
287+
$this->assertInstanceOf(Block::class, $parentBlock);
288+
289+
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($video);
290+
$this->assertInstanceOf(Block::class, $parentBlock);
291+
292+
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($pdf);
293+
$this->assertInstanceOf(Block::class, $parentBlock);
243294

244-
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append([$paragraph, $bulletedListItem, $headingOne, $headingTwo, $headingThree, $numberedListItem, $toDo, $toggle, $embed]);
295+
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append([$paragraph, $bulletedListItem, $headingOne, $headingTwo, $headingThree, $numberedListItem, $toDo, $toggle, $embed, $image, $video, $pdf]);
245296
$this->assertInstanceOf(Block::class, $parentBlock);
246297
}
247298
}

0 commit comments

Comments
 (0)