Passed
Push — master ( 03a489...acb598 )
by Dispositif
07:31
created

Language::all2wiki()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.0291

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 15
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 29
ccs 12
cts 13
cp 0.9231
crap 8.0291
rs 8.4444
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019/2020 © Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Domain\Enums;
11
12
/**
13
 * Convert language name in wiki lang code.
14
 * Class Lang.
15
 */
16
abstract class Language
17
{
18
    /**
19
     * only static call.
20
     */
21
    private function __construct()
22
    {
23
    }
24
25 28
    public static function all2wiki(?string $lang): ?string
26
    {
27 28
        if (empty($lang)) {
28
            return null;
29
        }
30 28
        $lower = mb_strtolower($lang);
31 28
        if (self::isWikiLang($lower)) {
32 12
            return $lower;
33
        }
34
35 16
        if ($dat = self::iso2b2wiki($lang)) {
36 3
            return $dat;
37
        }
38
39 13
        if ($dat = self::english2wiki($lang)) {
40 4
            return $dat;
41
        }
42
43 9
        if ($dat = self::longFrench2wiki($lang)) {
44 8
            return $dat;
45
        }
46
47 1
        if ($dat = self::html2iso639($lang)) {
48
            if (self::isWikiLang($dat)) {
49
                return $dat;
50
            }
51
        }
52
53
        return null;
54
    }
55 28
56
    /**
57
     * @param string $lang
58
     *
59
     * @return bool
60 28
     */
61 12
    private static function isWikiLang(string $lang): bool
62
    {
63
        /*
64 16
         * @var $liste_frlang []
65
         */
66
        if (LanguageData::LANG_FRWIKI && in_array($lang, LanguageData::LANG_FRWIKI)) {
67 17
            return true;
68
        }
69 17
70 17
        return false;
71 5
    }
72 4
73
    /**
74
     * HTML lang attribute : 'en-us' => 'en' (ISO 639-1)
75
     */
76 13
    public static function html2iso639(string $lang): ?string
77
    {
78
        if (preg_match('#^([a-z]{2})-[a-z]{2}+$#i', $lang, $matches)) {
79 17
            return strtolower($matches[1]);
80
        }
81 17
82 17
        return null;
83 16
    }
84
85
    public static function iso2b2wiki(string $lang): ?string
86 1
    {
87
        $lang = strtolower($lang);
88
        if (LanguageData::ISO2B_TO_FRENCH && array_key_exists($lang, LanguageData::ISO2B_TO_FRENCH)) {
89 13
            if (!empty(LanguageData::ISO2B_TO_FRENCH[$lang])) {
90
                return self::longFrench2wiki(LanguageData::ISO2B_TO_FRENCH[$lang]);
91 13
            }
92 13
        }
93 3
94
        return null;
95
    }
96 10
97 10
    private static function longFrench2wiki(string $lang): ?string
98 1
    {
99
        $lang = mb_strtolower($lang);
100
        if (LanguageData::FRENCH_TO_FRWIKI && array_key_exists($lang, LanguageData::FRENCH_TO_FRWIKI)) {
101 9
            return LanguageData::FRENCH_TO_FRWIKI[$lang];
102
        }
103
104
        return null;
105
    }
106
107
    public static function english2wiki(string $lang): ?string
108
    {
109
        $lang = ucfirst($lang);
110
        if (LanguageData::ENGLISH_TO_FRENCH && array_key_exists($lang, LanguageData::ENGLISH_TO_FRENCH)) {
111
            return self::longFrench2wiki(LanguageData::ENGLISH_TO_FRENCH[$lang]);
112
        }
113
        // ugly
114
        $lang = ucfirst(mb_strtolower($lang));
115
        if (LanguageData::ENGLISH_TO_FRENCH && array_key_exists($lang, LanguageData::ENGLISH_TO_FRENCH)) {
116
            return self::longFrench2wiki(LanguageData::ENGLISH_TO_FRENCH[$lang]);
117
        }
118
119
        return null;
120
    }
121
}
122