Skip to content

Commit 8a53bb5

Browse files
committed
implemented page-creation and -update for current properties
- only properties can be set within page-creation and -update (so no block-addition/modification is currently included) - properties multi_select, select, number, title and (rich_)text are supported for creation and update - tests to check for HandlingException within create, and updateProperties (in Endpoint Pages) are commented out
1 parent 536ef86 commit 8a53bb5

File tree

13 files changed

+338
-36
lines changed

13 files changed

+338
-36
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: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,55 @@ 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

4561

4662
/**
4763
* @return array
4864
* @throws HandlingException
4965
*/
50-
public function updateProperties(): array
66+
public function updateProperties(Page $page): Page
5167
{
52-
//toDo
53-
throw new HandlingException('Not implemented');
68+
$postData = [];
69+
$properties = [];
70+
71+
foreach ($page->getProperties() as $property) {
72+
$properties[$property->getTitle()] = $property->getRawContent();
73+
}
74+
75+
$postData["properties"] = $properties;
76+
77+
78+
$response = $this
79+
->patch(
80+
$this->url(Endpoint::PAGES . "/" . $page->getId()),
81+
$postData
82+
)
83+
->json();
84+
85+
return new Page($response);
5486
}
55-
}
87+
}

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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
namespace FiveamCode\LaravelNotionApi\Entities;
44

55
use DateTime;
6+
use FiveamCode\LaravelNotionApi\Entities\Properties\MultiSelect;
7+
use FiveamCode\LaravelNotionApi\Entities\Properties\Number;
68
use Illuminate\Support\Arr;
79
use Illuminate\Support\Collection;
810
use FiveamCode\LaravelNotionApi\Entities\Properties\Property;
11+
use FiveamCode\LaravelNotionApi\Entities\Properties\Select;
12+
use FiveamCode\LaravelNotionApi\Entities\Properties\Text;
13+
use FiveamCode\LaravelNotionApi\Entities\Properties\Title;
914
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
1015

1116
/**
@@ -55,6 +60,19 @@ class Page extends Entity
5560
protected DateTime $lastEditedTime;
5661

5762

63+
/**
64+
* Page constructor.
65+
* @param array|null $responseData
66+
* @throws HandlingException
67+
* @throws NotionException
68+
*/
69+
public function __construct(array $responseData = null)
70+
{
71+
$this->properties = new Collection();
72+
parent::__construct($responseData);
73+
}
74+
75+
5876
/**
5977
* @param array $responseData
6078
* @throws HandlingException
@@ -126,6 +144,61 @@ private function fillTitle(): void
126144
}
127145
}
128146

147+
/**
148+
* @param $propertyTitle
149+
* @param $property
150+
*/
151+
public function setProperty(string $propertyTitle, Property $property): void{
152+
$property->setTitle($propertyTitle);
153+
$this->properties->add($property);
154+
155+
if($property instanceof Title){
156+
$this->title = $property->getPlainText();
157+
}
158+
}
159+
160+
/**
161+
* @param $propertyTitle
162+
* @param $number
163+
*/
164+
public function setNumber(string $propertyTitle, float $number) : void{
165+
$this->setProperty($propertyTitle, Number::instance($number));
166+
}
167+
168+
/**
169+
* @param $propertyTitle
170+
* @param $text
171+
*/
172+
public function setTitle(string $propertyTitle, string $text) : void{
173+
$this->setProperty($propertyTitle, Title::instance($text));
174+
}
175+
176+
/**
177+
* @param $propertyTitle
178+
* @param $text
179+
*/
180+
public function setText(string $propertyTitle, string $text) : void{
181+
$this->setProperty($propertyTitle, Text::instance($text));
182+
}
183+
184+
/**
185+
* @param $propertyTitle
186+
* @param $name
187+
*/
188+
public function setSelect(string $propertyTitle, string $name) : void{
189+
$this->setProperty($propertyTitle, Select::instance($name));
190+
}
191+
192+
/**
193+
* @param $propertyTitle
194+
* @param $names
195+
*/
196+
public function setMultiSelect(string $propertyTitle, array $names) : void{
197+
$this->setProperty($propertyTitle, MultiSelect::instance($names));
198+
}
199+
200+
201+
129202
/**
130203
* @return string
131204
*/

