LocaleId   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 11
dl 0
loc 59
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assert_is_available() 0 4 2
A is_available() 0 7 2
A of() 0 11 2
A __construct() 0 3 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