|
| 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 | +} |
0 commit comments