Support   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 12
eloc 18
c 2
b 0
f 1
dl 0
loc 43
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setToArray() 0 3 1
A fillFromArray() 0 7 4
B fillFromPaths() 0 16 7
1
<?php
2
3
namespace kalanis\kw_langs;
4
5
6
use ArrayAccess;
7
use kalanis\kw_routed_paths\RoutedPath;
8
9
10
/**
11
 * Class Support
12
 * @package kalanis\kw_langs
13
 * Store translations through system runtime
14
 */
15
class Support
16
{
17
    public const LANG_KEY = 'lang';
18
19
    /**
20
     * @param ArrayAccess<string, mixed> $array
21
     * @param string|null $defaultLang
22
     * @return string|null
23
     */
24 1
    public static function fillFromArray(ArrayAccess $array, ?string $defaultLang): ?string
25
    {
26 1
        return $array->offsetExists(static::LANG_KEY)
27 1
        && !empty($array->offsetGet(static::LANG_KEY))
28 1
        && is_string($array->offsetGet(static::LANG_KEY))
29 1
            ? $array->offsetGet(static::LANG_KEY)
30 1
            : $defaultLang ;
31
    }
32
33
    /**
34
     * @param ArrayAccess<string, string> $array
35
     * @param string $lang
36
     */
37 1
    public static function setToArray(ArrayAccess $array, string $lang): void
38
    {
39 1
        $array->offsetSet(static::LANG_KEY, $lang);
40 1
    }
41
42 6
    public static function fillFromPaths(RoutedPath $path, string $defaultLang, bool $moreLangs): string
43
    {
44 6
        if ($path->getLang()) {
45 4
            return $path->getLang();
46
        }
47 3
        if ($moreLangs && !empty($path->getPath())) {
48 2
            $trace = $path->getPath();
49 2
            $firstDir = reset($trace);
50 2
            if (false !== $firstDir) {
51 2
                $length = strlen($firstDir);
52 2
                if ((1 < $length) && (4 > $length)) { // two-letter "en", three letter "eng"
53 2
                    return $firstDir;
54
                }
55
            }
56
        }
57 3
        return $defaultLang;
58
    }
59
}
60