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