src/Entities/Properties/MultiSelect.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,33 @@
1212
*/
1313
class MultiSelect extends Property
1414
{
15+
/**
16+
* @param $names
17+
* @return MultiSelect
18+
*/
19+
public static function instance(array $names): MultiSelect
20+
{
21+
$multiSelectProperty = new MultiSelect();
22+
$multiSelectRawContent = [];
23+
$selectItemCollection = new Collection();
24+
25+
foreach($names as $name){
26+
$selectItem = new SelectItem();
27+
$selectItem->setName($name);
28+
$selectItemCollection->add($selectItem);
29+
array_push($multiSelectRawContent, [
30+
"name" => $selectItem->getName()
31+
]);
32+
}
33+
34+
$multiSelectProperty->content = $selectItemCollection;
35+
$multiSelectProperty->rawContent = [
36+
"multi_select" => $multiSelectRawContent
37+
];
38+
39+
return $multiSelectProperty;
40+
}
41+
1542
/**
1643
* @throws HandlingException
1744
*/

src/Entities/Properties/Number.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ class Number extends Property
1313
*/
1414
protected float $number = 0;
1515

16+
17+
public static function instance(float $number): Number
18+
{
19+
$numberProperty = new Number();
20+
$numberProperty->number = $number;
21+
$numberProperty->content = $number;
22+
23+
$numberProperty->rawContent = [
24+
"number" => $number
25+
];
26+
27+
return $numberProperty;
28+
}
29+
30+
1631
/**
1732
*
1833
*/

src/Entities/Properties/Property.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ class Property extends Entity
3434

3535
/**
3636
* Property constructor.
37-
* @param string $title
37+
* @param string|null $title
3838
* @param array $responseData
3939
* @throws HandlingException
4040
*/
41-
public function __construct(string $title, array $responseData)
41+
public function __construct(string $title = null)
4242
{
43-
$this->title = $title;
44-
$this->setResponseData($responseData);
43+
if($title != null) $this->title = $title;
4544
}
4645

4746

@@ -95,6 +94,11 @@ public function getTitle(): string
9594
return $this->title;
9695
}
9796

97+
public function setTitle($title): void
98+
{
99+
$this->title = $title;
100+
}
101+
98102
/**
99103
* @return string
100104
*/
@@ -111,6 +115,10 @@ public function getRawContent()
111115
return $this->rawContent;
112116
}
113117

118+
public function setRawContent($rawContent) : void{
119+
$this->rawContent = $rawContent;
120+
}
121+
114122
/**
115123
* @return mixed
116124
*/
@@ -119,6 +127,11 @@ public function getContent()
119127
return $this->rawContent;
120128
}
121129

130+
131+
public function setContent($content) : void{
132+
$this->content = $content;
133+
}
134+
122135
/**
123136
* @param $propertyKey
124137
* @param $rawContent
@@ -127,21 +140,24 @@ public function getContent()
127140
*/
128141
public static function fromResponse($propertyKey, $rawContent): Property
129142
{
143+
$property = null;
130144
if ($rawContent['type'] == 'multi_select') {
131-
return new MultiSelect($propertyKey, $rawContent);
145+
$property = new MultiSelect($propertyKey);
132146
} else if ($rawContent['type'] == 'select') {
133-
return new Select($propertyKey, $rawContent);
147+
$property = new Select($propertyKey);
134148
} else if ($rawContent['type'] == 'text') {
135-
return new Text($propertyKey, $rawContent);
149+
$property = new Text($propertyKey);
136150
} else if ($rawContent['type'] == 'created_by') {
137-
return new CreatedBy($propertyKey, $rawContent);
151+
$property = new CreatedBy($propertyKey);
138152
} else if ($rawContent['type'] == 'title') {
139-
return new Title($propertyKey, $rawContent);
153+
$property = new Title($propertyKey);
140154
} else if ($rawContent['type'] == 'number') {
141-
return new Number($propertyKey, $rawContent);
155+
$property = new Number($propertyKey);
156+
} else {
157+
$property = new Property($propertyKey);
142158
}
143159

144-
145-
return new Property($propertyKey, $rawContent);
160+
$property->setResponseData($rawContent);
161+
return $property;
146162
}
147163
}

src/Entities/Properties/Select.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@
1111
*/
1212
class Select extends Property
1313
{
14+
15+
/**
16+
* @param $name
17+
* @return Select
18+
*/
19+
public static function instance(string $name): Select
20+
{
21+
$selectProperty = new Select();
22+
23+
$selectItem = new SelectItem();
24+
$selectItem->setName($name);
25+
$selectProperty->content = $selectItem;
26+
27+
$selectProperty->rawContent = [
28+
"select" => [
29+
"name" => $selectItem->getName()
30+
]
31+
];
32+
33+
return $selectProperty;
34+
}
35+
1436
/**
1537
* @throws HandlingException
1638
*/

0 commit comments

Comments
 (0)