Completed
Push — master ( ad2687...4cd033 )
by Restu
14:41
created

Lang::tryExplodeName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 9
rs 9.6666
1
<?php
2
namespace JayaCode\Framework\Core\Lang;
3
4
/**
5
 * Class Lang
6
 * @package JayaCode\Framework\Core\Lang
7
 */
8
class Lang
9
{
10
    /**
11
     * @var string
12
     */
13
    protected static $locale = 'eng';
14
    /**
15
     * @var string
16
     */
17
    protected static $localeDefault = "eng";
18
19
    /**
20
     * @var string
21
     */
22
    protected static $langDir = '/';
23
24
    /**
25
     * @var array
26
     */
27
    protected static $lang = [];
28
29
    /**
30
     * @param $locale
31
     */
32
    public static function setLocale($locale)
33
    {
34
        static::$locale = $locale;
35
    }
36
37
    /**
38
     * @param $dir
39
     * @throws \Exception
40
     */
41
    public static function setDir($dir)
42
    {
43
        static::$langDir = rtrim($dir, "/")."/";
44
45
        if (!is_dir(static::$langDir)) {
46
            throw new \Exception("folder lang not found");
47
        }
48
    }
49
50
    /**
51
     * @param $name
52
     * @param array $params
53
     * @param string $default
54
     * @return mixed|null|string
55
     */
56
    public static function get($name, $params = [], $default = "")
57
    {
58
        $nameArr = static::tryExplodeName($name);
59
60
        if (null !== $return = static::getFromArray(static::$lang, $nameArr, $params, null, false)) {
61
            return $return;
62
        }
63
64 View Code Duplication
        if (null !== $return = static::getFromFile($nameArr, $params, null, false)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
65
            return $return;
66
        }
67
68
        $nameArrDef = $nameArr;
69
        $nameArrDef[0] = static::$localeDefault;
70
71 View Code Duplication
        if (null !== $return = static::getFromArray(static::$lang, $nameArrDef, $params, null, false)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
72
            return $return;
73
        }
74 View Code Duplication
        if (null !== $return = static::getFromFile($nameArrDef, $params, null, false)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
            return $return;
76
        }
77
78
        return $default;
79
    }
80
81
    /**
82
     * @param $arr
83
     * @param $name
84
     * @param array $params
85
     * @param null $default
86
     * @param bool $getFormDefault
87
     * @return mixed|null
88
     */
89
    public static function getFromArray($arr, $name, $params = [], $default = null, $getFormDefault = true)
90
    {
91
        $name = static::tryExplodeName($name);
92
93
        $result = static::searchArrayWithNameArray($arr, $name);
0 ignored issues
show
Bug introduced by
It seems like $name defined by static::tryExplodeName($name) on line 91 can also be of type string; however, JayaCode\Framework\Core\...rchArrayWithNameArray() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
94
95
        if ($getFormDefault && $result === null && static::$localeDefault != static::$locale) {
96
            $name[0] = static::$localeDefault;
97
            $result = static::searchArrayWithNameArray($arr, $name);
0 ignored issues
show
Bug introduced by
It seems like $name defined by static::tryExplodeName($name) on line 91 can also be of type string; however, JayaCode\Framework\Core\...rchArrayWithNameArray() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
98
        }
99
100
        return static::getFromString(is_string($result)?$result:$default, $params);
101
    }
102
103
    /**
104
     * @param $name
105
     * @param array $params
106
     * @param null $default
107
     * @param bool $getFormDefault
108
     * @return mixed|null
109
     */
110
    public static function getFromFile($name, $params = [], $default = null, $getFormDefault = true)
111
    {
112
        $nameArr = static::tryExplodeName($name);
113
114
        static::$lang[$nameArr[0]][$nameArr[1]] = static::loadFile($nameArr[0]."/".$nameArr[1].".php");
115
116
        return static::getFromArray(static::$lang, $nameArr, $params, $default, $getFormDefault);
117
    }
118
119
    /**
120
     * @param $string
121
     * @param array $params
122
     * @return mixed|null
123
     */
124
    public static function getFromString($string, $params = [])
125
    {
126
        if ($string === null) {
127
            return null;
128
        }
129
130
        foreach ($params as $key => $value) {
131
            $string = str_replace("{" . $key . "}", $value, $string);
132
        }
133
134
        return $string;
135
    }
136
137
    /**
138
     * @param $name
139
     * @return array|string
140
     */
141
    protected static function tryExplodeName($name)
142
    {
143
        if (is_string($name)) {
144
            $name = static::$locale.".".$name;
145
            return explode(".", $name);
146
        }
147
148
        return $name;
149
    }
150
151
    /**
152
     * @param array $arrLang
153
     * @param array $nameArr
154
     * @return array|mixed|null
155
     */
156
    protected static function searchArrayWithNameArray(array $arrLang, array $nameArr)
157
    {
158
        $result = $arrLang;
159
        for ($i = 0; $i < count($nameArr) && is_array($result); $i++) {
160
            $result = array_key_exists($nameArr[$i], $result)?$result[$nameArr[$i]]:null;
161
        }
162
163
        return $result;
164
    }
165
166
    /**
167
     * @param $filename
168
     * @return array|mixed
169
     */
170
    protected static function loadFile($filename)
171
    {
172
        $locFile = static::$langDir.$filename;
173
        return file_exists($locFile)?include($locFile):[];
174
    }
175
}
176