Completed
Push — master ( 168210...51209a )
by Aitor Riba
02:01
created

Builder::setRouteHome()   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
     * 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 the url to set up language and return back.
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 url to set up language and return to url('/').
68
     *
69
     * @param string $code
70
     *
71
     * @return string
72
     */
73
    public static function setRouteHome($code)
74
    {
75
        return route('localizer::setLocaleHome', ['locale' => $code]);
76
    }
77
78
    /**
79
     * Returns  the current language code.
80
     *
81
     * @param string $ucfirst
82
     *
83
     * @return string
84
     */
85
    public static function getCurrentCode($ucfirst = false)
86
    {
87
        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...
88
            return ucfirst(App::getLocale());
89
        }
90
91
        return App::getLocale();
92
    }
93
94
    /**
95
     * Returns  the current language name.
96
     *
97
     * @return string
98
     */
99
    public static function getCurrentLanguage()
100
    {
101
        return self::addNames([self::getCurrentCode()])[self::getCurrentCode()];
102
    }
103
104
    /**
105
     * Returns the language name.
106
     *
107
     * @param string $code
108
     *
109
     * @return string
110
     */
111
    public static function getLanguage($code)
112
    {
113
        return self::addNames([$code])[$code];
114
    }
115
}
116