Skip to content

Commit d7c11de

Browse files
committed
small refactoring for collections and retrieve properties within block
1 parent 958e205 commit d7c11de

File tree

5 files changed

+120
-30
lines changed

5 files changed

+120
-30
lines changed

src/Endpoints/Block.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,37 @@ class Block extends Endpoint
1111
{
1212
private string $blockId;
1313

14-
public function __construct(Notion $notion, string $blockId){
14+
public function __construct(Notion $notion, string $blockId)
15+
{
1516
parent::__construct($notion);
1617
$this->blockId = $blockId;
1718
}
1819

20+
/**
21+
* Retrieve block children (as raw json-data)
22+
* url: https://api.notion.com/{version}/blocks/{block_id}/children
23+
* notion-api-docs: https://developers.notion.com/reference/get-block-children
24+
*
25+
* @return BlockCollection
26+
*/
27+
public function children(): Collection
28+
{
29+
return $this->collectChildren()->getResults();
30+
}
31+
1932
/**
2033
* Retrieve block children
2134
* url: https://api.notion.com/{version}/blocks/{block_id}/children
2235
* notion-api-docs: https://developers.notion.com/reference/get-block-children
2336
*
2437
* @return BlockCollection
2538
*/
26-
public function children(): BlockCollection
39+
public function childrenRaw(): Collection
40+
{
41+
return $this->collectChildren()->getRawResults();
42+
}
43+
44+
private function collectChildren(): BlockCollection
2745
{
2846
$response = $this->get(
2947
$this->url(Endpoint::BLOCKS . "/" . $this->blockId . "/children" . "?{$this->buildPaginationQuery()}")
@@ -34,7 +52,6 @@ public function children(): BlockCollection
3452

3553

3654
$blockCollection = new BlockCollection($response->json());
37-
3855
return $blockCollection;
3956
}
4057

src/Endpoints/Databases.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
88
use FiveamCode\LaravelNotionApi\Notion;
99
use FiveamCode\LaravelNotionApi\Query\StartCursor;
10+
use Illuminate\Support\Collection;
1011

1112

1213
/**
@@ -28,10 +29,28 @@ class Databases extends Endpoint implements EndpointInterface
2829
*
2930
* @return DatabaseCollection
3031
*/
31-
public function all(): DatabaseCollection
32+
public function all(): Collection
33+
{
34+
return $this->collect()->getResults();
35+
}
36+
37+
/**
38+
* List databases (raw json-data)
39+
* url: https://api.notion.com/{version}/databases
40+
* notion-api-docs: https://developers.notion.com/reference/get-databases
41+
*
42+
* @return DatabaseCollection
43+
*/
44+
public function allRaw(): Collection
45+
{
46+
return $this->collect()->getRawResults();
47+
}
48+
49+
private function collect(): DatabaseCollection
3250
{
3351
$resultData = $this->getJson($this->url(Endpoint::DATABASES) . "?{$this->buildPaginationQuery()}");
34-
return new DatabaseCollection($resultData);
52+
$databaseCollection = new DatabaseCollection($resultData);
53+
return $databaseCollection;
3554
}
3655

3756
/**
@@ -54,5 +73,4 @@ public function find(string $databaseId): Database
5473

5574
return new Database($response->json());
5675
}
57-
5876
}

src/Entities/Block.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,70 @@
22

33
namespace FiveamCode\LaravelNotionApi\Entities;
44

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

910
class Block extends Entity
1011
{
12+
protected string $type;
13+
protected bool $hasChildren;
14+
protected array $rawContent;
15+
protected DateTime $createdTime;
16+
protected DateTime $lastEditedTime;
17+
1118
protected function setResponseData(array $responseData): void
1219
{
1320
parent::setResponseData($responseData);
1421
if ($responseData['object'] !== 'block') throw WrapperException::instance("invalid json-array: the given object is not a block");
22+
23+
$this->fillFromRaw();
24+
}
25+
26+
private function fillFromRaw(): void
27+
{
28+
$this->fillId();
29+
$this->fillType();
30+
$this->fillContent();
31+
$this->fillHasChildren();
32+
$this->fillCreatedTime();
33+
$this->fillLastEditedTime();
34+
}
35+
36+
private function fillType(): void
37+
{
38+
if (Arr::exists($this->responseData, 'type')) {
39+
$this->type = $this->responseData['type'];
40+
}
41+
}
42+
43+
private function fillContent(): void
44+
{
45+
if (Arr::exists($this->responseData, $this->type)) {
46+
$this->rawContent = $this->responseData[$this->type];
47+
}
48+
}
49+
50+
private function fillHasChildren(): void
51+
{
52+
if (Arr::exists($this->responseData, 'has_children')) {
53+
$this->hasChildren = $this->responseData['has_children'];
54+
}
55+
}
56+
57+
public function getType(): string
58+
{
59+
return $this->type;
60+
}
61+
62+
public function getRawContent(): array
63+
{
64+
return $this->rawContent;
65+
}
66+
67+
public function hasChildren(): bool
68+
{
69+
return $this->hasChildren;
1570
}
1671
}

src/Entities/Database.php

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

55
use DateTime;
6-
use Carbon\Carbon;
76
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
87
use FiveamCode\LaravelNotionApi\Notion;
98
use Illuminate\Support\Arr;
109

1110

1211
class Database extends Entity
1312
{
14-
private string $title = "";
15-
private array $rawTitle = [];
16-
private array $rawProperties = [];
17-
private DateTime $createdTime;
18-
private DateTime $lastEditedTime;
13+
protected string $title = "";
14+
protected array $rawTitle = [];
15+
protected array $rawProperties = [];
16+
protected DateTime $createdTime;
17+
protected DateTime $lastEditedTime;
1918

2019

2120
protected function setResponseData(array $responseData): void
@@ -50,21 +49,6 @@ private function fillProperties()
5049
}
5150
}
5251

53-
private function fillCreatedTime()
54-
{
55-
if (Arr::exists($this->responseData, 'created_time')) {
56-
$this->createdTime = new Carbon($this->responseData['created_time']);
57-
}
58-
}
59-
60-
private function fillLastEditedTime()
61-
{
62-
if (Arr::exists($this->responseData, 'last_edited_time')) {
63-
$this->lastEditedTime = new Carbon($this->responseData['last_edited_time']);
64-
}
65-
}
66-
67-
6852
public function getTitle(): string
6953
{
7054
return $this->title;

src/Entities/Entity.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
66
use FiveamCode\LaravelNotionApi\Notion;
77
use Illuminate\Support\Arr;
8+
use Carbon\Carbon;
89

910
class Entity
1011
{
@@ -25,16 +26,31 @@ protected function setResponseData(array $responseData): void
2526
$this->responseData = $responseData;
2627
}
2728

28-
protected function fillId(){
29+
protected function fillCreatedTime()
30+
{
31+
if (Arr::exists($this->responseData, 'created_time')) {
32+
$this->createdTime = new Carbon($this->responseData['created_time']);
33+
}
34+
}
35+
36+
protected function fillLastEditedTime()
37+
{
38+
if (Arr::exists($this->responseData, 'last_edited_time')) {
39+
$this->lastEditedTime = new Carbon($this->responseData['last_edited_time']);
40+
}
41+
}
42+
43+
protected function fillId()
44+
{
2945
$this->id = $this->responseData['id'];
3046
}
3147

32-
public function getId() : string
48+
public function getId(): string
3349
{
3450
return $this->id;
3551
}
3652

37-
public function getRaw() : array
53+
public function getRaw(): array
3854
{
3955
return $this->responseData;
4056
}

0 commit comments

Comments
 (0)