LocaleId::assert_is_available()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace ICanBoogie\CLDR\Core;
4
5
final class LocaleId
6
{
7
    /**
8
     * Whether a locale ID is available.
9
     *
10
     * @param string $value
11
     *     A locale identifier; for example, fr-BE
12
     */
13
    public static function is_available(string $value): bool
14
    {
15
        if (isset(LocaleData::PARENT_LOCALES[$value])) {
16
            return true;
17
        }
18
19
        return in_array($value, LocaleData::AVAILABLE_LOCALES);
20
    }
21
22
    /**
23
     * @param string $value
24
     *     A locale identifier; for example, fr-BE
25
     *
26
     * @throws LocaleNotAvailable
27
     */
28
    public static function assert_is_available(string $value): void
29
    {
30
        self::is_available($value)
31
            or throw new LocaleNotAvailable($value);
32
    }
33
34
    /**
35
     * Returns a {@see LocaleId} of a value.
36
     *
37
     * Note: If the locale has a parent locale, that locale is used instead.
38
     *
39
     * @param string $value
40
     *     A locale identifier; for example, fr-BE
41
     *
42
     * @throws LocaleNotAvailable
43
     */
44
    public static function of(string $value): self
45
    {
46
        static $instances;
47
48
        self::assert_is_available($value);
49
50
        if (isset(LocaleData::PARENT_LOCALES[$value])) {
51
            $value = LocaleData::PARENT_LOCALES[$value];
52
        }
53
54
        return $instances[$value] ??= new self($value);
55
    }
56
57
    /**
58
     * @param string $value
59
     *     A locale identifier; for example, fr-BE.
60
     */
61
    private function __construct(
62
        public readonly string $value,
63
    ) {
64
    }
65
}
66