Skip to content

Commit 584a19d

Browse files
committed
add properties to Page, add PageCollection
> + add PageCollection as return-type within Database->query
1 parent ec20e17 commit 584a19d

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

src/Endpoints/Database.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

5+
use FiveamCode\LaravelNotionApi\Entities\PageCollection;
56
use Illuminate\Support\Collection;
67
use FiveamCode\LaravelNotionApi\Notion;
78
use FiveamCode\LaravelNotionApi\Query\Filter;
@@ -28,7 +29,7 @@ public function __construct(string $databaseId, Notion $notion)
2829
parent::__construct($notion);
2930
}
3031

31-
public function query(): array
32+
public function query(): Collection
3233
{
3334
if ($this->sorts->isNotEmpty())
3435
$postData["sorts"] = Sorting::sortQuery($this->sorts);
@@ -50,7 +51,9 @@ public function query(): array
5051
->json();
5152

5253
// toDo return Database Entity
53-
dd($response);
54+
// dd($response);
55+
$pageCollection = new PageCollection($response);
56+
return $pageCollection->getResults();
5457
}
5558

5659
public function filterBy(Collection $filter)

src/Entities/Database.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ private function fillFromRaw()
3434
$this->fillLastEditedTime();
3535
}
3636

37-
private function fillTitle()
37+
private function fillTitle() : void
3838
{
3939
if (Arr::exists($this->responseData, 'title') && is_array($this->responseData['title'])) {
4040
$this->title = Arr::first($this->responseData['title'], null, ['plain_text' => ''])['plain_text'];
4141
$this->rawTitle = $this->responseData['title'];
4242
}
4343
}
4444

45-
private function fillProperties()
45+
private function fillProperties() : void
4646
{
4747
if (Arr::exists($this->responseData, 'properties')) {
4848
$this->rawProperties = $this->responseData['properties'];
@@ -80,7 +80,6 @@ public function getCreatedTime(): DateTime
8080
return $this->createdTime;
8181
}
8282

83-
8483
public function getLastEditedTime(): DateTime
8584
{
8685
return $this->lastEditedTime;

src/Entities/Page.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,64 @@
22

33
namespace FiveamCode\LaravelNotionApi\Entities;
44

5+
use DateTime;
56
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
67
use FiveamCode\LaravelNotionApi\Notion;
8+
use Illuminate\Support\Arr;
79

810
class Page extends Entity
911
{
12+
protected array $rawProperties = [];
13+
protected DateTime $createdTime;
14+
protected DateTime $lastEditedTime;
15+
16+
1017
protected function setResponseData(array $responseData): void
1118
{
1219
parent::setResponseData($responseData);
1320
if ($responseData['object'] !== 'page') throw WrapperException::instance("invalid json-array: the given object is not a page");
21+
$this->fillFromRaw();
22+
}
23+
24+
private function fillFromRaw(): void
25+
{
26+
$this->fillId();
27+
$this->fillProperties();
28+
$this->fillCreatedTime();
29+
$this->fillLastEditedTime();
30+
}
31+
32+
private function fillProperties() : void
33+
{
34+
if (Arr::exists($this->responseData, 'properties')) {
35+
$this->rawProperties = $this->responseData['properties'];
36+
}
37+
}
38+
39+
40+
public function getProperties()
41+
{
42+
//TODO: return collection of property-entities (id, type, title)
43+
throw new \Exception("not implemented yet");
44+
}
45+
46+
public function getRawProperties(): array
47+
{
48+
return $this->rawProperties;
49+
}
50+
51+
public function getPropertyNames(): array
52+
{
53+
return array_keys($this->rawProperties);
54+
}
55+
56+
public function getCreatedTime(): DateTime
57+
{
58+
return $this->createdTime;
59+
}
60+
61+
public function getLastEditedTime(): DateTime
62+
{
63+
return $this->lastEditedTime;
1464
}
1565
}

src/Entities/PageCollection.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities;
4+
5+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
6+
use FiveamCode\LaravelNotionApi\Notion;
7+
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Collection;
9+
10+
11+
class PageCollection extends EntityCollection
12+
{
13+
14+
protected function setResponseData(array $responseData): void
15+
{
16+
parent::setResponseData($responseData);
17+
$this->collectChildren();
18+
}
19+
20+
protected function collectChildren()
21+
{
22+
$this->collection = new Collection();
23+
foreach ($this->rawResults as $blockChild) {
24+
$this->collection->add(new Page($blockChild));
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)