Skip to content

Commit fc9e7cd

Browse files
committed
refactor collections (return EntityCollection, if list is fetched)
1 parent ac2a847 commit fc9e7cd

File tree

5 files changed

+24
-53
lines changed

5 files changed

+24
-53
lines changed

src/Endpoints/Block.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ public function __construct(Notion $notion, string $blockId)
2424
*
2525
* @return BlockCollection
2626
*/
27-
public function children(): Collection
27+
public function children(): BlockCollection
2828
{
29-
return $this->collectChildren()->getResults();
29+
return $this->collectChildren();
3030
}
3131

32-
/**
33-
* Retrieve block children (as raw json-data)
34-
* url: https://api.notion.com/{version}/blocks/{block_id}/children
35-
* notion-api-docs: https://developers.notion.com/reference/get-block-children
36-
*
37-
* @return array
38-
*/
39-
public function childrenRaw(): array
40-
{
41-
return $this->collectChildren()->getRawResults();
42-
}
32+
// /**
33+
// * Retrieve block children (as raw json-data)
34+
// * url: https://api.notion.com/{version}/blocks/{block_id}/children
35+
// * notion-api-docs: https://developers.notion.com/reference/get-block-children
36+
// *
37+
// * @return array
38+
// */
39+
// public function childrenRaw(): array
40+
// {
41+
// return $this->collectChildren()->getRawResults();
42+
// }
4343

4444
private function collectChildren(): BlockCollection
4545
{
@@ -49,9 +49,7 @@ private function collectChildren(): BlockCollection
4949
if (!$response->ok())
5050
throw HandlingException::instance("Block not found.", ["blockId" => $this->blockId]);
5151

52-
53-
$blockCollection = new BlockCollection($response->json());
54-
return $blockCollection;
52+
return new BlockCollection($response->json());
5553
}
5654

5755
public function create(): array

src/Endpoints/Databases.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,24 @@
2020
*/
2121
class Databases extends Endpoint implements EndpointInterface
2222
{
23-
24-
2523
/**
2624
* List databases
2725
* url: https://api.notion.com/{version}/databases
2826
* notion-api-docs: https://developers.notion.com/reference/get-databases
2927
*
3028
* @return DatabaseCollection
3129
*/
32-
public function all(): Collection
30+
public function all(): DatabaseCollection
3331
{
34-
return $this->collect()->getResults();
32+
return $this->collect();
3533
}
3634

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 array
43-
*/
44-
public function allRaw(): array
45-
{
46-
return $this->collect()->getRawResults();
47-
}
4835

4936
// TODO rename this function - receive, access, fetch?
5037
private function collect(): DatabaseCollection
5138
{
5239
$resultData = $this->getJson($this->url(Endpoint::DATABASES) . "?{$this->buildPaginationQuery()}");
53-
$databaseCollection = new DatabaseCollection($resultData);
54-
return $databaseCollection;
40+
return new DatabaseCollection($resultData);
5541
}
5642

5743
/**

src/Endpoints/Users.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,11 @@ class Users extends Endpoint implements EndpointInterface
1717
* url: https://api.notion.com/{version}/users
1818
* notion-api-docs: https://developers.notion.com/reference/get-users
1919
*
20-
* @return Collection
20+
* @return UserCollection
2121
*/
22-
public function all(): Collection
22+
public function all(): UserCollection
2323
{
24-
return $this->collect()->getResults();
25-
}
26-
27-
28-
/**
29-
* List users (raw json-data)
30-
* url: https://api.notion.com/{version}/users
31-
* notion-api-docs: https://developers.notion.com/reference/get-users
32-
*
33-
* @return array
34-
*/
35-
public function allRaw(): array
36-
{
37-
return $this->collect()->getRawResults();
24+
return $this->collect();
3825
}
3926

4027
private function collect(): UserCollection{

src/Entities/Collections/EntityCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ public function getRaw(): array
6060
return $this->responseData;
6161
}
6262

63-
public function getRawResults(): array
63+
public function asRaw(): array
6464
{
6565
return $this->rawResults;
6666
}
6767

68-
public function getResults(): Collection // toDo: rename asCollection()
68+
public function asCollection(): Collection
6969
{
7070
return $this->collection;
7171
}

tests/EndpointDatabaseTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function it_returns_a_list_of_database_objects()
4444
)
4545
]);
4646

47-
$result = \Notion::databases()->all();
47+
$result = \Notion::databases()->all()->asCollection();
4848

4949
$this->assertIsIterable($result);
5050
$this->assertCount(2, $result);
@@ -63,7 +63,7 @@ public function it_returns_an_empty_list()
6363
)
6464
]);
6565

66-
$result = \Notion::databases()->all();
66+
$result = \Notion::databases()->all()->asCollection();
6767

6868
// TODO check class here
6969
$this->assertIsIterable($result);

0 commit comments

Comments
 (0)