Skip to content

Commit

Permalink
feat: add support for custom emoji (#48)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
brd6 authored Dec 9, 2024
1 parent 09a8a53 commit 2f193f9
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Resource/File/CustomEmoji.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Brd6\NotionSdkPhp\Resource\File;

use Brd6\NotionSdkPhp\Resource\Property\CustomEmojiProperty;

class CustomEmoji extends AbstractFile
{
public const FILE_TYPE = 'custom_emoji';

protected ?CustomEmojiProperty $customEmoji = null;

public static function getFileType(): string
{
return self::FILE_TYPE;
}

protected function initialize(): void
{
$data = (array) $this->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;
}
}
59 changes: 59 additions & 0 deletions src/Resource/Property/CustomEmojiProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Brd6\NotionSdkPhp\Resource\Property;

class CustomEmojiProperty extends AbstractProperty
{
protected string $id = '';
protected string $name = '';
protected string $url = '';

public static function fromRawData(array $rawData): self
{
$property = new self();

$property->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;
}
}
29 changes: 29 additions & 0 deletions tests/Fixtures/client_blocks_retrieve_block_custom_emoji_200.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
29 changes: 29 additions & 0 deletions tests/Resource/BlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}
}

0 comments on commit 2f193f9

Please sign in to comment.