Skip to content

Commit c1319a2

Browse files
authored
Merge pull request #17 from 5am-code/feature/create-page
create/update page + integration for all properties of notion-pages
2 parents f0775d5 + 5222889 commit c1319a2

32 files changed

+1662
-60
lines changed

src/Endpoints/Endpoint.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ protected function post(string $url, array $body): Response
116116
return $response;
117117
}
118118

119+
/**
120+
* @param string $url
121+
* @param array $body
122+
* @return Response
123+
* @throws HandlingException
124+
* @throws NotionException
125+
*/
126+
protected function patch(string $url, array $body): Response
127+
{
128+
$response = $this->notion->getConnection()->patch($url, $body);
129+
130+
if ($response->failed())
131+
throw NotionException::fromResponse($response);
132+
133+
$this->response = $response;
134+
return $response;
135+
}
136+
119137

120138
/**
121139
* @return string

src/Endpoints/Pages.php

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,81 @@ public function find(string $pageId): Page
3333
}
3434

3535
/**
36-
* @return array
37-
* @throws HandlingException
36+
* @return Page
3837
*/
39-
public function create(): array
38+
public function createInDatabase(string $parentId, Page $page): Page
4039
{
41-
//toDo
42-
throw new HandlingException( 'Not implemented');
40+
$postData = [];
41+
$properties = [];
42+
43+
foreach ($page->getProperties() as $property) {
44+
$properties[$property->getTitle()] = $property->getRawContent();
45+
}
46+
47+
$postData["parent"] = ["database_id" => $parentId];
48+
$postData["properties"] = $properties;
49+
50+
51+
$response = $this
52+
->post(
53+
$this->url(Endpoint::PAGES),
54+
$postData
55+
)
56+
->json();
57+
58+
return new Page($response);
4359
}
4460

61+
/**
62+
* @return Page
63+
*/
64+
public function createInPage(string $parentId, Page $page): Page
65+
{
66+
$postData = [];
67+
$properties = [];
68+
69+
foreach ($page->getProperties() as $property) {
70+
$properties[$property->getTitle()] = $property->getRawContent();
71+
}
72+
73+
$postData["parent"] = ["page_id" => $parentId];
74+
$postData["properties"] = $properties;
75+
76+
77+
$response = $this
78+
->post(
79+
$this->url(Endpoint::PAGES),
80+
$postData
81+
)
82+
->json();
83+
84+
return new Page($response);
85+
}
86+
87+
4588

4689
/**
4790
* @return array
4891
* @throws HandlingException
4992
*/
50-
public function updateProperties(): array
93+
public function update(Page $page): Page
5194
{
52-
//toDo
53-
throw new HandlingException('Not implemented');
95+
$postData = [];
96+
$properties = [];
97+
98+
foreach ($page->getProperties() as $property) {
99+
$properties[$property->getTitle()] = $property->getRawContent();
100+
}
101+
102+
$postData["properties"] = $properties;
103+
104+
$response = $this
105+
->patch(
106+
$this->url(Endpoint::PAGES . "/" . $page->getId()),
107+
$postData
108+
)
109+
->json();
110+
111+
return new Page($response);
54112
}
55-
}
113+
}

src/Entities/Entity.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Entity implements JsonSerializable
3333
*/
3434
public function __construct(array $responseData = null)
3535
{
36-
$this->setResponseData($responseData);
36+
if($responseData != null) $this->setResponseData($responseData);
3737
}
3838

3939
/**
@@ -98,6 +98,13 @@ public function getId(): string
9898
return $this->id;
9999
}
100100

101+
/**
102+
*
103+
*/
104+
public function setId($id): void{
105+
$this->id = $id;
106+
}
107+
101108
/**
102109
* @return array
103110
*/

src/Entities/Page.php

Lines changed: 193 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@
33
namespace FiveamCode\LaravelNotionApi\Entities;
44

55
use DateTime;
6+
use FiveamCode\LaravelNotionApi\Entities\Properties\Checkbox;
7+
use FiveamCode\LaravelNotionApi\Entities\Properties\Date;
8+
use FiveamCode\LaravelNotionApi\Entities\Properties\Email;
9+
use FiveamCode\LaravelNotionApi\Entities\Properties\MultiSelect;
10+
use FiveamCode\LaravelNotionApi\Entities\Properties\Number;
11+
use FiveamCode\LaravelNotionApi\Entities\Properties\People;
12+
use FiveamCode\LaravelNotionApi\Entities\Properties\PhoneNumber;
613
use Illuminate\Support\Arr;
714
use Illuminate\Support\Collection;
815
use FiveamCode\LaravelNotionApi\Entities\Properties\Property;
16+
use FiveamCode\LaravelNotionApi\Entities\Properties\Relation;
17+
use FiveamCode\LaravelNotionApi\Entities\Properties\Select;
18+
use FiveamCode\LaravelNotionApi\Entities\Properties\Text;
19+
use FiveamCode\LaravelNotionApi\Entities\Properties\Title;
20+
use FiveamCode\LaravelNotionApi\Entities\Properties\Url;
921
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
1022

