From 2f193f979d641e98b288094acf08a5a79a725ebb Mon Sep 17 00:00:00 2001 From: Berdrigue Date: Mon, 9 Dec 2024 20:14:36 +0100 Subject: [PATCH] feat: add support for custom emoji (#48) * feat: add support for custom emoji * style: improve code readability in BlockTest - Split long URL string into multiple lines for better readability in custom emoji test - Maintains same functionality while adhering to coding standards --- src/Resource/File/CustomEmoji.php | 37 ++++++++++++ src/Resource/Property/CustomEmojiProperty.php | 59 +++++++++++++++++++ ...locks_retrieve_block_custom_emoji_200.json | 29 +++++++++ tests/Resource/BlockTest.php | 29 +++++++++ 4 files changed, 154 insertions(+) create mode 100644 src/Resource/File/CustomEmoji.php create mode 100644 src/Resource/Property/CustomEmojiProperty.php create mode 100644 tests/Fixtures/client_blocks_retrieve_block_custom_emoji_200.json diff --git a/src/Resource/File/CustomEmoji.php b/src/Resource/File/CustomEmoji.php new file mode 100644 index 0000000..a052793 --- /dev/null +++ b/src/Resource/File/CustomEmoji.php @@ -0,0 +1,37 @@ +getRawData()[$this->getType()]; + $this->customEmoji = CustomEmojiProperty::fromRawData($data); + } + + public function getCustomEmoji(): ?CustomEmojiProperty + { + return $this->customEmoji; + } + + public function setCustomEmoji(CustomEmojiProperty $customEmoji): self + { + $this->customEmoji = $customEmoji; + + return $this; + } +} diff --git a/src/Resource/Property/CustomEmojiProperty.php b/src/Resource/Property/CustomEmojiProperty.php new file mode 100644 index 0000000..5985fd3 --- /dev/null +++ b/src/Resource/Property/CustomEmojiProperty.php @@ -0,0 +1,59 @@ +id = (string) $rawData['id']; + $property->name = (string) $rawData['name']; + $property->url = (string) $rawData['url']; + + return $property; + } + + public function getId(): string + { + return $this->id; + } + + public function setId(string $id): self + { + $this->id = $id; + + return $this; + } + + public function getName(): string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + public function getUrl(): string + { + return $this->url; + } + + public function setUrl(string $url): self + { + $this->url = $url; + + return $this; + } +} diff --git a/tests/Fixtures/client_blocks_retrieve_block_custom_emoji_200.json b/tests/Fixtures/client_blocks_retrieve_block_custom_emoji_200.json new file mode 100644 index 0000000..a065b51 --- /dev/null +++ b/tests/Fixtures/client_blocks_retrieve_block_custom_emoji_200.json @@ -0,0 +1,29 @@ +{ + "object": "block", + "id": "test-block-id", + "created_time": "2022-03-01T18:42:00.000Z", + "last_edited_time": "2022-03-01T18:54:00.000Z", + "created_by": { + "object": "user", + "id": "1091c8fb-8b5a-4cc2-afe9-fdf0367e16d6" + }, + "last_edited_by": { + "object": "user", + "id": "1091c8fb-8b5a-4cc2-afe9-fdf0367e16d6" + }, + "has_children": false, + "archived": false, + "type": "callout", + "callout": { + "rich_text": [], + "icon": { + "type": "custom_emoji", + "custom_emoji": { + "id": "45ce454c-d427-4f53-9489-e5d0f3d1db6b", + "name": "bufo", + "url": "https://s3-us-west-2.amazonaws.com/public.notion-static.com/865e85fc-7442-44d3-b323-9b03a2111720/3c6796979c50f4aa.png" + } + }, + "color": "default" + } +} \ No newline at end of file diff --git a/tests/Resource/BlockTest.php b/tests/Resource/BlockTest.php index 9e710b3..5d506fb 100644 --- a/tests/Resource/BlockTest.php +++ b/tests/Resource/BlockTest.php @@ -297,4 +297,33 @@ public function testAudioBlock(): void $this->assertNotEmpty($audioFile->getUrl()); $this->assertNotEmpty($audioFile->getExpiryTime()); } + + public function testCustomEmojiBlock(): void + { + $block = AbstractBlock::fromRawData( + (array) json_decode( + (string) file_get_contents('tests/Fixtures/client_blocks_retrieve_block_custom_emoji_200.json'), + true, + ), + ); + + $this->assertInstanceOf(CalloutBlock::class, $block); + $this->assertEquals('callout', $block->getType()); + $this->assertNotNull($block->getCallout()); + $this->assertInstanceOf(CalloutProperty::class, $block->getCallout()); + + $icon = $block->getCallout()->getIcon(); + $this->assertNotNull($icon); + $this->assertEquals('custom_emoji', $icon->getType()); + + $customEmoji = $icon->getCustomEmoji(); + $this->assertNotNull($customEmoji); + $this->assertEquals('45ce454c-d427-4f53-9489-e5d0f3d1db6b', $customEmoji->getId()); + $this->assertEquals('bufo', $customEmoji->getName()); + $this->assertEquals( + 'https://s3-us-west-2.amazonaws.com/public.notion-static.com/' + . '865e85fc-7442-44d3-b323-9b03a2111720/3c6796979c50f4aa.png', + $customEmoji->getUrl(), + ); + } }