Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| UnicodeExtras | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| explode | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| initials | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\name\Utility; |
| 6 | |
| 7 | use Drupal\Component\Utility\Unicode; |
| 8 | |
| 9 | /** |
| 10 | * Provides custom Unicode-related extension methods. |
| 11 | * |
| 12 | * @ingroup utility |
| 13 | */ |
| 14 | class UnicodeExtras extends Unicode { |
| 15 | |
| 16 | /** |
| 17 | * Split each word in a UTF-8 string. |
| 18 | * |
| 19 | * @param string $text |
| 20 | * The text that will be converted. |
| 21 | * |
| 22 | * @return array |
| 23 | * The input $text as an array of words. |
| 24 | */ |
| 25 | public static function explode($text) { |
| 26 | $regex = '/(^|[' . static::PREG_CLASS_WORD_BOUNDARY . '])/u'; |
| 27 | $words = preg_split($regex, $text, -1, PREG_SPLIT_NO_EMPTY); |
| 28 | return $words; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Generate the initials of all first characters in a string. |
| 33 | * |
| 34 | * Note that this is case-insensitive, camel case words are treated as a |
| 35 | * single word. |
| 36 | * |
| 37 | * @param string $text |
| 38 | * The text that will be converted. |
| 39 | * @param string $delimiter |
| 40 | * An optional string to separate each character. |
| 41 | * |
| 42 | * @return string |
| 43 | * The input $text with first letters of each word capitalized. |
| 44 | */ |
| 45 | public static function initials($text, $delimiter = '') { |
| 46 | $text = mb_strtolower($text); |
| 47 | $results = []; |
| 48 | $words = array_filter(self::explode($text)); |
| 49 | foreach ($words as $word) { |
| 50 | $results[] = mb_substr($word, 0, 1); |
| 51 | } |
| 52 | $text = implode($delimiter, $results); |
| 53 | $text = mb_strtoupper($text); |
| 54 | return $text ? $text . $delimiter : ''; |
| 55 | } |
| 56 | |
| 57 | } |