Test Failed
Push — master ( cb1b37...395f42 )
by Alexey
04:51
created

Text::module()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 6
nop 5
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by IntelliJ IDEA.
4
 * User: inji
5
 * Date: 04.11.2017
6
 * Time: 15:40
7
 */
8
9
namespace I18n;
10
class Text extends \Object {
11
    public static $strings = [];
12
13
    public static function module($module, $code, $params = [], $default = false, $lang = false) {
14
        $paramsKeys = array_keys($params);
15
        foreach ($paramsKeys as &$paramsKey) {
16
            $paramsKey = '${' . $paramsKey . '}';
17
        }
18
        $text = static::findString($module, $code, $lang);
19
        if ($text === false) {
20
            $text = $default !== false ? $default : $code;
21
        }
22
        return str_replace($paramsKeys, $params, $text);
23
    }
24
25
    public static function findString($module, $code, $lang = false) {
26
        $lang = $lang ? $lang : \App::$cur->i18n->lang();
27
        return static::loadStrings($module, $lang) && isset(static::$strings[$module][$lang][$code]) ? static::$strings[$module][$lang][$code] : false;
28
    }
29
30
    public static function loadStrings($module, $lang = false) {
31
        $lang = $lang ? $lang : \App::$cur->i18n->lang();
32
        $modulePaths = array_reverse(\Module::getModulePaths($module), true);
33
        if (isset(static::$strings[$module][$lang])) {
34
            return true;
35
        }
36
        if (!isset(static::$strings[$module])) {
37
            static::$strings[$module] = [];
38
        }
39
        foreach ($modulePaths as $modulePath) {
40
            if (file_exists($modulePath . '/i18n/' . $lang . '.php')) {
41
                if (!isset(static::$strings[$module][$lang])) {
42
                    static::$strings[$module][$lang] = include $modulePath . '/i18n/' . $lang . '.php';
43
                } else {
44
                    static::$strings[$module][$lang] = array_merge(static::$strings[$module][$lang], include $modulePath . '/i18n/' . $lang . '.php');
45
                }
46
            }
47
        }
48
        return isset(static::$strings[$module][$lang]);
49
    }
50
}