Completed
Push — master ( 511693...0349ad )
by Dispositif
04:19
created

Language::isWikiLang()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 © 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
11
namespace App\Domain\Enums;
12
13
/**
14
 * Convert language name in wiki lang code.
15
 * Class Lang
16
 *
17
 * @package App\Domain\Enums
18
 */
19
abstract class Language
20
{
21
    const dataFile = __DIR__.'/languageData.php';
22
23
    /**
24
     * only static call
25
     */
26
    private function __construct() { }
27
28
    /**
29
     * @param string $lang
30
     *
31
     * @return bool
32
     */
33
    private static function isWikiLang(string $lang): bool
34
    {
35
        require self::dataFile;
36
        /**
37
         * @var $liste_frlang []
38
         */
39
        if (isset($liste_frlang) && in_array($lang, $liste_frlang)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $liste_frlang seems to never exist and therefore isset should always be false.
Loading history...
40
            return true;
41
        }
42
43
        return false;
44
    }
45
46
    public static function all2wiki(string $lang): ?string
47
    {
48
        $lower = mb_strtolower($lang);
49
        if (self::isWikiLang($lower)) {
50
            return $lower;
51
        }
52
53
        if ($dat = self::iso2b2wiki($lang)) {
54
            return $dat;
55
        }
56
57
        if ($dat = self::english2wiki($lang)) {
58
            return $dat;
59
        }
60
61
        if ($dat = self::longFrench2wiki($lang)) {
62
            return $dat;
63
        }
64
65
        return null;
66
    }
67
68
    public static function iso2b2wiki(string $lang): ?string
69
    {
70
        require self::dataFile;
71
        $lang = strtolower($lang);
72
        if (isset($iso2b_to_french) && array_key_exists($lang, $iso2b_to_french)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $iso2b_to_french seems to never exist and therefore isset should always be false.
Loading history...
73
            if (!empty($iso2b_to_french[$lang])) {
74
                return self::longFrench2wiki($iso2b_to_french[$lang]);
75
            }
76
        }
77
78
        return null;
79
    }
80
81
    public static function english2wiki(string $lang): ?string
82
    {
83
        require self::dataFile;
84
85
        $lang = ucfirst($lang);
86
        if (isset($english_to_french) && array_key_exists($lang, $english_to_french)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $english_to_french seems to never exist and therefore isset should always be false.
Loading history...
87
            return self::longFrench2wiki($english_to_french[$lang]);
88
        }
89
        // ugly
90
        $lang = ucfirst(mb_strtolower($lang));
91
        if (isset($english_to_french) && array_key_exists($lang, $english_to_french)) {
92
            return self::longFrench2wiki($english_to_french[$lang]);
93
        }
94
95
        return null;
96
    }
97
98
    private static function longFrench2wiki(string $lang): ?string
99
    {
100
        require self::dataFile;
101
        $lang = mb_strtolower($lang);
102
        if (isset($french_to_frlang) && array_key_exists($lang, $french_to_frlang)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $french_to_frlang seems to never exist and therefore isset should always be false.
Loading history...
103
            return $french_to_frlang[$lang];
104
        }
105
106
        return null;
107
    }
108
109
}
110