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

Normalize   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
eloc 59
c 1
b 0
f 0
dl 0
loc 188
rs 10
ccs 62
cts 62
cp 1

11 Methods

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