Skip to content

Commit b5b978c

Browse files
authored
Merge pull request #160 from 5am-code/dev
Release v1.1.0 with new features and minor fixes
2 parents 7e66280 + 6b31bcd commit b5b978c

File tree

63 files changed

+3573
-103
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+3573
-103
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
vendor
33
.phpunit.result.cache
44
coverage/
5-
.phpunit.cache/
5+
.phpunit.cache/
6+
.env*
7+
coverage-report

composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@
4949
},
5050
"scripts": {
5151
"test": "vendor/bin/pest",
52-
"test-coverage": "vendor/bin/pest --coverage-html coverage"
52+
"test-coverage": "phpdbg -qrr ./vendor/bin/pest --coverage-html ./coverage-report"
5353
},
5454
"config": {
55-
"sort-packages": true
55+
"sort-packages": true,
56+
"allow-plugins": {
57+
"pestphp/pest-plugin": true
58+
}
5659
},
5760
"extra": {
5861
"laravel": {

src/Builder/DatabaseBuilder.php

+245
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Builder;
4+
5+
use FiveamCode\LaravelNotionApi\Endpoints\Databases;
6+
use FiveamCode\LaravelNotionApi\Entities\Database;
7+
use Illuminate\Support\Collection;
8+
9+
/**
10+
* Class DatabaseBuilder.
11+
*/
12+
class DatabaseBuilder
13+
{
14+
/**
15+
* @var array
16+
*/
17+
private array $payload;
18+
19+
/**
20+
* @var Databases
21+
*/
22+
private Databases $databasesEndpoint;
23+
24+
/**
25+
* DatabaseBuilder constructor.
26+
*
27+
* @param Databases $databasesEndpoint
28+
*/
29+
public function __construct(Databases $databasesEndpoint)
30+
{
31+
$this->databasesEndpoint = $databasesEndpoint;
32+
$this->payload = [
33+
'is_inline' => false,
34+
'parent' => [],
35+
'title' => [
36+
[
37+
'text' => [
38+
'content' => '',
39+
],
40+
],
41+
],
42+
'properties' => [],
43+
];
44+
}
45+
46+
/**
47+
* Creates database within given page.
48+
*
49+
* @param string $pageId
50+
* @return Database
51+
*/
52+
public function createInPage(string $pageId): Database
53+
{
54+
$this->payload['parent'] = [
55+
'type' => 'page_id',
56+
'page_id' => $pageId,
57+
];
58+
59+
if ($this->payload['properties'] === []) {
60+
$this->addTitle();
61+
}
62+
63+
return $this->databasesEndpoint->create($this->payload());
64+
}
65+
66+
/**
67+
* Sets the title for the database creation.
68+
*
69+
* @param string $title
70+
* @return DatabaseBuilder
71+
*/
72+
public function title(string $title): DatabaseBuilder
73+
{
74+
$this->payload['title'] = [
75+
[
76+
'text' => [
77+
'content' => $title,
78+
],
79+
],
80+
];
81+
82+
return $this;
83+
}
84+
85+
/**
86+
* Sets the description for the database creation.
87+
*
88+
* @param string $description
89+
* @return DatabaseBuilder
90+
*/
91+
public function description(string $description): DatabaseBuilder
92+
{
93+
$this->payload['description'] = [
94+
[
95+
'text' => [
96+
'content' => $description,
97+
],
98+
],
99+
];
100+
101+
return $this;
102+
}
103+
104+
/**
105+
* Sets the created database as inline (currently not supported).
106+
*
107+
* @todo increase Notion API Version, to make this work
108+
*
109+
* @return DatabaseBuilder
110+
*/
111+
public function inline(): DatabaseBuilder
112+
{
113+
$this->payload['is_inline'] = true;
114+
115+
return $this;
116+
}
117+
118+
/**
119+
* Sets the icon for the database creation.
120+
*
121+
* @param string $icon
122+
* @return DatabaseBuilder
123+
*/
124+
public function iconEmoji(string $icon): DatabaseBuilder
125+
{
126+
$this->payload['icon'] = [
127+
'type' => 'emoji',
128+
'emoji' => $icon,
129+
];
130+
131+
return $this;
132+
}
133+
134+
/**
135+
* Sets the icon for the database creation.
136+
*
137+
* @param string $url
138+
* @return DatabaseBuilder
139+
*/
140+
public function iconExternal(string $url): DatabaseBuilder
141+
{
142+
$this->payload['icon'] = [
143+
'type' => 'external',
144+
'external' => [
145+
'url' => $url,
146+
],
147+
];
148+
149+
return $this;
150+
}
151+
152+
/**
153+
* Sets the cover for the database creation.
154+
*
155+
* @param string $url
156+
* @return DatabaseBuilder
157+
*/
158+
public function coverExternal(string $url): DatabaseBuilder
159+
{
160+
$this->payload['cover'] = [
161+
'type' => 'external',
162+
'external' => [
163+
'url' => $url,
164+
],
165+
];
166+
167+
return $this;
168+
}
169+
170+
/**
171+
* Adds the property `title` database creation.
172+
*
173+
* @param string $name
174+
* @return DatabaseBuilder
175+
*/
176+
public function addTitle(string $name = 'Name')
177+
{
178+
$this->add(PropertyBuilder::title($name));
179+
180+
return $this;
181+
}
182+
183+
/**
184+
* Adds one or multiple properties to the database creation.
185+
*
186+
* @param PropertyBuilder|Collection|DatabaseSchemeBuilder $properties
187+
* @return DatabaseBuilder
188+
*/
189+
public function add(PropertyBuilder|Collection|DatabaseSchemeBuilder $properties): DatabaseBuilder
190+
{
191+
if ($properties instanceof PropertyBuilder) {
192+
$properties = collect([$properties]);
193+
}
194+
195+
if ($properties instanceof DatabaseSchemeBuilder) {
196+
$properties = $properties->getProperties();
197+
}
198+
199+
$properties->each(function (PropertyBuilder $property) {
200+
$this->payload['properties'][$property->getName()] = $property->payload();
201+
});
202+
203+
return $this;
204+
}
205+
206+
/**
207+
* Adds multiple properties to the database creation, similar to a Laravel migration.
208+
*
209+
* @param callable $callback
210+
* @return DatabaseBuilder
211+
*/
212+
public function scheme(callable $callback): DatabaseBuilder
213+
{
214+
$builder = new DatabaseSchemeBuilder();
215+
$callback($builder);
216+
217+
return $this->add($builder);
218+
}
219+
220+
/**
221+
* Adds a raw property to the database creation.
222+
*
223+
* @param string $title
224+
* @param string $propertyType
225+
* @param array|null $content
226+
* @return DatabaseBuilder
227+
*/
228+
public function addRaw(string $title, string $propertyType, ?array $content = null): DatabaseBuilder
229+
{
230+
$this->payload['properties'][$title] = [];
231+
$this->payload['properties'][$title][$propertyType] = $content ?? new \stdClass();
232+
233+
return $this;
234+
}
235+
236+
/**
237+
* Returns the payload for the database creation.
238+
*
239+
* @return array
240+
*/
241+
public function payload(): array
242+
{
243+
return $this->payload;
244+
}
245+
}

0 commit comments

Comments
 (0)