Builder::addNames()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 10

Duplication

Lines 22
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 22
loc 22
rs 8.9197
cc 4
eloc 10
nc 4
nop 1
1
<?php
2
3
namespace Aitor24\Localizer;
4
5
use Illuminate\Support\Facades\App;
6
7
class Builder
8
{
9
    /**
10
     * Get all allowed languages.
11
     *
12
     * @return array
13
     **/
14
    public static function allowedLanguages()
15
    {
16
        if (config('localizer.allowed_langs')) {
17
            return self::addNames(array_merge(config('localizer.allowed_langs'), [config('localizer.default_lang')]));
18
        } else {
19
            return self::addNames([config('localizer.default_lang')]);
20
        }
21
    }
22
23
    /**
24
     * Return true if $code is an allowed lang.
25
     *
26
     * @return bool
27
     **/
28
    public static function isAllowedLanguage($code)
29
    {
30
        return in_array($code, array_keys(self::allowedLanguages()));
31
    }
32
33
    /**
34
     * Add names to an array of language codes as [$code => $language].
35
     *
36
     * @param array $codes
37
     *
38
     * @return array
39
     **/
40 View Code Duplication
    public static function addNames($codes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        // Get languages from config
43
        $languages = config('localizer_languages.languages');
44
45
        $array = [];
46
47
        // Generate an array with $code as key and $code language as value
48
        foreach ($codes as $code) {
49
            $lang_name = 'Unknown';
50
51
            foreach ($languages as $language) {
52
                if ($language['code'] == $code) {
53
                    $lang_name = $language['name'];
54
                }
55
            }
56
57
            $array[$code] = $lang_name;
58
        }
59
60
        return $array;
61
    }
62
63
    /**
64
     * Add names to an array of language codes as [$language => $code].
65
     *
66
     * @param array $langs
67
     *
68
     * @return array
69
     **/
70 View Code Duplication
    public static function addCodes($langs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        // Get languages from config
73
        $languages = config('localizer_languages.languages');
74
75
        $array = [];
76
77
        // Generate an array with $lang as key and $lang code as value
78
        foreach ($langs as $lang) {
79
            $lang_code = 'unk';
80
81
            foreach ($languages as $language) {
82
                if ($language['name'] == $lang) {
83
                    $lang_code = $language['code'];
84
                }
85
            }
86
87
            $array[$lang] = $lang_code;
88
        }
89
90
        return $array;
91
    }
92
93
    /**
94
     * Returns the url to set up language and return back.
95
     *
96
     * @param string $code
97
     *
98
     * @return string
99
     **/
100
    public static function setRoute($code)
101
    {
102
        return route('localizer::setLocale', ['locale' => $code]);
103
    }
104
105
    /**
106
     * Returns the url to set up language and return to url('/').
107
     *
108
     * @param string $code
109
     *
110
     * @return string
111
     **/
112
    public static function setRouteHome($code)
113
    {
114
        return route('localizer::setLocaleHome', ['locale' => $code]);
115
    }
116
117
    /**
118
     * Returns the current language code.
119
     *
120
     * @return string
121
     **/
122
    public static function getCode($name = 'default')
123
    {
124
        if ($name == 'default') {
125
            $name = self::getLanguage();
126
        }
127
128
        return self::addCodes([$name])[$name];
129
    }
130
131
    /**
132
     * Returns the language name.
133
     *
134
     * @return string
135
     **/
136
    public static function getLanguage($code = 'default')
137
    {
138
        if ($code == 'default') {
139
            $code = App::getLocale();
140
        }
141
142
        return self::addNames([$code])[$code];
143
    }
144
}
145