|
| 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