shimabox/pemojine

Pemojine is a library for handling Emoji in PHP.

v0.6.5 2020-12-20 05:32 UTC

This package is auto-updated.

Last update: 2024-10-20 13:46:28 UTC


README

pemojine-logo

Pemojine

Pemojine is a library for handling Emoji in PHP.

License Build Status Maintainability Coverage Status Latest Stable Version Latest Unstable Version

demo

This library is getting data from here.

Attention

  • Support for php5.5-7.3

Architecture

pemojine-architecture

  • Treat one Emoji as a group.
  • MediumGroup bundle Groups.
  • BigGroup bundle MediumGroups.
  • Vendor bundle BigGroups.
  • @see Full Emoji List, v13.1

Features

  • You can output it for each vendor (Apple, Google, etc...).
  • Outputable for each group.
    • BigGroups, MediumGroups, Groups.
  • Randomly output Emoji.
  • Create sentences containing Emoji.
  • The group structure can be easily customized.

Demo

  • Todo

Installation

Via Composer

$ composer require shimabox/pemojine

Develop

$ git clone https://github.com/shimabox/pemojine
$ cd pemojine
$ composer install

Usage

<?php
require_once 'vendor/autoload.php';

use SMB\Pemojine\Container as PemojineContainer;
use SMB\Pemojine\Structure\Vendor;
use SMB\Pemojine\Helper\Sentence;

// Create an instance of Google using a Container.
$pemojine = PemojineContainer::make(new Vendor\Google());

// Get the configured vendor name.
echo $pemojine->getVendorName(); // => Google

/*
 |----------------------------------------------------------------------
 | Get all BigGroups.
 |----------------------------------------------------------------------
 */
$allBigGroups = $pemojine->getAllBigGroups();

// $allBigGroups is an array with a big group name in the key and an object(BigGroup) in the value.
foreach ($allBigGroups as $bigGroupName => $bigGroup) {
    echo 'BigGroupName::' . $bigGroupName/* or $bigGroup->getName() */;
    foreach ($bigGroup as $mediumGroupName => $mediumGroup) {
        echo 'MediumGroupName::' . $mediumGroupName/* or $mediumGroup->getName() */;
        foreach ($mediumGroup as $groupName => $group) {
            // Output shortName.
            echo 'Group::' . $groupName/* or $group->getName(), $group->getShortName() */;
            // Output Emoji.
            echo $group->output();
            // Output HTML reference character. e.g) '&#x1F600;', '&#x1F1EF;&#x1F1F5;'
            echo htmlspecialchars($group->outputHtml(), ENT_QUOTES, 'UTF-8');
            // Get unicode. e.g.) 'U+1F600', 'U+1F1EF U+1F1F5'
            echo $group->getUnicode();
            // Get unicode with removed blank. e.g.) 'U+1F600', 'U+1F1EFU+1F1F5'
            echo $group->getUnicodeWithRemovedBlank();
            // Get UTF8 string. e.g.) '\u{1F600}', '\u{1F1EF} \u{1F1F5}'
            echo $group->getUtf8String();
            // Get UTF8 string with removed blank. e.g.) '\u{1F600}', '\u{1F1EF}\u{1F1F5}'
            echo $group->getUtf8StringWithRemovedBlank();
            // Get aliases of shortName
            $alias = implode(' ', $group->getAliasesOfName()/* or $group->getAliasesOfShortName() */);
            echo htmlspecialchars($alias, ENT_QUOTES, 'UTF-8');
        }
    }
}

/*
 |----------------------------------------------------------------------
 | When selecting randomly from Big Groups.
 |----------------------------------------------------------------------
 */
// Select a BigGroup at random
$randomBigGroup = $pemojine->randomFromBigGroup();

echo $randomBigGroup->getName(); // Randomly chosen BigGroupName.

// When acquiring a random group from narrowed down.
$groupOfBig = $randomBigGroup->getGroupAtRandom();
echo $groupOfBig->getName();
$alias = implode(' ', $groupOfBig->getAliasesOfName());
echo htmlspecialchars($alias, ENT_QUOTES, 'UTF-8');
echo $groupOfBig->output();
echo htmlspecialchars($groupOfBig->outputHtml(), ENT_QUOTES, 'UTF-8');
echo $groupOfBig->getUnicode();
echo $groupOfBig->getUnicodeWithRemovedBlank();
echo $groupOfBig->getUtf8String();
echo $groupOfBig->getUtf8StringWithRemovedBlank();

/*
 |----------------------------------------------------------------------
 | When selecting 'animal-mammal' in Medium Groups.
 |----------------------------------------------------------------------
 */
