Passed
Push — master ( eae2ea...251e76 )
by
unknown
01:43
created

Language::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PluginSimpleValidate\Libraries;
4
5
class Language
6
{
7
    /**
8
     * @var string
9
     */
10
    private $lang;
11
12
    /**
13
     * @var array
14
     */
15
    private $translation;
16
17
    /**
18
     * Language constructor.
19
     * @param string $lang
20
     * @param array $translation
21
     * provide parameter $translation for your own custom translation array
22
     */
23 9
    public function __construct(string $lang, array $translation = [])
24
    {
25 9
        $this->lang = $lang;
26
27 9
        if (!empty($translation)) {
28
            $this->translation = $translation;
29
        } else {
30 9
            $this->translation = include implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), '..', 'lang', $this->lang . '.php']);
31
        }
32 9
    }
33
34
    /**
35
     * @return string
36
     */
37
    public function getLang(): string
38
    {
39
        return $this->lang;
40
    }
41
42
    /**
43
     * @param string $key
44
     * @return string
45
     */
46 8
    public function getTranslation(string $key)
47
    {
48 8
        return isset($this->translation[$key]) ? $this->translation[$key] : $key;
49
    }
50
}