Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods for converting emojis from unicode to named and back. #2

Merged
merged 1 commit into from May 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Emoji.php
Expand Up @@ -68,6 +68,28 @@ public function replaceEmojiWithImages($string)
return $string;
}

public function replaceNamedWithUnicode($string)
{
$index = $this->getIndex();

return preg_replace_callback($index->getEmojiNameRegex(), function ($matches) use ($index) {
$emoji = $index->findByName($matches[1]);

return $index->convertUnicodeToString($emoji['unicode']);
}, $string);
}

public function replaceUnicodeWithNamed($string)
{
$index = $this->getIndex();

return preg_replace_callback($index->getEmojiUnicodeRegex(), function ($matches) use ($index) {
$emoji = $index->findByUnicode($matches[0]);

return ':'.$emoji['name'].':';
}, $string);
}

public function countEmoji($string)
{
$index = $this->getIndex();
Expand Down
2 changes: 1 addition & 1 deletion src/EmojiIndex.php
Expand Up @@ -113,7 +113,7 @@ public function getEmojiNameRegex()
return $this->emojiNameRegex;
}

private function convertUnicodeToString($cp)
public function convertUnicodeToString($cp)
{
$cp = hexdec($cp);

Expand Down
20 changes: 17 additions & 3 deletions tests/EmojiTest.php
Expand Up @@ -15,18 +15,32 @@ public function setUp()
public function testEmojiReplacesUnicodeEmojiWithImage()
{
$replacedString = $this->emoji->replaceEmojiWithImages('I ❤ Emoji');
$this->assertEquals('I <img alt=":heart:" class="emoji" src="http://twemoji.maxcdn.com/36x36/2764.png"> Emoji', $replacedString);
$this->assertSame('I <img alt=":heart:" class="emoji" src="http://twemoji.maxcdn.com/36x36/2764.png"> Emoji', $replacedString);
}

public function testEmojiReplacesNamedEmojiWithImage()
{
$replacedString = $this->emoji->replaceEmojiWithImages('Merry Christmas :santa:');
$this->assertEquals('Merry Christmas <img alt=":santa:" class="emoji" src="http://twemoji.maxcdn.com/36x36/1f385.png">', $replacedString);
$this->assertSame('Merry Christmas <img alt=":santa:" class="emoji" src="http://twemoji.maxcdn.com/36x36/1f385.png">', $replacedString);
}

public function testReplaceNamedWithUnicode()
{
$replacedString = $this->emoji->replaceNamedWithUnicode('I :heart: Emoji');

$this->assertSame('I ❤ Emoji', $replacedString);
}

public function testReplaceUnicodeWithNamed()
{
$replacedString = $this->emoji->replaceUnicodeWithNamed('I ❤ Emoji');

$this->assertSame('I :heart: Emoji', $replacedString);
}

public function testCountEmojiWithoutAnyEmoji()
{
$this->assertEquals(0, $this->emoji->countEmoji('This does not contain any emoji'));
$this->assertSame(0, $this->emoji->countEmoji('This does not contain any emoji'));
}

public function testCountEmojiWithEmoji()
Expand Down