Translator::trans()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: afshin
5
 * Date: 11/19/17
6
 * Time: 5:24 PM
7
 */
8
9
namespace Core\Translator;
10
11
use Interop\Container\ContainerInterface;
12
13
class Translator extends _TranslateHandler
14
{
15
    /**
16
     * @var ContainerInterface $setting
17
     *
18
    */
19
    public function init()
20
    {
21
        $setting = $this->settings['app']['translation'];
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist on Core\Translator\Translator. Since you implemented __get, consider adding a @property annotation.
Loading history...
22
        $this->local = $setting['default_lang'];
23
24
    }
25
26
    private function _loadkey(string $key , array $replace = [])
27
    {
28
        list($namespace,$group) = explode('.',$key);
29
        $t_dir = $this->getTranslation_dirs();
30
31
        if(in_array($this->local,$t_dir)){
32
            $translationBaseFolder = $this->settings['app']['translation']['translations_path'].$this->local;
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist on Core\Translator\Translator. Since you implemented __get, consider adding a @property annotation.
Loading history...
33
            $translationBaseFile = $translationBaseFolder.'/'.$namespace.'.php';
34
            if(file_exists($translationBaseFile)){
35
                $lang = include ($translationBaseFile);
36
            }else{
37
                return  $key;
38
            }
39
        }else{
40
        }
41
        $keyArr = explode('.',$key);
42
        unset($keyArr[0]);
43
        $keyArr = array_values($keyArr);
44
        $lang_new = $this->getDataFromTranslation($lang,$keyArr);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $lang does not seem to be defined for all execution paths leading up to this point.
Loading history...
45
46
        if(count($replace) > 0){
47
            foreach($replace as $key=>$replace_item){
48
                if(strpos($lang_new,'%') != 0){
49
                    str_replace($key,$replace_item,$lang_new);
50
                }
51
            }
52
        }else{
53
            return $lang_new;
54
        }
55
    }
56
57
58
    public function getDataFromTranslation( $data, $keyArr){
59
        $arrayFound  = isset($data[$keyArr[0]]) ? $data[$keyArr[0]] : '';
60
        if($arrayFound){
61
            unset($keyArr[0]);
62
            $keyArr = array_values($keyArr);
63
            if(isset($keyArr[0])){
64
                return $this->getDataFromTranslation($arrayFound,$keyArr);
65
            }
66
        }
67
        return $arrayFound ;
68
    }
69
70
71
72
73
74
    public function trans(string $key, array $replace = [])
75
    {
76
        return $this->_loadkey($key,$replace);
77
    }
78
79
80
81
    public function getTranslation_dirs()
82
    {
83
        $dir = scandir($this->settings['app']['translation']['translations_path']);
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist on Core\Translator\Translator. Since you implemented __get, consider adding a @property annotation.
Loading history...
84
        $ex_folders = array('..', '.');
85
86
87
88
        return array_diff($dir,$ex_folders);
89
    }
90
91
}