Skip to content
This repository was archived by the owner on Jun 21, 2022. It is now read-only.

Commit f35d1c9

Browse files
Added logic for blocks & rich content
1 parent 0144602 commit f35d1c9

File tree

9 files changed

+235
-28
lines changed

9 files changed

+235
-28
lines changed

src/Notion/BlockBase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,16 @@ class BlockBase
1313
public $last_edited_time = '';
1414

1515
public $has_children = false;
16+
17+
public $typeConfiguration = [];
18+
19+
public function get(): array
20+
{
21+
return [
22+
'object' => $this->object,
23+
'id' => $this->id,
24+
'type' => $this->type,
25+
$this->type => $this->typeConfiguration,
26+
];
27+
}
1628
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php namespace Notion\Blocks;
2+
3+
use Notion\RichText;
4+
use Notion\BlockBase;
5+
6+
class Heading1Block extends BlockBase
7+
{
8+
public $type = 'heading_1';
9+
10+
public function __construct(RichText $richText)
11+
{
12+
$this->typeConfiguration = [
13+
'text' => [$richText->get()],
14+
];
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php namespace Notion\Blocks;
2+
3+
use Notion\RichText;
4+
use Notion\BlockBase;
5+
6+
class Heading2Block extends BlockBase
7+
{
8+
public $type = 'heading_2';
9+
10+
public function __construct(RichText $richText)
11+
{
12+
$this->typeConfiguration = [
13+
'text' => [$richText->get()],
14+
];
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php namespace Notion\Blocks;
2+
3+
use Notion\RichText;
4+
use Notion\BlockBase;
5+
6+
class Heading3Block extends BlockBase
7+
{
8+
public $type = 'heading_3';
9+
10+
public function __construct(RichText $richText)
11+
{
12+
$this->typeConfiguration = [
13+
'text' => [$richText->get()],
14+
];
15+
}
16+
}
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
<?php namespace Codecycler\Notion\Notion\Blocks;
1+
<?php namespace Notion\Blocks;
22

3-
use Codecycler\Notion\BlockBase;
4-
use Codecycler\Notion\RichText;
3+
use Notion\BlockBase;
4+
use Notion\RichText;
55

66
class ParagraphBlock extends BlockBase
77
{
88
public $type = 'paragraph';
99

10-
public $text = [];
11-
12-
public $children = [];
13-
14-
public function __construct()
10+
public function __construct(RichText $richText)
1511
{
16-
$this->text = new RichText();
12+
$this->typeConfiguration = [
13+
'text' => [$richText->get()],
14+
//'children' => [], // Todo: Children for paragraph block
15+
];
1716
}
1817
}

src/Notion/Blocks/TodoBlock.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php namespace Notion\Blocks;
2+
3+
use Notion\RichText;
4+
use Notion\BlockBase;
5+
6+
class TodoBlock extends BlockBase
7+
{
8+
public $type = 'to_do';
9+
10+
public function __construct(RichText $richText, $checked = false)
11+
{
12+
$this->typeConfiguration = [
13+
'text' => [$richText->get()],
14+
'checked' => $checked,
15+
];
16+
}
17+
}

src/Notion/ObjectBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected function createNewProperty($label, $property)
9696
case "phone_number":
9797
return new PhoneNumber($label, $property);
9898
case "rich_text":
99-
return new RichText($label, $property);
99+
//return new RichText($label, $property);
100100
case "rollup":
101101
return new Rollup($label, $property);
102102
case "select":

src/Notion/Objects/Page.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Notion\Objects;
22

3+
use Notion\RichText;
34
use Notion\ObjectBase;
45

56
class Page extends ObjectBase
@@ -20,23 +21,9 @@ public function setParent($type, $id): self
2021
}
2122

2223
// TODO: Implement rich text logic
23-
public function addChild($type, $text, $extra = null): self
24+
public function addBlock($block): self
2425
{
25-
$this->children[] = [
26-
'object' => 'block',
27-
'type' => $type,
28-
$type => [
29-
'text' => [
30-
[
31-
'text' => [
32-
'type' => 'text',
33-
'content' => $text,
34-
],
35-
]
36-
],
37-
],
38-
];
39-
26+
$this->children[] = $block;
4027
return $this;
4128
}
4229

@@ -58,7 +45,11 @@ public function prepareForRequest()
5845
}
5946

6047
if (count($this->children) > 0) {
61-
$data['children'] = $this->children;
48+
$data['children'] = [];
49+
50+
foreach ($this->children as $child) {
51+
$data['children'][] = $child->get();
52+
}
6253
}
6354

6455
return $data;

src/Notion/RichText.php

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,145 @@
11
<?php namespace Notion;
22

3-
class RichText extends PropertyBase
3+
class RichText
44
{
5+
public $plain_text;
6+
7+
public $href;
8+
9+
public $annotations = [
10+
'color' => 'default',
11+
];
12+
13+
public $type;
14+
15+
public $typeConfiguration = [];
16+
17+
public static $colorOptions = [
18+
'default',
19+
'gray',
20+
'brown',
21+
'orange',
22+
'yellow',
23+
'green',
24+
'blue',
25+
'purple',
26+
'pink',
27+
'red',
28+
'gray_background',
29+
'brown_background',
30+
'orange_background',
31+
'yellow_background',
32+
'green_background',
33+
'blue_background',
34+
'purple_background',
35+
'pink_background',
36+
'red_background',
37+
];
38+
39+
public function __construct($config = null)
40+
{
41+
$this->init($config);
42+
}
43+
44+
public function init($config)
45+
{
46+
}
47+
48+
public function get()
49+
{
50+
return [
51+
'annotations' => $this->annotations,
52+
$this->type => $this->typeConfiguration,
53+
];
54+
}
55+
56+
public function text($content, $link = null): self
57+
{
58+
$this->type = 'text';
59+
$this->typeConfiguration['content'] = $content;
60+
$this->typeConfiguration['type'] = 'text';
61+
62+
if ($link) {
63+
$this->typeConfiguration['link'] = [
64+
'type' => 'url',
65+
'url' => $link,
66+
];
67+
}
68+
69+
return $this;
70+
}
71+
72+
public function mention($type, $config = null): self
73+
{
74+
$this->type = 'mention';
75+
$this->typeConfiguration['type'] = $type;
76+
77+
if ($type === 'user') {
78+
$this->typeConfiguration['user'] = [
79+
'object' => 'user',
80+
'id' => $config,
81+
];
82+
}
83+
84+
if ($type === 'page') {
85+
$this->typeConfiguration['page'] = [
86+
'id' => $config,
87+
];
88+
}
89+
90+
if ($type === 'database') {
91+
$this->typeConfiguration['database'] = [
92+
'id' => $config,
93+
];
94+
}
95+
96+
if ($type === 'date') {
97+
// Todo: Handle date
98+
}
99+
100+
return $this;
101+
}
102+
103+
public function equation($expression): self
104+
{
105+
$this->type = 'equation';
106+
$this->typeConfiguration['expression'] = $expression;
107+
return $this;
108+
}
109+
110+
public function bold($setting = true): self
111+
{
112+
$this->annotations['bold'] = $setting;
113+
return $this;
114+
}
115+
116+
public function italic($setting = true): self
117+
{
118+
$this->annotations['italic'] = $setting;
119+
return $this;
120+
}
121+
122+
public function strikethrough($setting = true): self
123+
{
124+
$this->annotations['strikethrough'] = $setting;
125+
return $this;
126+
}
127+
128+
public function underline($setting = true): self
129+
{
130+
$this->annotations['underline'] = $setting;
131+
return $this;
132+
}
133+
134+
public function code($setting = true): self
135+
{
136+
$this->annotations['code'] = $setting;
137+
return $this;
138+
}
139+
140+
public function color($setting = 'default'): self
141+
{
142+
$this->annotations['color'] = $setting;
143+
return $this;
144+
}
5145
}

0 commit comments

Comments
 (0)