// Select 'animal-mammal'.
$selectedMediumGroup = $pemojine->selectMediumGroup('animal-mammal');

echo $selectedMediumGroup->getVendorName(); // => 'Google'

echo $selectedMediumGroup->getName(); // => 'animal-mammal'

// $selectedMediumGroup is an iterator with a group name in the key and an object(Group) in the value.
foreach ($selectedMediumGroup/* or $selectedMediumGroup->getChildren() */ as $groupName => $group) {
    echo 'Group::' . $groupName;
    echo $group->output();
    echo htmlspecialchars($group->outputHtml(), ENT_QUOTES, 'UTF-8');
    echo $group->getUnicode();
    echo $group->getUnicodeWithRemovedBlank();
    echo $group->getUtf8String();
    echo $group->getUtf8StringWithRemovedBlank();
    $alias = implode(' ', $group->getAliasesOfName());
    echo htmlspecialchars($alias, ENT_QUOTES, 'UTF-8');
}

// When acquiring a random group from narrowed down.
$groupOfM = $selectedMediumGroup->getGroupAtRandom();
echo $groupOfM->getName()/* or $groupOfM->getShortName() */;
$alias = implode(' ', $groupOfM->getAliasesOfName()/* or $groupOfM->getAliasesOfShortName() */);
echo htmlspecialchars($alias, ENT_QUOTES, 'UTF-8');
echo $groupOfM->output();
echo htmlspecialchars($groupOfM->outputHtml(), ENT_QUOTES, 'UTF-8');
echo $groupOfM->getUnicode();
echo $groupOfM->getUnicodeWithRemovedBlank();
echo $groupOfM->getUtf8String();
echo $groupOfM->getUtf8StringWithRemovedBlank();

// In the case of Medium Group, you can find a Big Group of parents.
$parent = $pemojine->selectMediumGroup('animal-mammal')->findParentGroup(); // => Parent BigGroup
assert($parent->getName() === 'Animals & Nature'); // => true

// The following are the same instances.
$a = $pemojine->selectBigGroup('Animals & Nature')->selectMediumGroup('animal-mammal');
$b = $pemojine->selectMediumGroup('animal-mammal');
assert($a === $b); // => true

// Exists?
assert($pemojine->hasMediumGroup('animal-bird') === true);
assert($pemojine->hasMediumGroup('piyo') === false);

/*
 |----------------------------------------------------------------------
 | Create sentences including Emoji.
 |----------------------------------------------------------------------
 */
// Get an Outputter instance.
$outputter = $pemojine->getOutputter();

// Other ways to get instances.
/*
    // Create an EmojiTable instance of Apple.
    $emojiTable = new \SMB\Pemojine\Structure\Vendor\Apple\EmojiTable();
    // Create an Outputter instance.
    $outputter = new \SMB\Pemojine\Outputter\Outputter($emojiTable);
*/

// Create a Sentence instance.
$sentence = new Sentence($outputter);

// Sentence 1
$output_1 = $sentence->create('Hello U+1F1EFU+1F1F5 \u{1F601}:grin:|beaming face with smiling eyes|!!');
assert('Hello 🇯🇵 😁😁😁!!' === $output_1);
echo $output_1;

// Sentence 2
$output_2 = $sentence->create('foo ::heart:: :bar:cinema:baz:');
assert('foo :❤: :bar🎦baz:' === $output_2);
echo $output_2;

// Sentence 3
$output_3 = $sentence->create('foo ||grinning face with big eyes|| |bar|face with tears of joy|baz|');
assert('foo |😃| |bar😂baz|' === $output_3);
echo $output_3;

Customize

How to add a new Emoji group

Create three files below.

  • Files that implements SMB\Pemojine\Structure\Interfaces\Configurable.
  • Files that implements SMB\Pemojine\Structure\Interfaces\Gettable.
  • Files that implements SMB\Pemojine\Structure\Interfaces\EmojiTable\Gettable.

@see example/Custom

Examples (dev only)

Basic

Helper

Custom

Regenerate data (dev only)

$ php scraping/src/generator.php

Scrape the data here.
(The result is cached for 1 hour in scraping/cache/full-emoji-list, scraping/cache/full-emoji-modifiers)

scraping/output/Config, scraping/output/Structure Since files are output below, please place them under src/Config, src/Structure respectively.

Shell for sync.

  • For Mac(Linux).
$ sh scraping/sync.sh
  • For Windows.
$ cd scraping/
$ win_sync.bat

Test

$ composer test # or vendor/bin/phpunit
  • Coverage
$ composer coverage # or vendor/bin/phpunit --coverage-text --coverage-html ./report/

TODO

License

The MIT License (MIT). Please see License File for more information.