Normalizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 17 3
A normalizeList() 0 5 1
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