Completed
Push — master ( b14e30...1c0d18 )
by Aitor Riba
01:51
created

Builder::getLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 ['en'];
18
        } else {
19
            return self::addNames(config('localizer.allowed_langs'));
20
        }
21
    }
22
23
    /**
24
     * Returns add names for arrays with only codes an return an array as [$code => $language].
25
     *
26
     * @param array $langs
27
     *
28
     * @return array
29
     */
30
    public static function addNames($langs)
31
    {
32
        // Read JSON file
33
        $json = file_get_contents(__DIR__.'/languages.json');
34
35
        //Decode JSON
36
        $json_data = json_decode($json, true);
37
38
        $array = [];
39
40
        //Generate an array with $lang code as key and name as value
41
        foreach ($langs as $lang) {
42
            $lang_name = 'Unknoun';
43
            foreach ($json_data as $lang_data) {
44
                if ($lang_data['code'] == $lang) {
45
                    $lang_name = $lang_data['name'];
46
                }
47
            }
48
            $array[$lang] = $lang_name;
49
        }
50
51
        return $array;
52
    }
53
54
    /**
55
     * Returns an string url to set up language.
56
     *
57
     * @param string $code
58
     *
59
     * @return string
60
     */
61
    public static function setRoute($code)
62
    {
63
        return route('localizer::setLocale', ['locale' => $code]);
64
    }
65
66
    /**
67
     * Returns  the current language code.
68
     *
69
     * @param string $ucfirst
70
     *
71
     * @return string
72
     */
73
    public static function getCurrentCode($ucfirst = false)
74
    {
75
        if ($ucfirst) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ucfirst of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
76
            return ucfirst(App::getLocale());
77
        }
78
79
        return App::getLocale();
80
    }
81
82
    /**
83
     * Returns  the current language name.
84
     *
85
     * @return string
86
     */
87
    public static function getCurrentLanguage()
88
    {
89
        return self::addNames([self::getCurrentCode()])[self::getCurrentCode()];
90
    }
91
92
    /**
93
     * Returns the language name.
94
     *
95
     * @param string $code
96
     *
97
     * @return string
98
     */
99
    public static function getLanguage($code)
100
    {
101
        return self::addNames([$code])[$code];
102
    }
103
}
104