function ComponentNodeVisitor::emojiForString

Same name in this branch
  1. 10 core/modules/sdc/src/Twig/ComponentNodeVisitor.php \Drupal\sdc\Twig\ComponentNodeVisitor::emojiForString()
Same name and namespace in other branches
  1. 11.x core/modules/sdc/src/Twig/ComponentNodeVisitor.php \Drupal\sdc\Twig\ComponentNodeVisitor::emojiForString()
  2. 11.x core/lib/Drupal/Core/Template/ComponentNodeVisitor.php \Drupal\Core\Template\ComponentNodeVisitor::emojiForString()

Chooses an emoji representative for the input string.

Parameters

string $input: The input string.

Return value

string The emoji code.

1 call to ComponentNodeVisitor::emojiForString()
ComponentNodeVisitor::leaveNode in core/lib/Drupal/Core/Template/ComponentNodeVisitor.php

File

core/lib/Drupal/Core/Template/ComponentNodeVisitor.php, line 187

Class

ComponentNodeVisitor
Provides a ComponentNodeVisitor to change the generated parse-tree.

Namespace

Drupal\Core\Template

Code

protected static function emojiForString(string $input) : string {
  // Compute a cheap and reproducible float between 0 and 1 for based on the
  // component ID.
  $max_length = 40;
  $input = strtolower($input);
  $input = strtr($input, '-_:', '000');
  $input = substr($input, 0, $max_length);
  $chars = str_split($input);
  $chars = array_pad($chars, 20, '0');
  $sum = array_reduce($chars, static fn(int $total, string $char) => $total + ord($char), 0);
  $num = $sum / 4880;
  // Compute an int between 129338 and 129431, which is the sequential emoji
  // range we are interested in. We chose this range because all integers in
  // between correspond to an emoji. These emojis depict sports, food, and
  // animals.
  $html_entity = floor(129338 + $num * (129431 - 129338));
  $emoji = mb_convert_encoding("&#{$html_entity};", 'UTF-8', 'HTML-ENTITIES');
  return is_string($emoji) ? $emoji : '';
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.