Completed
Push — master ( 6849bf...8877c9 )
by Aitor Riba
01:59
created

Builder::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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(config('localizer.allowed_langs'));
18
        } else {
19
            return self::addNames([config('localizer.default_lang')]);
20
        }
21
    }
22
23
    /**
24
     * Add names to an array of language codes as [$code => $language].
25
     *
26
     * @param array $codes
27
     *
28
     * @return array
29
     **/
30 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...
31
    {
32
        // Read and decode JSON
33
        $json_data = json_decode(file_get_contents(__DIR__.'/languages.json'), true);
34
35
        $array = [];
36
37
        // Generate an array with $code as key and $code language as value
38
        foreach ($codes as $code) {
39
            $lang_name = 'Unknoun';
40
            foreach ($json_data as $lang_data) {
41
                if ($lang_data['code'] == $code) {
42
                    $lang_name = $lang_data['name'];
43
                }
44
            }
45
            $array[$code] = $lang_name;
46
        }
47
48
        return $array;
49
    }
50
51
    /**
52
     * Add names to an array of language codes as [$language => $code].
53
     *
54
     * @param array $langs
55
     *
56
     * @return array
57
     **/
58 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...
59
    {
60
        // Read and decode JSON
61
        $json_data = json_decode(file_get_contents(__DIR__.'/languages.json'), true);
62
63
        $array = [];
64
65
        // Generate an array with $lang as key and $lang code as value
66
        foreach ($langs as $lang) {
67
            $code = 'unk';
68
            foreach ($json_data as $lang_data) {
69
                if ($lang_data['name'] == $lang) {
70
                    $code = $lang_data['code'];
71
                }
72
            }
73
            $array[$lang] = $code;
74
        }
75
76
        return $array;
77
    }
78
79
    /**
80
     * Returns the url to set up language and return back.
81
     *
82
     * @param string $code
83
     *
84
     * @return string
85
     **/
86
    public static function setRoute($code)
87
    {
88
        return route('localizer::setLocale', ['locale' => $code]);
89
    }
90
91
    /**
92
     * Returns the url to set up language and return to url('/').
93
     *
94
     * @param string $code
95
     *
96
     * @return string
97
     **/
98
    public static function setRouteHome($code)
99
    {
100
        return route('localizer::setLocaleHome', ['locale' => $code]);
101
    }
102
103
    /**
104
     * Returns the current language code.
105
     *
106
     * @return string
107
     **/
108
    public static function getCode($name = self::getLanguage())
109
    {
110
        return self::addCodes([$name])[$name];
111
    }
112
113
    /**
114
     * Returns the language name.
115
     *
116
     * @return string
117
     **/
118
    public static function getLanguage($code = App::getLocale())
119
    {
120
        return self::addNames([$code])[$code];
121
    }
122
}
123