Failed Conditions
Push — master ( a2282e...ad0b68 )
by Mark
40s queued 35s
created

Locale::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
rs 10
cc 3
nc 3
nop 2
crap 3.0261
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
4
5
use NumberFormatter;
6
use PhpOffice\PhpSpreadsheet\Exception;
7
8
final class Locale
9
{
10
    /**
11
     * Language code: ISO-639 2 character, alpha.
12
     * Optional script code: ISO-15924 4 alpha.
13
     * Optional country code: ISO-3166-1, 2 character alpha.
14
     * Separated by underscores or dashes.
15
     */
16
    public const STRUCTURE = '/^(?P<language>[a-z]{2})([-_](?P<script>[a-z]{4}))?([-_](?P<country>[a-z]{2}))?$/i';
17
18
    private NumberFormatter $formatter;
19
20 28
    public function __construct(string $locale, int $style)
21
    {
22 28
        if (class_exists(NumberFormatter::class) === false) {
23
            throw new Exception();
24
        }
25
26 28
        $formatterLocale = str_replace('-', '_', $locale);
27 28
        $this->formatter = new NumberFormatter($formatterLocale, $style);
28 28
        if ($this->formatter->getLocale() !== $formatterLocale) {
29 3
            throw new Exception("Unable to read locale data for '{$locale}'");
30
        }
31
    }
32
33 25
    public function format(): string
34
    {
35 25
        return $this->formatter->getPattern();
36
    }
37
}
38