|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace UnicornFail\Emoji\Dataset; |
|
6
|
|
|
|
|
7
|
|
|
use UnicornFail\Emoji\Emojibase\DatasetInterface; |
|
|
|
|
|
|
8
|
|
|
use UnicornFail\Emoji\Emojibase\SkinsInterface; |
|
9
|
|
|
use UnicornFail\Emoji\Util\ImmutableArrayIterator; |
|
10
|
|
|
use UnicornFail\Emoji\Util\Normalize; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @property ?string $annotation |
|
14
|
|
|
* @property ?string $emoji |
|
15
|
|
|
* @property ?string $emoticon |
|
16
|
|
|
* @property ?int $gender |
|
17
|
|
|
* @property ?int $group |
|
18
|
|
|
* @property ?string $hexcode |
|
19
|
|
|
* @property ?string $htmlEntity |
|
20
|
|
|
* @property ?int $order |
|
21
|
|
|
* @property ?string $shortcode |
|
22
|
|
|
* @property string[] $shortcodes |
|
23
|
|
|
* @property Dataset $skins |
|
24
|
|
|
* @property ?int $subgroup |
|
25
|
|
|
* @property string[] $tags |
|
26
|
|
|
* @property ?string $text |
|
27
|
|
|
* @property int[] $tone |
|
28
|
|
|
* @property int $type |
|
29
|
|
|
* @property ?float $version |
|
30
|
|
|
*/ |
|
31
|
|
|
final class Emoji extends ImmutableArrayIterator implements \Stringable |
|
32
|
|
|
{ |
|
33
|
|
|
public const PROPERTY_TYPES = [ |
|
34
|
|
|
'annotation' => '!?string', |
|
35
|
|
|
'emoji' => '!?string', |
|
36
|
|
|
'emoticon' => '!?string', |
|
37
|
|
|
'gender' => '?int', |
|
38
|
|
|
'group' => '?int', |
|
39
|
|
|
'hexcode' => '!?string', |
|
40
|
|
|
'order' => '?int', |
|
41
|
|
|
'shortcodes' => 'string[]<\UnicornFail\Emoji\Util\Normalize::shortcodes>', |
|
42
|
|
|
'skins' => '\UnicornFail\Emoji\Dataset\Dataset', |
|
43
|
|
|
'subgroup' => '?int', |
|
44
|
|
|
'tags' => 'string[]', |
|
45
|
|
|
'text' => '!?string', |
|
46
|
|
|
'tone' => 'int[]', |
|
47
|
|
|
'type' => 'int', |
|
48
|
|
|
'version' => '!?float', |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param mixed[] $data |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(array $data = []) |
|
55
|
|
|
{ |
|
56
|
|
|
parent::__construct(Normalize::properties($data, self::PROPERTY_TYPES)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function __toString(): string |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->getUnicode() ?: ''; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getHtmlEntity(): ?string |
|
65
|
|
|
{ |
|
66
|
|
|
$hexcode = $this->hexcode; |
|
67
|
|
|
|
|
68
|
|
|
return $hexcode |
|
69
|
|
|
? '&#x' . \implode(';&#x', \explode('-', $hexcode)) . ';' |
|
70
|
|
|
: null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string[]|null $exclude |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getShortcode(?array $exclude = null, bool $wrap = false): ?string |
|
77
|
|
|
{ |
|
78
|
|
|
$shortcode = \current($this->getShortcodes($exclude)); |
|
79
|
|
|
|
|
80
|
|
|
if ($wrap && $shortcode) { |
|
81
|
|
|
$shortcode = \sprintf(':%s:', $shortcode); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $shortcode ?: null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param ?string[] $exclude |
|
89
|
|
|
* |
|
90
|
|
|
* @return string[] |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getShortcodes(?array $exclude = null): array |
|
93
|
|
|
{ |
|
94
|
|
|
/** @var string[] $shortcodes */ |
|
95
|
|
|
$shortcodes = (array) $this->offsetGet('shortcodes'); |
|
96
|
|
|
|
|
97
|
|
|
return $exclude |
|
98
|
|
|
? \array_diff($shortcodes, $exclude) |
|
99
|
|
|
: $shortcodes; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getSkin(int $tone = SkinsInterface::LIGHT_SKIN): ?self |
|
103
|
|
|
{ |
|
104
|
|
|
/** @var ?static $skin */ |
|
105
|
|
|
$skin = \current( |
|
106
|
|
|
$this->skins->filter( |
|
107
|
|
|
static function (Emoji $emoji) use ($tone) { |
|
108
|
|
|
return \in_array($tone, (array) $emoji->tone, true); |
|
109
|
|
|
} |
|
110
|
|
|
)->getArrayCopy() |
|
111
|
|
|
) ?: null; |
|
112
|
|
|
|
|
113
|
|
|
return $skin; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getUnicode(): ?string |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->type === DatasetInterface::EMOJI && ($emoji = $this->emoji) |
|
119
|
|
|
? $emoji |
|
120
|
|
|
: $this->text; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: