Normalizer::normalizeList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lochmueller\LanguageDetection\Service;
6
7
use Lochmueller\LanguageDetection\Domain\Collection\LocaleCollection;
8
use Lochmueller\LanguageDetection\Domain\Model\Dto\LocaleValueObject;
9
10
class Normalizer
11
{
12
    public function normalizeList(LocaleCollection $locales): LocaleCollection
13
    {
14
        $result = array_map(fn(LocaleValueObject $locale): string => $this->normalize((string)$locale), $locales->toArray());
15
16
        return LocaleCollection::fromArray($result);
17
    }
18
19
    public function normalize(string $locale): string
20
    {
21
        // Drop charset
22
        $pos = strpos($locale, '.');
23
        if ($pos !== false) {
24
            $locale = substr($locale, 0, $pos);
25
        }
26
27
        $code = str_replace('-', '_', $locale);
28
        $parts = explode('_', $code);
29
30
        $locale = strtolower($parts[0]);
31
        if (isset($parts[1])) {
32
            $locale .= '_' . strtoupper($parts[1]);
33
        }
34
35
        return $locale;
36
    }
37
}
38