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

Text   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A module() 0 11 4
A findString() 0 4 4
B loadStrings() 0 20 7
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
}