1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
|
|
|
4
|
|
|
// 02.03.23 |
5
|
|
|
namespace AlecRabbit\WCWidth\Kernel; |
6
|
|
|
|
7
|
|
|
use AlecRabbit\WCWidth\Kernel\Contract\IUnicodeVersion; |
|
|
|
|
8
|
|
|
|
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
use function in_array; |
12
|
|
|
|
13
|
|
|
use const AlecRabbit\WCWidth\UNICODE_VERSIONS; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** @internal */ |
16
|
|
|
final class UnicodeVersion implements IUnicodeVersion |
17
|
|
|
{ |
18
|
|
|
private static ?string $useVersion = null; |
19
|
|
|
|
20
|
|
|
// @codeCoverageIgnoreStart |
21
|
|
|
private function __construct() |
22
|
|
|
{ |
23
|
|
|
// no instances |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// @codeCoverageIgnoreEnd |
27
|
|
|
|
28
|
|
|
public static function setVersion(string $version): void |
29
|
|
|
{ |
30
|
|
|
self::$useVersion = self::doRefine($version); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private static function doRefine(?string $version): string |
34
|
|
|
{ |
35
|
|
|
if ('latest' === $version || null === $version) { |
36
|
|
|
$version = self::latest(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
self::assertVersion($version); |
40
|
|
|
|
41
|
|
|
return |
42
|
|
|
$version; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function latest(): string |
46
|
|
|
{ |
47
|
|
|
return |
48
|
|
|
UNICODE_VERSIONS[array_key_last(UNICODE_VERSIONS)]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private static function assertVersion(string $version): void |
52
|
|
|
{ |
53
|
|
|
if (!in_array($version, UNICODE_VERSIONS, true)) { |
54
|
|
|
throw new InvalidArgumentException( |
55
|
|
|
sprintf( |
56
|
|
|
'Unknown Unicode version: %s', |
57
|
|
|
$version |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function getVersion(): ?string |
64
|
|
|
{ |
65
|
|
|
return self::$useVersion; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function reset(): void |
69
|
|
|
{ |
70
|
|
|
self::$useVersion = null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function refine(?string $version): string |
74
|
|
|
{ |
75
|
|
|
if (self::$useVersion) { |
76
|
|
|
return self::$useVersion; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return self::doRefine($version); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |