Skip to content

Commit 7bbb2d5

Browse files
authored
feat(comments): implement Comment object and endpoints (#56)
1 parent fd0dea2 commit 7bbb2d5

12 files changed

Lines changed: 901 additions & 0 deletions

File tree

src/Client.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Brd6\NotionSdkPhp\Constant\NotionErrorCodeConstant;
88
use Brd6\NotionSdkPhp\Endpoint\BlocksEndpoint;
9+
use Brd6\NotionSdkPhp\Endpoint\CommentsEndpoint;
910
use Brd6\NotionSdkPhp\Endpoint\DatabasesEndpoint;
1011
use Brd6\NotionSdkPhp\Endpoint\PagesEndpoint;
1112
use Brd6\NotionSdkPhp\Endpoint\SearchEndpoint;
@@ -44,6 +45,7 @@ class Client
4445
private ClientOptions $options;
4546
private HttpMethodsClientInterface $httpClient;
4647
private BlocksEndpoint $blocksEndpoint;
48+
private CommentsEndpoint $commentsEndpoint;
4749
private UsersEndpoint $usersEndpoint;
4850
private PagesEndpoint $pagesEndpoint;
4951
private DatabasesEndpoint $databasesEndpoint;
@@ -55,6 +57,7 @@ public function __construct(?ClientOptions $options = null)
5557
$this->initializeHttpClient();
5658

5759
$this->blocksEndpoint = new BlocksEndpoint($this);
60+
$this->commentsEndpoint = new CommentsEndpoint($this);
5861
$this->usersEndpoint = new UsersEndpoint($this);
5962
$this->pagesEndpoint = new PagesEndpoint($this);
6063
$this->databasesEndpoint = new DatabasesEndpoint($this);
@@ -158,6 +161,11 @@ public function blocks(): BlocksEndpoint
158161
return $this->blocksEndpoint;
159162
}
160163

