Skip to content

Commit 367cd32

Browse files
authored
Merge pull request 5am-code#19 from 5am-code/feature/blocks
implement notion block types as classes
2 parents 1463c6a + 062e07d commit 367cd32

15 files changed

+662
-4
lines changed

src/Entities/Blocks/Block.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ class Block extends Entity
2828
*/
2929
protected array $rawContent;
3030

31+
/**
32+
* @var mixed
33+
*/
34+
protected $content;
35+
36+
37+
/**
38+
* @var string
39+
*/
40+
protected string $text = "[warning: unsupported in notion api]";
41+
3142
/**
3243
* @var DateTime
3344
*/
@@ -54,7 +65,7 @@ protected function setResponseData(array $responseData): void
5465
/**
5566
*
5667
*/
57-
private function fillFromRaw(): void
68+
protected function fillFromRaw(): void
5869
{
5970
$this->fillId();
6071
$this->fillType();
@@ -133,4 +144,61 @@ public function getLastEditedTime(): DateTime
133144
{
134145
return $this->lastEditedTime;
135146
}
147+
148+
/**
149+
*
150+
*/
151+
public function getContent()
152+
{
153+
return $this->content;
154+
}
155+
156+
/**
157+
* @return string
158+
*/
159+
public function asText() : string
160+
{
161+
return $this->text;
162+
}
163+
164+
/**
165+
* @param $rawContent
166+
* @return Block
167+
* @throws HandlingException
168+
*/
169+
public static function fromResponse($rawContent): Block
170+
{
171+
$blockClass = self::mapTypeToClass($rawContent['type']);
172+
$block = new $blockClass($rawContent);
173+
return $block;
174+
}
175+
176+
/**
177+
* Maps the type of a block to the corresponding package class by converting the type name.
178+
*
179+
* @param string $type
180+
* @return string
181+
*/
182+
private static function mapTypeToClass(string $type): string
183+
{
184+
185+
switch ($type) {
186+
case 'bulleted_list_item':
187+
case 'numbered_list_item':
188+
case 'child_page':
189+
case 'paragraph':
190+
case 'to_do':
191+
case 'toggle':
192+
$class = str_replace('_', '', ucwords($type, '_'));
193+
return "FiveamCode\\LaravelNotionApi\\Entities\\Blocks\\" . $class;
194+
case 'heading_1':
195+
return HeadingOne::class;
196+
case 'heading_2':
197+
return HeadingTwo::class;
198+
case 'heading_3':
199+
return HeadingThree::class;
200+
default:
201+
return Block::class;
202+
}
203+
}
136204
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Exceptions\HandlingException;
9+
10+
/**
11+
* Class BulletedListItem
12+
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
13+
*/
14+
class BulletedListItem extends TextBlock
15+
{
16+
}

src/Entities/Blocks/ChildPage.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Exceptions\HandlingException;
9+
10+
/**
11+
* Class ChildPage
12+
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
13+
*/
14+
class ChildPage extends Block
15+
{
16+
/**
17+
*
18+
*/
19+
protected function fillFromRaw(): void
20+
{
21+
parent::fillFromRaw();
22+
$this->fillContent();
23+
}
24+
25+
/**
26+
*
27+
*/
28+
protected function fillContent() : void
29+
{
30+
$this->content = $this->rawContent['title'];
31+
$this->text = $this->getContent();
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function getContent(): string
38+
{
39+
return $this->content;
40+
}
41+
}

src/Entities/Blocks/HeadingOne.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Exceptions\HandlingException;
9+
10+
/**
11+
* Class HeadingOne
12+
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
13+
*/
14+
class HeadingOne extends TextBlock
15+
{
16+
}

src/Entities/Blocks/HeadingThree.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Exceptions\HandlingException;
9+
10+
/**
11+
* Class HeadingThree
12+
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
13+
*/
14+
class HeadingThree extends TextBlock
15+
{
16+
}

src/Entities/Blocks/HeadingTwo.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Exceptions\HandlingException;
9+
10+
/**
11+
* Class HeadingTwo
12+
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
13+
*/
14+
class HeadingTwo extends TextBlock
15+
{
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Exceptions\HandlingException;
9+
10+
/**
11+
* Class NumberedListItem
12+
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
13+
*/
14+
class NumberedListItem extends TextBlock
15+
{
16+
}

src/Entities/Blocks/Paragraph.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 Paragraph extends TextBlock
16+
{
17+
}

src/Entities/Blocks/TextBlock.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 TextBlock
13+
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
14+
*/
15+
class TextBlock extends Block
16+
{
17+
/**
18+
*
19+
*/
20+
protected function fillFromRaw(): void
21+
{
22+
parent::fillFromRaw();
23+
$this->fillContent();
24+
}
25+
26+
/**
27+
*
28+
*/
29+
protected function fillContent(): void
30+
{
31+
$this->content = new RichText($this->rawContent['text']);
32+
$this->text = $this->getContent()->getPlainText();
33+
}
34+
35+
/**
36+
* @return RichText
37+
*/
38+
public function getContent(): RichText
39+
{
40+
return $this->content;
41+
}
42+
}

src/Entities/Blocks/ToDo.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Exceptions\HandlingException;
9+
10+
/**
11+
* Class ToDo
12+
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
13+
*/
14+
class ToDo extends TextBlock
15+
{
16+
}

0 commit comments

Comments
 (0)