Completed
Push — master ( 0d6573...bd21d3 )
by Fumio
07:02
created

Translator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
dl 0
loc 101
rs 10
c 0
b 0
f 0
ccs 24
cts 27
cp 0.8889
wmc 13
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A has() 0 4 1
A get() 0 14 4
A resolve() 0 10 2
A fullkey() 0 4 2
A translate() 0 12 3
1
<?php
2
3
namespace LaravelPlus\Extension\Specs;
4
5
use Illuminate\Contracts\Translation\Translator as TranslatorInterface;
6
7
class Translator
8
{
9
    /**
10
     * @var \Illuminate\Contracts\Translation\Translator
11
     */
12
    protected $translator;
13
14
    /**
15
     * @var string
16
     */
17
    protected $namespace;
18
19
    /**
20
     * @param \Illuminate\Contracts\Translation\Translator $translator
21
     * @param string $namespace
22
     */
23 9
    public function __construct(TranslatorInterface $translator, $namespace = '')
24
    {
25 9
        $this->translator = $translator;
26 9
        $this->namespace = $namespace;
27 9
    }
28
29
    /**
30
     * Determine if a translation exists.
31
     *
32
     * @param string $key
33
     * @param string $locale
34
     * @return bool
35
     */
36 2
    public function has($key, $locale = null)
37
    {
38 2
        return $this->translator->has($this->fullkey($key));
39
    }
40
41
    /**
42
     * @param string $key
43
     * @param mixed $default
44
     *
45
     * @return mixed
46
     */
47 5
    public function get($key, $default = false)
48
    {
49 5
        $value = $this->translate($this->fullkey($key), $default);
50
51 5
        if (is_string($value)) {
52 3
            $value = $this->resolve($value);
53 5
        } elseif (is_array($value)) {
54 5
            foreach ($value as &$subValue) {
55 3
                $subValue = $this->resolve($subValue);
56
            }
57
        }
58
59 5
        return $value;
60
    }
61
62
    /**
63
     * @param string $string
64
     * @param string $default
65
     *
66
     * @return string
67
     */
68 3
    public function resolve($string, $default = false)
69
    {
70 3
        if (strpos($string, '@') !== false) {
71
            list(, $key) = explode('@', $string, 2);
72
73
            $string = $this->translate($this->fullkey('vocabulary.'.$key), $default);
74
        }
75
76 3
        return $string;
77
    }
78
79
    /**
80
     * @param string $key
81
     *
82
     * @return string
83
     */
84 5
    private function fullkey($key)
85
    {
86 5
        return $this->namespace ? $this->namespace.'::'.$key : $key;
87
    }
88
89
    /**
90
     * @param string $key
91
     * @param mixed  $default
92
     *
93
     * @return mixed
94
     */
95 5
    protected function translate($key, $default = false)
96
    {
97 5
        $string = $this->translator->get($key);
98
99 5
        if ($default !== false) {
100 3
            if ($string == $key) {
101
                return $default;
102
            }
103
        }
104
105 5
        return $string;
106
    }
107
}
108