forked from ddeboer/transcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMbTranscoder.php
More file actions
100 lines (83 loc) · 2.5 KB
/
MbTranscoder.php
File metadata and controls
100 lines (83 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
namespace Ddeboer\Transcoder;
use Ddeboer\Transcoder\Exception\ExtensionMissingException;
use Ddeboer\Transcoder\Exception\UndetectableEncodingException;
use Ddeboer\Transcoder\Exception\UnsupportedEncodingException;
class MbTranscoder implements TranscoderInterface
{
/**
* @var array<string, int>
*/
private static $encodings;
/**
* @var string
*/
private $defaultEncoding;
/**
* Create a Mb-based transcoder.
*
* @throws ExtensionMissingException
*/
public function __construct(string $defaultEncoding = 'UTF-8')
{
if (!function_exists('mb_convert_encoding')) {
throw new ExtensionMissingException('mb');
}
if (null === self::$encodings) {
self::$encodings = array_change_key_case(
array_flip(mb_list_encodings()),
CASE_LOWER
);
}
$this->assertSupported($defaultEncoding, false);
$this->defaultEncoding = $defaultEncoding;
}
/**
* {@inheritdoc}
*
* @param array<string>|string|null $from
*/
public function transcode(string $string, $from = null, ?string $to = null): string
{
if ($from) {
if (is_array($from)) {
array_map([$this, 'assertSupported'], $from);
} else {
$this->assertSupported($from);
}
} else {
$from = 'auto';
}
if ($to) {
$this->assertSupported($to, false);
}
if ($from === 'auto') {
$from = mb_detect_encoding($string, 'auto', true);
}
if ($from === false) {
throw new UndetectableEncodingException($string, 'Unable to detect character encoding');
}
$result = mb_convert_encoding(
$string,
$to ?: $this->defaultEncoding,
$from
);
// For PHPStan: We check the encoding is valid.
assert($result !== false);
return $result;
}
/**
* @throws UnsupportedEncodingException
*/
private function assertSupported(string $encoding, bool $allowAuto = true): void
{
if (!$this->isSupported($encoding, $allowAuto)) {
throw new UnsupportedEncodingException($encoding);
}
}
private function isSupported(string $encoding, bool $allowAuto): bool
{
return ($allowAuto && $encoding === 'auto') || isset(self::$encodings[strtolower($encoding)]);
}
}