1123
/**
@@ -55,6 +67,19 @@ class Page extends Entity
5567
protected DateTime $lastEditedTime;
5668

5769

70+
/**
71+
* Page constructor.
72+
* @param array|null $responseData
73+
* @throws HandlingException
74+
* @throws NotionException
75+
*/
76+
public function __construct(array $responseData = null)
77+
{
78+
$this->properties = new Collection();
79+
parent::__construct($responseData);
80+
}
81+
82+
5883
/**
5984
* @param array $responseData
6085
* @throws HandlingException
@@ -72,10 +97,10 @@ protected function setResponseData(array $responseData): void
7297
*/
7398
private function fillFromRaw(): void
7499
{
75-
$this->fillId();
100+
$this->fillId();
76101
$this->fillObjectType();
77102
$this->fillProperties();
78-
$this->fillTitle(); //!Warning: call after 'fillProperties', since title is included within properties
103+
$this->fillTitle(); // This has to be called after fillProperties(), since title is provided by properties
79104
$this->fillCreatedTime();
80105
$this->fillLastEditedTime();
81106
}
@@ -126,6 +151,171 @@ private function fillTitle(): void
126151
}
127152
}
128153

154+
/**
155+
* @param $propertyTitle
156+
* @param $property
157+
* @return Page
158+
*/
159+
public function set(string $propertyKey, Property $property): Page
160+
{
161+
$property->setTitle($propertyKey);
162+
$this->properties->add($property);
163+
$this->propertyMap[$propertyKey] = $property;
164+
165+
if ($property instanceof Title) {
166+
$this->title = $property->getPlainText();
167+
}
168+
169+
return $this;
170+
}
171+
172+
/**
173+
* @param $propertyTitle
174+
* @param $number
175+
* @return Page
176+
*/
177+
public function setNumber(string $propertyTitle, float $number): Page
178+
{
179+
$this->set($propertyTitle, Number::value($number));
180+
181+
return $this;
182+
}
183+
184+
/**
185+
* @param $propertyTitle
186+
* @param $text
187+
* @return Page
188+
*/
189+
public function setTitle(string $propertyTitle, string $text): Page
190+
{
191+
$this->set($propertyTitle, Title::value($text));
192+
193+
return $this;
194+
}
195+
196+
/**
197+
* @param $propertyTitle
198+
* @param $text
199+
* @return Page
200+
*/
201+
public function setText(string $propertyTitle, string $text): Page
202+
{
203+
$this->set($propertyTitle, Text::value($text));
204+
205+
return $this;
206+
}
207+
208+
/**
209+
* @param $propertyTitle
210+
* @param $name
211+
* @return Page
212+
*/
213+
public function setSelect(string $propertyTitle, string $name): Page
214+
{
215+
$this->set($propertyTitle, Select::value($name));
216+
217+
return $this;
218+
}
219+
220+
/**
221+
* @param $propertyTitle
222+
* @param $url
223+
* @return Page
224+
*/
225+
public function setUrl(string $propertyTitle, string $url): Page
226+
{
227+
$this->set($propertyTitle, Url::value($url));
228+
229+
return $this;
230+
}
231+
232+
/**
233+
* @param $propertyTitle
234+
* @param $phoneNumber
235+
* @return Page
236+
*/
237+
public function setPhoneNumber(string $propertyTitle, string $phoneNumber): Page
238+
{
239+
$this->set($propertyTitle, PhoneNumber::value($phoneNumber));
240+
241+
return $this;
242+
}
243+
244+
/**
245+
* @param $propertyTitle
246+
* @param $email
247+
* @return Page
248+
*/
249+
public function setEmail(string $propertyTitle, string $email): Page
250+
{
251+
$this->set($propertyTitle, Email::value($email));
252+
253+
return $this;
254+
}
255+
256+
/**
257+
* @param $propertyTitle
258+
* @param $names
259+
* @return Page
260+
*/
261+
public function setMultiSelect(string $propertyTitle, array $names): Page
262+
{
263+
$this->set($propertyTitle, MultiSelect::value($names));
264+
265+
return $this;
266+
}
267+
268+
/**
269+
* @param $propertyTitle
270+
* @param $checked
271+
* @return Page
272+
*/
273+
public function setCheckbox(string $propertyTitle, bool $checked): Page
274+
{
275+
$this->set($propertyTitle, Checkbox::value($checked));
276+
277+
return $this;
278+
}
279+
280+
281+
/**
282+
* @param $propertyTitle
283+
* @param $start
284+
* @param $end
285+
* @return Page
286+
*/
287+
public function setDate(string $propertyTitle, DateTime $start, ?DateTime $end = null): Page
288+
{
289+
$this->set($propertyTitle, Date::value($start, $end));
290+
291+
return $this;
292+
}
293+
294+
/**
295+
* @param $propertyTitle
296+
* @param $relationIds
297+
* @return Page
298+
*/
299+
public function setRelation(string $propertyTitle, array $relationIds): Page
300+
{
301+
$this->set($propertyTitle, Relation::value($relationIds));
302+
303+
return $this;
304+
}
305+
306+
/**
307+
* @param $propertyTitle
308+
* @param $userIds
309+
* @return Page
310+
*/
311+
public function setPeople(string $propertyTitle, array $userIds): Page
312+
{
313+
$this->set($propertyTitle, People::value($userIds));
314+
315+
return $this;
316+
}
317+
318+
129319
/**
130320
* @return string
131321
*/
@@ -148,7 +338,7 @@ public function getProperties(): Collection
148338
*/
149339
public function getProperty(string $propertyKey): ?Property
150340
{
151-
if(!isset($this->propertyMap[$propertyKey])){
341+
if (!isset($this->propertyMap[$propertyKey])) {
152342
return null;
153343
}
154344
return $this->propertyMap[$propertyKey];

0 commit comments

Comments
 (0)