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