Passed
Push — latest ( c5296c...5fe1f6 )
by Mark
02:28
created

Emoji::getShortcodes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji;
6
7
/**
8
 * @property ?string $annotation
9
 * @property ?string $emoji
10
 * @property ?string $emoticon
11
 * @property ?int $gender
12
 * @property ?int $group
13
 * @property ?string $hexcode
14
 * @property ?string $htmlEntity
15
 * @property ?int $order
16
 * @property ?string $shortcode
17
 * @property string[] $shortcodes
18
 * @property Dataset $skins
19
 * @property ?int $subgroup
20
 * @property string[] $tags
21
 * @property ?string $text
22
 * @property int[] $tone
23
 * @property int $type
24
 * @property ?float $version
25
 */
26
final class Emoji extends ImmutableArrayIterator implements \Stringable
27
{
28
    public const PROPERTY_TYPES = [
29
        'annotation' => '!?string',
30
        'emoji' => '!?string',
31
        'emoticon' => '!?string',
32
        'gender' => '?int',
33
        'group' => '?int',
34
        'hexcode' => '!?string',
35
        'order' => '?int',
36
        'shortcodes' => '\UnicornFail\Emoji\Normalize::shortcodes[]',
37
        'skins' => '\UnicornFail\Emoji\Dataset',
38
        'subgroup' => '?int',
39
        'tags' => 'string[]',
40
        'text' => '!?string',
41
        'tone' => 'int[]',
42
        'type' => 'int',
43
        'version' => '!?float',
44
    ];
45
46
    /**
47
     * @param mixed[] $data
48
     */
49 33
    public function __construct(array $data = [])
50
    {
51 33
        parent::__construct(Normalize::properties($data, self::PROPERTY_TYPES));
52 33
    }
53
54 3
    public function __toString(): string
55
    {
56 3
        return $this->getUnicode() ?: '';
57
    }
58
59 18
    public function getHtmlEntity(): ?string
60
    {
61 18
        $hexcode = $this->hexcode;
62
63 18
        return $hexcode ? '&#x' . \implode(';&#x', \explode('-', $hexcode)) . ';' : null;
64
    }
65
66
    /**
67
     * @param string[]|null $exclude
68
     */
69 39
    public function getShortcode(?array $exclude = null, bool $wrap = false): ?string
70
    {
71 39
        $shortcode = \current($this->getShortcodes($exclude));
72
73 39
        if ($wrap && $shortcode) {
74 33
            $shortcode = \sprintf(':%s:', $shortcode);
75
        }
76
77 39
        return $shortcode ?: null;
78
    }
79
80
    /**
81
     * @param string[]|null $exclude
82
     *
83
     * @return string[]
84
     */
85 39
    public function getShortcodes(?array $exclude = null): array
86
    {
87 39
        $shortcodes = (array) $this->offsetGet('shortcodes');
88
89 39
        return $exclude ? \array_diff($shortcodes, $exclude) : $shortcodes;
90
    }
91
92 6
    public function getSkin(int $tone = EmojibaseSkinsInterface::LIGHT_SKIN): ?Emoji
93
    {
94 6
        return \current(
95 6
            $this->skins->filter(
96
                static function (Emoji $emoji) use ($tone) {
97 3
                    return \in_array($tone, (array) $emoji->tone, true);
98 6
                }
99 6
            )->getArrayCopy()
100 6
        ) ?: null;
101
    }
102
103 9
    public function getUnicode(): ?string
104
    {
105 9
        return $this->type === EmojibaseInterface::EMOJI && ($emoji = $this->emoji) ? $emoji : $this->text;
106
    }
107
}
108