Completed
Push — master ( b6ea02...766f34 )
by Eridan
02:19
created

Translator::translate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* MIT License
4
5
Copyright (c) 2018 Eridan Domoratskiy
6
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
of this software and associated documentation files (the "Software"), to deal
9
in the Software without restriction, including without limitation the rights
10
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
copies of the Software, and to permit persons to whom the Software is
12
furnished to do so, subject to the following conditions:
13
14
The above copyright notice and this permission notice shall be included in all
15
copies or substantial portions of the Software.
16
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
SOFTWARE. */
24
25
namespace MaintenanceScreen;
26
27
use Symfony\Component\Config\Definition\Processor;
28
29
use MaintenanceScreen\Configurations\TranslatorConfiguration;
30
31
/**
32
 * Translates to some language
33
 *
34
 * @author ProgMiner
35
 */
36
class Translator {
37
38
    /**
39
     * @var string[] Translations (Key => Translation)
40
     */
41
    protected $translations;
42
43
    /**
44
     * @var string Language name
45
     */
46
    protected $language;
47
48
    /**
49
     * @param array  $translations Array with translations
50
     * @param string $language     Language name
51
     */
52 4
    public function __construct(array $translations, string $language) {
53 4
        $this->translations = $translations;
54 4
        $this->language = $language;
55 4
    }
56
57
    /**
58
     * Returns language name
59
     *
60
     * @return string
61
     */
62 4
    public function getLanguage(): string {
63 4
        return $this->language;
64
    }
65
66
    /**
67
     * Returns all translations
68
     *
69
     * @return string[] Translations (Key => Translation)
70
     */
71 4
    public function getTranslations(): array {
72 4
        return $this->translations;
73
    }
74
75
    /**
76
     * Checks translation for key exists
77
     *
78
     * @param string $key Key
79
     *
80
     * @return bool
81
     */
82 4
    public function supports(string $key): bool {
83 4
        return isset($this->translations[$key]);
84
    }
85
86
    /**
87
     * Translates key
88
     *
89
     * @param string $key Key
90
     *
91
     * @return string Translation or key if is not exists
92
     */
93 4
    public function translate(string $key) {
94 4
        if (!$this->supports($key)) {
95 4
            return $key;
96
        }
97
98
        return $this->translations[$key];
99
    }
100
101
    /**
102
     * Makes Translator instance from config
103
     *
104
     * @param array  $config   Configuration array
105
     * @param string $language Language name
106
     *
107
     * @return static Made instance
108
     */
109
    public static function fromConfig(array $config, string $language): Translator {
110
        $translations = (new Processor())->
111
            processConfiguration(new TranslatorConfiguration(), [$config]);
112
113
        return new static($translations, $language);
114
    }
115
}
116