Skip to content

Commit 7a13920

Browse files
committed
Check for mbstring extension on alternative encodings
1 parent 3a9cc61 commit 7a13920

File tree

1 file changed

+8
-83
lines changed

1 file changed

+8
-83
lines changed

src/Encoding/Encoding.php

+8-83
Original file line numberDiff line numberDiff line change
@@ -6,92 +6,17 @@
66

77
final class Encoding implements EncodingInterface
88
{
9-
private const ENCODINGS = [
10-
'BASE64',
11-
'UUENCODE',
12-
'HTML-ENTITIES',
13-
'Quoted-Printable',
14-
'7bit',
15-
'8bit',
16-
'UCS-4',
17-
'UCS-4BE',
18-
'UCS-4LE',
19-
'UCS-2',
20-
'UCS-2BE',
21-
'UCS-2LE',
22-
'UTF-32',
23-
'UTF-32BE',
24-
'UTF-32LE',
25-
'UTF-16',
26-
'UTF-16BE',
27-
'UTF-16LE',
28-
'UTF-8',
29-
'UTF-7',
30-
'UTF7-IMAP',
31-
'ASCII',
32-
'EUC-JP',
33-
'SJIS',
34-
'eucJP-win',
35-
'EUC-JP-2004',
36-
'SJIS-Mobile#DOCOMO',
37-
'SJIS-Mobile#KDDI',
38-
'SJIS-Mobile#SOFTBANK',
39-
'SJIS-mac',
40-
'SJIS-2004',
41-
'UTF-8-Mobile#DOCOMO',
42-
'UTF-8-Mobile#KDDI-A',
43-
'UTF-8-Mobile#KDDI-B',
44-
'UTF-8-Mobile#SOFTBANK',
45-
'CP932',
46-
'SJIS-win',
47-
'CP51932',
48-
'JIS',
49-
'ISO-2022-JP',
50-
'ISO-2022-JP-MS',
51-
'GB18030',
52-
'Windows-1252',
53-
'Windows-1254',
54-
'ISO-8859-1',
55-
'ISO-8859-2',
56-
'ISO-8859-3',
57-
'ISO-8859-4',
58-
'ISO-8859-5',
59-
'ISO-8859-6',
60-
'ISO-8859-7',
61-
'ISO-8859-8',
62-
'ISO-8859-9',
63-
'ISO-8859-10',
64-
'ISO-8859-13',
65-
'ISO-8859-14',
66-
'ISO-8859-15',
67-
'ISO-8859-16',
68-
'EUC-CN',
69-
'CP936',
70-
'HZ',
71-
'EUC-TW',
72-
'BIG-5',
73-
'CP950',
74-
'EUC-KR',
75-
'UHC',
76-
'ISO-2022-KR',
77-
'Windows-1251',
78-
'CP866',
79-
'KOI8-R',
80-
'KOI8-U',
81-
'ArmSCII-8',
82-
'CP850',
83-
'ISO-2022-JP-2004',
84-
'ISO-2022-JP-MOBILE#KDDI',
85-
'CP50220',
86-
'CP50221',
87-
'CP50222',
88-
];
89-
909
public function __construct(
9110
private readonly string $value
9211
) {
93-
if (!in_array($value, self::ENCODINGS)) {
94-
throw new \Exception(sprintf('Invalid encoding "%s": choose one of '.implode(', ', self::ENCODINGS), $value));
12+
if ('UTF-8' !== $value) {
13+
if (!function_exists('mb_list_encodings')) {
14+
throw new \Exception('Unable to validate encoding: make sure the mbstring extension is installed and enabled');
15+
}
16+
17+
if (!in_array($value, mb_list_encodings())) {
18+
throw new \Exception(sprintf('Invalid encoding "%s": choose one of '.implode(', ', mb_list_encodings()), $value));
19+
}
9520
}
9621
}
9722

0 commit comments

Comments
 (0)