1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Lang |
5
|
|
|
* |
6
|
|
|
* Platine Lang is a translation library with extensible translator and storage |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Lang |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file BaseTranslator.php |
33
|
|
|
* |
34
|
|
|
* The base translator class |
35
|
|
|
* |
36
|
|
|
* @package Platine\Lang\Translator |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link https://www.platine-php.com |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Lang\Translator; |
48
|
|
|
|
49
|
|
|
use Platine\Lang\Configuration; |
50
|
|
|
use Platine\Lang\Storage\MemoryStorage; |
51
|
|
|
use Platine\Lang\Storage\StorageInterface; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @class BaseTranslator |
55
|
|
|
* @package Platine\Lang\Translator |
56
|
|
|
*/ |
57
|
|
|
abstract class BaseTranslator implements TranslatorInterface |
58
|
|
|
{ |
59
|
|
|
/** |
60
|
|
|
* The configuration instance |
61
|
|
|
* @var Configuration |
62
|
|
|
*/ |
63
|
|
|
protected Configuration $config; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The configuration instance |
67
|
|
|
* @var StorageInterface |
68
|
|
|
*/ |
69
|
|
|
protected StorageInterface $storage; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inhereitdoc} |
73
|
|
|
*/ |
74
|
|
|
public function __construct( |
75
|
|
|
?Configuration $config = null, |
76
|
|
|
?StorageInterface $storage = null |
77
|
|
|
) { |
78
|
|
|
$this->config = $config ?? new Configuration([]); |
79
|
|
|
$this->storage = $storage ?? new MemoryStorage($config); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Return the configuration |
84
|
|
|
* @return Configuration |
85
|
|
|
*/ |
86
|
|
|
public function getConfig(): Configuration |
87
|
|
|
{ |
88
|
|
|
return $this->config; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return the storage instance |
93
|
|
|
* @return StorageInterface |
94
|
|
|
*/ |
95
|
|
|
public function getStorage(): StorageInterface |
96
|
|
|
{ |
97
|
|
|
return $this->storage; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inhereitdoc} |
102
|
|
|
*/ |
103
|
|
|
public function __toString(): string |
104
|
|
|
{ |
105
|
|
|
return $this->getLocale(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inhereitdoc} |
110
|
|
|
*/ |
111
|
|
|
public function getDomain(): string |
112
|
|
|
{ |
113
|
|
|
return $this->storage->getDomain(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inhereitdoc} |
118
|
|
|
*/ |
119
|
|
|
public function setDomain(string $domain): self |
120
|
|
|
{ |
121
|
|
|
$this->storage->setDomain($domain); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inhereitdoc} |
128
|
|
|
*/ |
129
|
|
|
public function getDomains(): array |
130
|
|
|
{ |
131
|
|
|
return $this->storage->getDomains(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inhereitdoc} |
136
|
|
|
*/ |
137
|
|
|
public function addDomain(string $domain, ?string $path = null): self |
138
|
|
|
{ |
139
|
|
|
$this->storage->addDomain($domain, $path); |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inhereitdoc} |
146
|
|
|
*/ |
147
|
|
|
public function getEncoding(): string |
148
|
|
|
{ |
149
|
|
|
return $this->storage->getEncoding(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inhereitdoc} |
154
|
|
|
*/ |
155
|
|
|
public function setEncoding(string $encoding): self |
156
|
|
|
{ |
157
|
|
|
$this->storage->setEncoding($encoding); |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inhereitdoc} |
164
|
|
|
*/ |
165
|
|
|
public function getLocale(): string |
166
|
|
|
{ |
167
|
|
|
return $this->storage->getLocale(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inhereitdoc} |
172
|
|
|
*/ |
173
|
|
|
public function setLocale(string $locale): self |
174
|
|
|
{ |
175
|
|
|
if ($locale !== $this->storage->getLocale()) { |
176
|
|
|
$this->storage->setLocale($locale); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inhereitdoc} |
184
|
|
|
*/ |
185
|
|
|
public function isLocaleSupported(string $locale): bool |
186
|
|
|
{ |
187
|
|
|
return in_array($locale, $this->config->get('locales')); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inhereitdoc} |
192
|
|
|
*/ |
193
|
|
|
public function locales(): array |
194
|
|
|
{ |
195
|
|
|
return $this->config->get('locales'); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|