Skip to content

Commit 23fe48d

Browse files
authored
Merge pull request #31 from 5am-code/feature/new-blocks
Feature/new blocks
2 parents 941dec8 + 76a4298 commit 23fe48d

File tree

10 files changed

+523
-6
lines changed

10 files changed

+523
-6
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: 10 additions & 4 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()];
@@ -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

@@ -194,6 +195,11 @@ private static function mapTypeToClass(string $type): string
194195
case 'paragraph':
195196
case 'to_do':
196197
case 'toggle':
198+
case 'embed':
199+
case 'image':
200+
case 'video':
201+
case 'file':
202+
case 'pdf':
197203
$class = str_replace('_', '', ucwords($type, '_'));
198204
return "FiveamCode\\LaravelNotionApi\\Entities\\Blocks\\" . $class;
199205
case 'heading_1':

src/Entities/Blocks/ChildPage.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
*/
1414
class ChildPage extends Block
1515
{
16+
function __construct(array $responseData = null){
17+
$this->type = "child_page";
18+
parent::__construct($responseData);
19+
}
20+
1621
/**
1722
*
1823
*/

src/Entities/Blocks/Embed.php

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

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+
}

0 commit comments

Comments
 (0)