164+
public function comments(): CommentsEndpoint
165+
{
166+
return $this->commentsEndpoint;
167+
}
168+
161169
public function users(): UsersEndpoint
162170
{
163171
return $this->usersEndpoint;

src/Endpoint/CommentsEndpoint.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brd6\NotionSdkPhp\Endpoint;
6+
7+
use Brd6\NotionSdkPhp\Exception\ApiResponseException;
8+
use Brd6\NotionSdkPhp\Exception\HttpResponseException;
9+
use Brd6\NotionSdkPhp\Exception\InvalidFileException;
10+
use Brd6\NotionSdkPhp\Exception\InvalidPaginationResponseException;
11+
use Brd6\NotionSdkPhp\Exception\InvalidParentException;
12+
use Brd6\NotionSdkPhp\Exception\InvalidResourceException;
13+
use Brd6\NotionSdkPhp\Exception\InvalidResourceTypeException;
14+
use Brd6\NotionSdkPhp\Exception\InvalidRichTextException;
15+
use Brd6\NotionSdkPhp\Exception\RequestTimeoutException;
16+
use Brd6\NotionSdkPhp\Exception\UnsupportedFileTypeException;
17+
use Brd6\NotionSdkPhp\Exception\UnsupportedPaginationResponseTypeException;
18+
use Brd6\NotionSdkPhp\Exception\UnsupportedParentTypeException;
19+
use Brd6\NotionSdkPhp\Exception\UnsupportedRichTextTypeException;
20+
use Brd6\NotionSdkPhp\Exception\UnsupportedUserTypeException;
21+
use Brd6\NotionSdkPhp\RequestParameters;
22+
use Brd6\NotionSdkPhp\Resource\Comment;
23+
use Brd6\NotionSdkPhp\Resource\Pagination\CommentResults;
24+
use Brd6\NotionSdkPhp\Resource\Pagination\PaginationRequest;
25+
use Http\Client\Exception;
26+
27+
use function array_merge;
28+
use function count;
29+
use function strlen;
30+
31+
class CommentsEndpoint extends AbstractEndpoint
32+
{
33+
/**
34+
* @throws ApiResponseException
35+
* @throws HttpResponseException
36+
* @throws InvalidPaginationResponseException
37+
* @throws RequestTimeoutException
38+
* @throws UnsupportedPaginationResponseTypeException
39+
* @throws Exception
40+
* @throws InvalidFileException
41+
* @throws InvalidParentException
42+
* @throws InvalidResourceException
43+
* @throws InvalidResourceTypeException
44+
* @throws InvalidRichTextException
45+
* @throws UnsupportedFileTypeException
46+
* @throws UnsupportedParentTypeException
47+
* @throws UnsupportedRichTextTypeException
48+
* @throws UnsupportedUserTypeException
49+
*/
50+
public function retrieve(string $blockId, ?PaginationRequest $paginationRequest = null): CommentResults
51+
{
52+
$query = ['block_id' => $blockId];
53+
54+
if ($paginationRequest !== null) {
55+
$query = array_merge($query, $paginationRequest->toArray());
56+
}
57+
58+
$requestParameters = (new RequestParameters())
59+
->setPath('comments')
60+
->setMethod('GET')
61+
->setQuery($query);
62+
63+
$rawData = $this->getClient()->request($requestParameters);
64+
65+
/** @var CommentResults $commentResults */
66+
$commentResults = CommentResults::fromRawData($rawData);
67+
68+
return $commentResults;
69+
}
70+
71+
/**
72+
* @throws ApiResponseException
73+
* @throws Exception
74+
* @throws HttpResponseException
75+
* @throws InvalidFileException
76+
* @throws InvalidParentException
77+
* @throws InvalidResourceException
78+
* @throws InvalidResourceTypeException
79+
* @throws InvalidRichTextException
80+
* @throws RequestTimeoutException
81+
* @throws UnsupportedFileTypeException
82+
* @throws UnsupportedParentTypeException
83+
* @throws UnsupportedRichTextTypeException
84+
* @throws UnsupportedUserTypeException
85+
*/
86+
public function create(Comment $comment): Comment
87+
{
88+
$data = [];
89+
$parent = $comment->getParent();
90+
91+
if ($parent !== null) {
92+
$data['parent'] = $parent->toArray();
93+
} elseif (strlen($comment->getDiscussionId()) > 0) {
94+
$data['discussion_id'] = $comment->getDiscussionId();
95+
} else {
96+
throw new InvalidParentException('Either parent.page_id or discussion_id must be provided');
97+
}
98+
99+
$richTextData = [];
100+
foreach ($comment->getRichText() as $richText) {
101+
$richTextData[] = $richText->toArray();
102+
}
103+
$data['rich_text'] = $richTextData;
104+
105+
if (count($comment->getAttachments()) > 0) {
106+
$attachmentsData = [];
107+
foreach ($comment->getAttachments() as $attachment) {
108+
$attachmentsData[] = $attachment->toArray();
109+
}
110+
$data['attachments'] = $attachmentsData;
111+
}
112+
113+
$requestParameters = (new RequestParameters())
114+
->setPath('comments')
115+
->setMethod('POST')
116+
->setBody($data);
117+
118+
$rawData = $this->getClient()->request($requestParameters);
119+
120+
/** @var Comment $createdComment */
121+
$createdComment = Comment::fromRawData($rawData);
122+
123+
return $createdComment;
124+
}
125+
}

src/Resource/Comment.php

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brd6\NotionSdkPhp\Resource;
6+
7+
use Brd6\NotionSdkPhp\Exception\InvalidFileException;
8+
use Brd6\NotionSdkPhp\Exception\InvalidParentException;
9+
use Brd6\NotionSdkPhp\Exception\InvalidRichTextException;
10+
use Brd6\NotionSdkPhp\Exception\UnsupportedFileTypeException;
11+
use Brd6\NotionSdkPhp\Exception\UnsupportedParentTypeException;
12+
use Brd6\NotionSdkPhp\Exception\UnsupportedRichTextTypeException;
13+
use Brd6\NotionSdkPhp\Exception\UnsupportedUserTypeException;
14+
use Brd6\NotionSdkPhp\Resource\Page\Parent\AbstractParentProperty;
15+
use Brd6\NotionSdkPhp\Resource\RichText\AbstractRichText;
16+
use Brd6\NotionSdkPhp\Resource\User\AbstractUser;
17+
use DateTimeImmutable;
18+
19+
class Comment extends AbstractResource
20+
{
21+
public const RESOURCE_TYPE = 'comment';
22+
23+
protected ?AbstractParentProperty $parent = null;
24+
protected string $discussionId = '';
25+
protected ?DateTimeImmutable $createdTime = null;
26+
protected ?DateTimeImmutable $lastEditedTime = null;
27+
protected ?UserInterface $createdBy = null;
28+
29+
/**
30+
* @var array<AbstractRichText>
31+
*/
32+
protected array $richText = [];
33+
34+
/**
35+
* @var array<CommentAttachment>
36+
*/
37+
protected array $attachments = [];
38+
39+
public function __construct()
40+
{
41+
parent::__construct();
42+
43+
$this->object = self::RESOURCE_TYPE;
44+
}
45+
46+
/**
47+
* @throws InvalidFileException
48+
* @throws InvalidParentException
49+
* @throws InvalidRichTextException
50+
* @throws UnsupportedFileTypeException
51+
* @throws UnsupportedParentTypeException
52+
* @throws UnsupportedRichTextTypeException
53+
* @throws UnsupportedUserTypeException
54+
*/
55+
protected function initialize(): void
56+
{
57+
$this->parent = isset($this->getRawData()['parent']) ?
58+
AbstractParentProperty::fromRawData((array) $this->getRawData()['parent']) :
59+
null;
60+
61+
$this->discussionId = (string) ($this->getRawData()['discussion_id'] ?? '');
62+
63+
$this->createdTime = isset($this->getRawData()['created_time']) ?
64+
new DateTimeImmutable((string) $this->getRawData()['created_time']) :
65+
null;
66+
67+
$this->lastEditedTime = isset($this->getRawData()['last_edited_time']) ?
68+
new DateTimeImmutable((string) $this->getRawData()['last_edited_time']) :
69+
null;
70+
71+
$this->createdBy = isset($this->getRawData()['created_by']) ?
72+
AbstractUser::fromRawData((array) $this->getRawData()['created_by']) :
73+
null;
74+
75+
/** @var array<array> $richTextData */
76+
$richTextData = (array) ($this->getRawData()['rich_text'] ?? []);
77+
foreach ($richTextData as $richTextItem) {
78+
$this->richText[] = AbstractRichText::fromRawData($richTextItem);
79+
}
80+
81+
/** @var array<array> $attachmentsData */
82+
$attachmentsData = (array) ($this->getRawData()['attachments'] ?? []);
83+
foreach ($attachmentsData as $attachmentItem) {
84+
$this->attachments[] = CommentAttachment::fromRawData($attachmentItem);
85+
}
86+
}
87+
88+
public static function getResourceType(): string
89+
{
90+
return self::RESOURCE_TYPE;
91+
}
92+
93+
public function getParent(): ?AbstractParentProperty
94+
{
95+
return $this->parent;
96+
}
97+
98+
public function setParent(?AbstractParentProperty $parent): self
99+
{
100+
$this->parent = $parent;
101+
102+
return $this;
103+
}
104+
105+
public function getDiscussionId(): string
106+
{
107+
return $this->discussionId;
108+
}
109+
110+
public function setDiscussionId(string $discussionId): self
111+
{
112+
$this->discussionId = $discussionId;
113+
114+
return $this;
115+
}
116+
117+
public function getCreatedTime(): ?DateTimeImmutable
118+
{
119+
return $this->createdTime;
120+
}
121+
122+
public function setCreatedTime(?DateTimeImmutable $createdTime): self
123+
{
124+
$this->createdTime = $createdTime;
125+
126+
return $this;
127+
}
128+
129+
public function getLastEditedTime(): ?DateTimeImmutable
130+
{
131+
return $this->lastEditedTime;
132+
}
133+
134+
public function setLastEditedTime(?DateTimeImmutable $lastEditedTime): self
135+
{
136+
$this->lastEditedTime = $lastEditedTime;
137+
138+
return $this;
139+
}
140+
141+
public function getCreatedBy(): ?UserInterface
142+
{
143+
return $this->createdBy;
144+
}
145+
146+
public function setCreatedBy(?UserInterface $createdBy): self
147+
{
148+
$this->createdBy = $createdBy;
149+
150+
return $this;
151+
}
152+
153+
/**
154+
* @return array<AbstractRichText>
155+
*/
156+
public function getRichText(): array
157+
{
158+
return $this->richText;
159+
}
160+
161+
/**
162+
* @param array<AbstractRichText> $richText
163+
*/
164+
public function setRichText(array $richText): self
165+
{
166+
$this->richText = $richText;
167+
168+
return $this;
169+
}
170+
171+
/**
172+
* @return array<CommentAttachment>
173+
*/
174+
public function getAttachments(): array
175+
{
176+
return $this->attachments;
177+
}
178+
179+
/**
180+
* @param array<CommentAttachment> $attachments
181+
*/
182+
public function setAttachments(array $attachments): self
183+
{
184+
$this->attachments = $attachments;
185+
186+
return $this;
187+
}
188+
}

0 commit comments

Comments
 (0)