Test Setup Failed
Pull Request — latest (#3)
by Mark
31:31
created

Normalize::locale()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 28
ccs 15
cts 15
cp 1
rs 9.4888
cc 5
nc 5
nop 1
crap 5

1 Method

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