|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace UnicornFail\Emoji\Util; |
|
6
|
|
|
|
|
7
|
|
|
use UnicornFail\Emoji\Emoji; |
|
8
|
|
|
use UnicornFail\Emoji\Emojibase\DatasetInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* {@internal} |
|
12
|
|
|
*/ |
|
13
|
|
|
final class Normalize |
|
14
|
|
|
{ |
|
15
|
|
|
public const TYPES = ['array', 'bool', 'boolean', 'double', 'float', 'int', 'integer', 'null', 'object', 'string']; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param mixed $emojis |
|
19
|
|
|
* @param Emoji[] $dataset |
|
20
|
|
|
* |
|
21
|
|
|
* @return Emoji[] |
|
22
|
|
|
*/ |
|
23
|
33 |
|
public static function dataset($emojis = [], string $index = 'hexcode', array &$dataset = []): array |
|
24
|
|
|
{ |
|
25
|
33 |
|
foreach (static::emojis($emojis) as $emoji) { |
|
26
|
18 |
|
$keys = \array_filter((array) $emoji->$index); |
|
27
|
18 |
|
foreach ($keys as $k) { |
|
28
|
18 |
|
if (isset($dataset[$k])) { |
|
29
|
3 |
|
continue; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
18 |
|
$dataset[$k] = $emoji; |
|
33
|
|
|
|
|
34
|
18 |
|
static::dataset($emoji->skins, $index, $dataset); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
33 |
|
return $dataset; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param mixed $emojis |
|
43
|
|
|
* |
|
44
|
|
|
* @return Emoji[] |
|
45
|
|
|
*/ |
|
46
|
33 |
|
public static function emojis($emojis = []): array |
|
47
|
|
|
{ |
|
48
|
33 |
|
if ($emojis instanceof Emoji) { |
|
49
|
9 |
|
$emojis = [$emojis]; |
|
50
|
33 |
|
} elseif ($emojis instanceof \Iterator) { |
|
51
|
21 |
|
$emojis = \iterator_to_array($emojis); |
|
52
|
|
|
} else { |
|
53
|
33 |
|
$emojis = (array) $emojis; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
33 |
|
foreach ($emojis as &$emoji) { |
|
57
|
18 |
|
if (\is_array($emoji)) { |
|
58
|
9 |
|
$emoji = new Emoji($emoji); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
18 |
|
if (! $emoji instanceof Emoji) { |
|
62
|
3 |
|
throw new \RuntimeException(\sprintf('Passed array item must be an instance of %s.', Emoji::class)); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
33 |
|
return (array) $emojis; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
699 |
|
public static function locale(string $locale): string |
|
70
|
|
|
{ |
|
71
|
|
|
// Immediately return if locale is an exact match. |
|
72
|
699 |
|
if (\in_array($locale, DatasetInterface::SUPPORTED_LOCALES, true)) { |
|
73
|
675 |
|
return $locale; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Immediately return if this local has already been normalized. |
|
77
|
24 |
|
static $normalized = []; |
|
78
|
24 |
|
if (isset($normalized[$locale])) { |
|
79
|
15 |
|
return $normalized[$locale]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
24 |
|
$original = $locale; |
|
83
|
24 |
|
$normalized[$original] = ''; |
|
84
|
|
|
|
|
85
|
|
|
// Otherwise, see if it just needs some TLC. |
|
86
|
24 |
|
$locale = \strtolower($locale); |
|
87
|
24 |
|
$locale = \preg_replace('/[^a-z]/', '-', $locale) ?? $locale; |
|
88
|
24 |
|
foreach ([$locale, \current(\explode('-', $locale, 2))] as $locale) { |
|
89
|
24 |
|
if (\in_array($locale, DatasetInterface::SUPPORTED_LOCALES, true)) { |
|
90
|
15 |
|
$normalized[$original] = $locale; |
|
91
|
15 |
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
24 |
|
return $normalized[$original]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param mixed[] $properties |
|
100
|
|
|
* @param string[] $types |
|
101
|
|
|
* |
|
102
|
|
|
* @return mixed[] |
|
103
|
|
|
*/ |
|
104
|
54 |
|
public static function properties(array $properties, array $types): array |
|
105
|
|
|
{ |
|
106
|
54 |
|
$properties += \array_fill_keys(\array_keys($types), null); |
|
107
|
54 |
|
foreach ($properties as $key => $value) { |
|
108
|
54 |
|
$type = $types[$key] ?? '?string'; |
|
109
|
54 |
|
$properties[$key] = Property::cast($type, $value); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
54 |
|
return $properties; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param mixed $value |
|
117
|
|
|
*/ |
|
118
|
90 |
|
public static function setType(&$value, string $type): bool |
|
119
|
|
|
{ |
|
120
|
90 |
|
static $methods = [ |
|
121
|
|
|
'array' => 'toArray', |
|
122
|
|
|
'bool' => 'toBoolean', |
|
123
|
|
|
'boolean' => 'toBoolean', |
|
124
|
|
|
'double' => 'toFloat', |
|
125
|
|
|
'float' => 'toFloat', |
|
126
|
|
|
'int' => 'toInteger', |
|
127
|
|
|
'integer' => 'toInteger', |
|
128
|
|
|
'object' => 'toObject', |
|
129
|
|
|
'string' => 'toString', |
|
130
|
|
|
]; |
|
131
|
|
|
|
|
132
|
|
|
// Immediately return if not a valid type. |
|
133
|
90 |
|
if (! isset($methods[$type])) { |
|
134
|
6 |
|
$value = null; |
|
135
|
|
|
|
|
136
|
6 |
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
84 |
|
$method = $methods[$type]; |
|
140
|
84 |
|
$value = static::$method($value); |
|
141
|
|
|
|
|
142
|
84 |
|
return true; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param string|string[] $shortcode |
|
147
|
|
|
* |
|
148
|
|
|
* @return string[] |
|
149
|
|
|
*/ |
|
150
|
123 |
|
public static function shortcodes($shortcode): array |
|
151
|
|
|
{ |
|
152
|
123 |
|
$normalized = []; |
|
153
|
123 |
|
foreach (\func_get_args() as $shortcodes) { |
|
154
|
123 |
|
$normalized = \array_values(\array_unique(\array_merge( |
|
155
|
123 |
|
$normalized, |
|
156
|
123 |
|
\array_map( |
|
157
|
|
|
static function ($shortcode) { |
|
158
|
123 |
|
return \preg_replace('/[^a-z0-9-]/', '-', \strtolower(\trim($shortcode, ':(){}[]'))); |
|
159
|
|
|
}, |
|
160
|
123 |
|
(array) $shortcodes |
|
161
|
|
|
) |
|
162
|
|
|
))); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
123 |
|
return \array_unique(\array_filter($normalized)); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param mixed $value |
|
170
|
|
|
* |
|
171
|
|
|
* @return mixed[] |
|
172
|
|
|
*/ |
|
173
|
3 |
|
public static function toArray($value): array |
|
174
|
|
|
{ |
|
175
|
3 |
|
return (array) ($value ?? []); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param mixed $value |
|
180
|
|
|
*/ |
|
181
|
12 |
|
public static function toBoolean($value): bool |
|
182
|
|
|
{ |
|
183
|
12 |
|
return (bool) ($value ?? false); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param mixed $value |
|
188
|
|
|
*/ |
|
189
|
45 |
|
public static function toFloat($value): float |
|
190
|
|
|
{ |
|
191
|
45 |
|
return (float) ($value ?? 0.0); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param mixed $value |
|
196
|
|
|
*/ |
|
197
|
48 |
|
public static function toInteger($value): int |
|
198
|
|
|
{ |
|
199
|
48 |
|
return (int) ($value ?? 0); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param mixed $value |
|
204
|
|
|
*/ |
|
205
|
3 |
|
public static function toObject($value): object |
|
206
|
|
|
{ |
|
207
|
3 |
|
return (object) ($value ?? new \stdClass()); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param mixed $value |
|
212
|
|
|
*/ |
|
213
|
39 |
|
public static function toString($value): string |
|
214
|
|
|
{ |
|
215
|
39 |
|
return \is_array($value) ? \implode($value) : (string) ($value ?? ''); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|