Skip to content

Commit 104ced0

Browse files
committed
some refactoring for entities and simple collections for list-requests
1 parent 3b79e2b commit 104ced0

File tree

12 files changed

+206
-98
lines changed

12 files changed

+206
-98
lines changed

src/Endpoints/Block.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Endpoints;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\BlockCollection;
6+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
7+
use FiveamCode\LaravelNotionApi\Notion;
8+
use Illuminate\Support\Collection;
9+
10+
class Block extends Endpoint
11+
{
12+
private string $blockId;
13+
14+
public function __construct(Notion $notion, string $blockId){
15+
parent::__construct($notion);
16+
$this->blockId = $blockId;
17+
}
18+
19+
/**
20+
* Retrieve block children
21+
* url: https://api.notion.com/{version}/blocks/{block_id}/children
22+
* notion-api-docs: https://developers.notion.com/reference/get-block-children
23+
*
24+
* @param string $blockId
25+
* @return BlockCollection
26+
*/
27+
public function children(): BlockCollection
28+
{
29+
$response = $this->get(
30+
$this->url(Endpoint::BLOCKS . "/" . $this->blockId . "/children")
31+
);
32+
33+
if (!$response->ok())
34+
throw WrapperException::instance("Block not found.", ["blockId" => $this->blockId]);
35+
36+
37+
$blockCollection = new BlockCollection($response->json());
38+
39+
return $blockCollection;
40+
}
41+
42+
public function create(): array
43+
{
44+
//toDo
45+
throw new \Exception("not implemented yet");
46+
}
47+
}

src/Endpoints/Blocks.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/Endpoints/Databases.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

55
use FiveamCode\LaravelNotionApi\Entities\Database;
6+
use FiveamCode\LaravelNotionApi\Entities\DatabaseCollection;
7+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
68
use FiveamCode\LaravelNotionApi\Notion;
79

810
class Databases extends Endpoint implements EndpointInterface
@@ -14,11 +16,12 @@ class Databases extends Endpoint implements EndpointInterface
1416
* url: https://api.notion.com/{version}/databases
1517
* notion-api-docs: https://developers.notion.com/reference/get-databases
1618
*
17-
* @return array
19+
* @return DatabaseCollection
1820
*/
19-
public function all(): array
21+
public function all(): DatabaseCollection
2022
{
21-
return $this->getJson($this->url(Endpoint::DATABASES));
23+
$resultData = $this->getJson($this->url(Endpoint::DATABASES));
24+
return new DatabaseCollection($resultData);
2225
}
2326

2427
/**
@@ -31,10 +34,14 @@ public function all(): array
3134
*/
3235
public function find(string $databaseId): Database
3336
{
34-
$jsonArray = $this->getJson(
37+
$response = $this->get(
3538
$this->url(Endpoint::DATABASES . "/{$databaseId}")
3639
);
37-
return new Database($jsonArray);
40+
41+
if (!$response->ok())
42+
throw WrapperException::instance("Database not found.", ["databaseId" => $databaseId]);
43+
44+
return new Database($response->json());
3845
}
3946

4047
}

src/Entities/Block.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
class Block extends Entity
1010
{
11-
protected function setRaw(array $raw): void
11+
protected function setResponseData(array $responseData): void
1212
{
13-
parent::setRaw($raw);
14-
if ($raw['object'] !== 'block') throw WrapperException::instance("invalid json-array: the given object is not a block");
13+
parent::setResponseData($responseData);
14+
if ($responseData['object'] !== 'block') throw WrapperException::instance("invalid json-array: the given object is not a block");
1515
}
1616
}

src/Entities/BlockCollection.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 BlockCollection 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 Block($blockChild));
25+
}
26+
}
27+
}

src/Entities/Database.php

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,27 @@
1212
class Database extends Entity
1313
{
1414
private string $title = "";
15-
private array $rawTitle = []; // toDo why "raw"title?
15+
private array $rawTitle = [];
1616
private array $rawProperties = [];
1717
private DateTime $createdTime;
1818
private DateTime $lastEditedTime;
1919

20-
// toDo Draft
21-
private array $responseData;
2220

23-
24-
protected function setRaw(array $raw): void
21+
protected function setResponseData(array $responseData): void
2522
{
26-
parent::setRaw($raw);
27-
if ($raw['object'] !== 'database') throw WrapperException::instance("invalid json-array: the given object is not a database");
28-
29-
//toDo Draft
30-
$this->responseData = $raw;
31-
23+
parent::setResponseData($responseData);
24+
if ($responseData['object'] !== 'database') throw WrapperException::instance("invalid json-array: the given object is not a database");
3225
$this->fillFromRaw();
33-
34-
// if (Arr::exists($raw, 'title') && is_array($raw['title'])) {
35-
// $this->title = Arr::first($raw['title'], null, ['plain_text' => ''])['plain_text'];
36-
// $this->rawTitle = $raw['title'];
37-
// }
38-
//
39-
// if (Arr::exists($raw, 'properties')) {
40-
// $this->rawProperties = $raw['properties'];
41-
// }
42-
//
43-
// if (Arr::exists($raw, 'created_time')) {
44-
// $this->createdTime = new Carbon($raw['created_time']);
45-
// }
46-
//
47-
// if (Arr::exists($raw, 'last_edited_time')) {
48-
// $this->lastEditedTime = new Carbon($raw['last_edited_time']);
49-
// }
5026
}
51-
//toDo Draft
27+
5228

5329
private function fillFromRaw()
5430
{
31+
$this->fillId();
5532
$this->fillTitle();
33+
$this->fillProperties();
34+
$this->fillCreatedTime();
35+
$this->fillLastEditedTime();
5636
}
5737

5838
private function fillTitle()
@@ -62,7 +42,28 @@ private function fillTitle()
6242
$this->rawTitle = $this->responseData['title'];
6343
}
6444
}
65-
//toDo end Draft
45+
46+
private function fillProperties()
47+
{
48+
if (Arr::exists($this->responseData, 'properties')) {
49+
$this->rawProperties = $this->responseData['properties'];
50+
}
51+
}
52+
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+
6667

6768
public function getTitle(): string
6869
{

src/Entities/DatabaseCollection.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 DatabaseCollection 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 $databaseChild) {
24+
$this->collection->add(new Database($databaseChild));
25+
}
26+
}
27+
}

src/Entities/Entity.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@
99
class Entity
1010
{
1111
private string $id;
12-
private array $raw;
12+
protected array $responseData = [];
1313

14-
public function __construct(array $raw = null)
14+
15+
public function __construct(array $responseData = null)
1516
{
16-
$this->setRaw($raw);
17+
$this->setResponseData($responseData);
1718
}
1819

19-
protected function setRaw(array $raw): void
20+
protected function setResponseData(array $responseData): void
2021
{
21-
if (!Arr::exists($raw, 'object')) throw WrapperException::instance("invalid json-array: no object given");
22-
if (!Arr::exists($raw, 'id')) throw WrapperException::instance("invalid json-array: no id provided");
22+
if (!Arr::exists($responseData, 'object')) throw WrapperException::instance("invalid json-array: no object given");
23+
if (!Arr::exists($responseData, 'id')) throw WrapperException::instance("invalid json-array: no id provided");
2324

24-
$this->raw = $raw;
25-
$this->id = $raw['id'];
25+
$this->responseData = $responseData;
2626
}
2727

28+
protected function fillId(){
29+
$this->id = $this->responseData['id'];
30+
}
2831

2932
public function getId() : string
3033
{
@@ -33,6 +36,6 @@ public function getId() : string
3336

3437
public function getRaw() : array
3538
{
36-
return $this->raw;
39+
return $this->responseData;
3740
}
3841
}

src/Entities/EntityCollection.php

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,57 @@
55
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
66
use FiveamCode\LaravelNotionApi\Notion;
77
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Collection;
89

910

1011
//TODO:WORK IN PROGRESS
11-
class EntityCollection
12+
abstract class EntityCollection
1213
{
13-
protected array $raw;
14-
protected Notion $notion;
14+
protected array $responseData = [];
15+
protected array $rawResults = [];
16+
protected Collection $collection;
1517

16-
public function __construct(Notion $notion = null, array $raw = null)
18+
public function __construct(array $reponseData = null)
1719
{
18-
$this->notion = $notion;
19-
$this->setRaw($raw);
20+
$this->setResponseData($reponseData);
2021
}
2122

22-
protected function setRaw(array $raw): void
23+
protected abstract function collectChildren();
24+
25+
protected function setResponseData(array $reponseData): void
2326
{
24-
if (!Arr::exists($raw, 'object')) throw WrapperException::instance("invalid json-array: no object given");
25-
if (!Arr::exists($raw, 'results')) throw WrapperException::instance("invalid json-array: no results given");
26-
if ($raw['object'] != 'list') throw WrapperException::instance("invalid json-array: the given object is not a list");
27+
if (!Arr::exists($reponseData, 'object')) throw WrapperException::instance("invalid json-array: no object given");
28+
if (!Arr::exists($reponseData, 'results')) throw WrapperException::instance("invalid json-array: no results given");
29+
if ($reponseData['object'] !== 'list') throw WrapperException::instance("invalid json-array: the given object is not a list");
30+
31+
$this->responseData = $reponseData;
32+
$this->fillFromRaw();
33+
}
2734

28-
$this->raw = $raw;
35+
protected function fillFromRaw()
36+
{
37+
$this->fillResult();
38+
}
39+
40+
protected function fillResult()
41+
{
42+
$this->rawResults = $this->responseData['results'];
43+
$this->collectChildren();
2944
}
3045

46+
3147
public function getRaw(): array
3248
{
33-
return $this->raw;
49+
return $this->responseData;
50+
}
51+
52+
public function getRawResults(): array
53+
{
54+
return $this->rawResults;
55+
}
56+
57+
public function getResults(): Collection
58+
{
59+
return $this->collection;
3460
}
3561
}

0 commit comments

Comments
 (0)