Passed
Push — master ( 302cec...c80fc2 )
by Alec
01:56 queued 14s
created

UnicodeVersion::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
0 ignored issues
show
Coding Style introduced by
Header blocks must be separated by a single blank line
Loading history...
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;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\WCWidth\UNICODE_VERSIONS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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
}