Test Failed
Push — docs ( 541d16...38ea2c )
by Mark
35:24
created

Normalize::emojis()   B

Complexity

Conditions 9
Paths 42

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9

Importance

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