Skip to content

Commit 9fe5ae3

Browse files
committed
Merge branch 'dev' into feature/notion-models-and-commands
2 parents 8c53b2a + b3bb528 commit 9fe5ae3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1076
-198
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
"require": {
2828
"php": "^8.0",
2929
"guzzlehttp/guzzle": "^7.0.1",
30-
"illuminate/support": "^8.0|^9.0"
30+
"illuminate/support": "^8.0|^9.0|^10.0"
3131
},
3232
"require-dev": {
33-
"orchestra/testbench": "^6.0",
33+
"orchestra/testbench": "^6.0|^8.0",
3434
"pestphp/pest": "^1.22",
3535
"pestphp/pest-plugin-laravel": "^1.3",
3636
"phpunit/phpunit": "^9.0"

src/Endpoints/Comments.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Endpoints;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\Collections\CommentCollection;
6+
use FiveamCode\LaravelNotionApi\Entities\Comment as CommentEntity;
7+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
8+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
9+
use FiveamCode\LaravelNotionApi\Notion;
10+
11+
/**
12+
* Class Comments.
13+
*/
14+
class Comments extends Endpoint
15+
{
16+
/**
17+
* @var ?string
18+
*/
19+
private ?string $discussionId = null;
20+
21+
/**
22+
* @var ?string
23+
*/
24+
private ?string $pageId = null;
25+
26+
/**
27+
* Block constructor.
28+
*
29+
* @param Notion $notion
30+
* @param string $blockId
31+
*
32+
* @throws HandlingException
33+
* @throws \FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException
34+
*/
35+
public function __construct(Notion $notion)
36+
{
37+
parent::__construct($notion);
38+
}
39+
40+
/**
41+
* Retrieve a list of comments
42+
* url: https://api.notion.com/{version}/comments?block_id=* [get]
43+
* notion-api-docs: https://developers.notion.com/reference/retrieve-a-comment.
44+
*
45+
* @param string $blockId
46+
* @return CommentCollection
47+
*
48+
* @throws HandlingException
49+
* @throws NotionException
50+
*/
51+
public function ofBlock(string $blockId): CommentCollection
52+
{
53+
$response = $this->get(
54+
$this->url(Endpoint::COMMENTS."?block_id={$blockId}&{$this->buildPaginationQuery()}")
55+
);
56+
57+
return new CommentCollection($response->json());
58+
}
59+
60+
/**
61+
* @param string $discussionId
62+
* @return Comments
63+
*/
64+
public function onDiscussion(string $discussionId): self
65+
{
66+
if ($this->pageId !== null) {
67+
throw new HandlingException('You can only use `onDiscussion()` or `onPage()`.');
68+
}
69+
70+
$this->discussionId = $discussionId;
71+
72+
return $this;
73+
}
74+
75+
/**
76+
* @param string $pageId
77+
* @return Comments
78+
*/
79+
public function onPage(string $pageId): self
80+
{
81+
if ($this->discussionId !== null) {
82+
throw new HandlingException('You can only use `onDiscussion()` or `onPage()`.');
83+
}
84+
85+
$this->pageId = $pageId;
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* Create a comment
92+
* url: https://api.notion.com/{version}/comments [post]
93+
* notion-api-docs: https://developers.notion.com/reference/create-a-comment.
94+
*
95+
* @param CommentEntity $comment
96+
* @return CommentEntity
97+
*
98+
* @throws HandlingException
99+
* @throws NotionException
100+
*/
101+
public function create($comment): CommentEntity
102+
{
103+
if ($this->discussionId === null && $this->pageId === null) {
104+
throw new HandlingException('You must use `onDiscussion()` or `onPage()`.');
105+
}
106+
107+
$body = $comment->getRawResponse();
108+
if ($this->discussionId !== null) {
109+
$body['discussion_id'] = $this->discussionId;
110+
} else {
111+
$body['parent'] = [
112+
'page_id' => $this->pageId,
113+
];
114+
}
115+
116+
$response = $this->post(
117+
$this->url(Endpoint::COMMENTS),
118+
$body
119+
);
120+
121+
return new CommentEntity($response->json());
122+
}
123+
}

src/Endpoints/Database.php

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use FiveamCode\LaravelNotionApi\Notion;
99
use FiveamCode\LaravelNotionApi\Query\Filters\Filter;
1010
use FiveamCode\LaravelNotionApi\Query\Filters\FilterBag;
11+
use FiveamCode\LaravelNotionApi\Query\Filters\Operators;
1112
use FiveamCode\LaravelNotionApi\Query\Sorting;
1213
use Illuminate\Support\Collection;
1314

@@ -60,6 +61,18 @@ public function __construct(string $databaseId, Notion $notion)
6061
* @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException
6162
*/
6263
public function query(): PageCollection
64+
{
65+
$response = $this
66+
->post(
67+
$this->url(Endpoint::DATABASES."/{$this->databaseId}/query"),
68+
$this->getPostData()
69+
)
70+
->json();
71+
72+
return new PageCollection($response);
73+
}
74+
75+
public function getPostData(): array
6376
{
6477
$postData = [];
6578

@@ -81,28 +94,20 @@ public function query(): PageCollection
8194
$postData['page_size'] = $this->pageSize;
8295
}
8396

84-
$response = $this
85-
->post(
86-
$this->url(Endpoint::DATABASES."/{$this->databaseId}/query"),
87-
$postData
88-
)
89-
->json();
90-
91-
return new PageCollection($response);
97+
return $postData;
9298
}
9399

94100
/**
95101
* @param $filter
96102
* @return Database $this
97103
*
98104
* @throws HandlingException
99-
*
100-
* @todo As soon as this package drops PHP 7.4 support, we can use union types here (FilterBag and Filter)
101105
*/
102-
public function filterBy($filter): Database // TODO that's a breaking change
106+
public function filterBy(Collection|Filter|FilterBag $filter): Database
103107
{
104-
$this->checkFilterType($filter);
105-
108+
if ($filter instanceof Collection) {
109+
return $this->filterByCollection($filter);
110+
}
106111
if ($filter instanceof FilterBag) {
107112
return $this->filterByBag($filter);
108113
}
@@ -113,6 +118,12 @@ public function filterBy($filter): Database // TODO that's a breaking change
113118
return $this;
114119
}
115120

121+
/**
122+
* @param Filter $filter
123+
* @return $this
124+
*
125+
* @throws HandlingException
126+
*/
116127
public function filterBySingleFilter(Filter $filter): Database
117128
{
118129
$this->filter = $filter;
@@ -123,7 +134,7 @@ public function filterBySingleFilter(Filter $filter): Database
123134

124135
/**
125136
* @param FilterBag $filterBag
126-
* @return $this
137+
* @return Database $this
127138
*/
128139
public function filterByBag(FilterBag $filterBag): Database
129140
{
@@ -133,6 +144,18 @@ public function filterByBag(FilterBag $filterBag): Database
133144
return $this;
134145
}
135146

147+
/**
148+
* @param Collection $filterCollection
149+
* @return Database $this
150+
*/
151+
public function filterByCollection(Collection $filterCollection): Database
152+
{
153+
$filterBag = new FilterBag(Operators::OR);
154+
$filterBag->addFilters($filterCollection);
155+
156+
return $this->filterByBag($filterBag);
157+
}
158+
136159
/**
137160
* @param Collection|Sorting $sorts
138161
* @return Database $this
@@ -150,7 +173,7 @@ public function sortBy(Sorting|Collection $sorts): Database
150173
$this->sorts = $sorts;
151174
break;
152175
default:
153-
throw new HandlingException("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of Sortings.");
176+
throw new HandlingException("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of sortings.");
154177
}
155178

156179
return $this;
@@ -166,11 +189,4 @@ public function offsetByResponse(EntityCollection $entityCollection): Database
166189

167190
return $this;
168191
}
169-
170-
private function checkFilterType($filter): void
171-
{
172-
if (! ($filter instanceof Filter || $filter instanceof FilterBag)) {
173-
throw new HandlingException('Please provide either a filter bag or a single filter.');
174-
}
175-
}
176192
}

src/Endpoints/Endpoint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Endpoint
1919
public const PAGES = 'pages';
2020
public const USERS = 'users';
2121
public const SEARCH = 'search';
22+
public const COMMENTS = 'comments';
2223

2324
/**
2425
* @var Notion

src/Entities/Blocks/Block.php

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
namespace FiveamCode\LaravelNotionApi\Entities\Blocks;
44

5-
use DateTime;
65
use FiveamCode\LaravelNotionApi\Entities\Entity;
76
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
7+
use FiveamCode\LaravelNotionApi\Traits\HasArchive;
8+
use FiveamCode\LaravelNotionApi\Traits\HasParent;
9+
use FiveamCode\LaravelNotionApi\Traits\HasTimestamps;
810
use Illuminate\Support\Arr;
911

1012
/**
1113
* Class Block.
1214
*/
1315
class Block extends Entity
1416
{
17+
use HasTimestamps, HasArchive, HasParent;
18+
1519
/**
1620
* @var string
1721
*/
@@ -37,16 +41,6 @@ class Block extends Entity
3741
*/
3842
protected string $text = '[warning: unsupported in notion api]';
3943

40-
/**
41-
* @var DateTime
42-
*/
43-
protected DateTime $createdTime;
44-
45-
/**
46-
* @var DateTime
47-
*/
48-
protected DateTime $lastEditedTime;
49-
5044
/**
5145
* @param array $responseData
5246
*
@@ -65,12 +59,10 @@ protected function setResponseData(array $responseData): void
6559

6660
protected function fillFromRaw(): void
6761
{
68-
$this->fillId();
62+
parent::fillEssentials();
6963
$this->fillType();
7064
$this->fillRawContent();
7165
$this->fillHasChildren();
72-
$this->fillCreatedTime();
73-
$this->fillLastEditedTime();
7466
}
7567

7668
private function fillType(): void
@@ -126,22 +118,6 @@ public function hasChildren(): bool
126118
return $this->hasChildren;
127119
}
128120

129-
/**
130-
* @return DateTime
131-
*/
132-
public function getCreatedTime(): DateTime
133-
{
134-
return $this->createdTime;
135-
}
136-
137-
/**
138-
* @return DateTime
139-
*/
140-
public function getLastEditedTime(): DateTime
141-
{
142-
return $this->lastEditedTime;
143-
}
144-
145121
public function getContent()
146122
{
147123
return $this->content;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Collections;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\Comment;
6+
use Illuminate\Support\Collection;
7+
8+
/**
9+
* Class CommentCollection.
10+
*/
11+
class CommentCollection extends EntityCollection
12+
{
13+
/**
14+
* collects all comments from the raw results (from Notion).
15+
*/
16+
protected function collectChildren(): void
17+
{
18+
$this->collection = new Collection();
19+
foreach ($this->rawResults as $commentContent) {
20+
$this->collection->add(new Comment($commentContent));
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)