|
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 MemoryStorage.php |
|
33
|
|
|
* |
|
34
|
|
|
* The memory storage class |
|
35
|
|
|
* |
|
36
|
|
|
* @package Platine\Lang\Storage |
|
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\Storage; |
|
48
|
|
|
|
|
49
|
|
|
use InvalidArgumentException; |
|
50
|
|
|
use Platine\Lang\Configuration; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @class MemoryStorage |
|
54
|
|
|
* @package Platine\Lang\Storage |
|
55
|
|
|
*/ |
|
56
|
|
|
class MemoryStorage implements StorageInterface |
|
57
|
|
|
{ |
|
58
|
|
|
/** |
|
59
|
|
|
* The configuration instance |
|
60
|
|
|
* @var Configuration |
|
61
|
|
|
*/ |
|
62
|
|
|
protected Configuration $config; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* The default domain |
|
66
|
|
|
* @var string |
|
67
|
|
|
*/ |
|
68
|
|
|
protected string $domain; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* List of domain currently in use |
|
72
|
|
|
* @var array<string, string> |
|
73
|
|
|
*/ |
|
74
|
|
|
protected array $domains = []; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* The current locale |
|
78
|
|
|
* @var string |
|
79
|
|
|
*/ |
|
80
|
|
|
protected string $locale; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* The locale encoding |
|
84
|
|
|
* @var string |
|
85
|
|
|
*/ |
|
86
|
|
|
protected string $encoding; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Create new instance |
|
90
|
|
|
* @param Configuration|null $config |
|
91
|
|
|
*/ |
|
92
|
|
|
public function __construct(?Configuration $config = null) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->config = $config ?? new Configuration([]); |
|
95
|
|
|
$this->domain = $this->config->get('domain'); |
|
96
|
|
|
$this->encoding = $this->config->get('encoding'); |
|
97
|
|
|
$this->locale = $this->config->get('locale'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Return the configuration |
|
102
|
|
|
* @return Configuration |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getConfig(): Configuration |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->config; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inhereitdoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getDomains(): array |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->domains; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inhereitdoc} |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setLocale(string $locale): self |
|
121
|
|
|
{ |
|
122
|
|
|
$this->locale = $locale; |
|
123
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inhereitdoc} |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getLocale(): string |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->locale; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inhereitdoc} |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getEncoding(): string |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->encoding; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inhereitdoc} |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setEncoding(string $encoding): self |
|
147
|
|
|
{ |
|
148
|
|
|
$this->encoding = $encoding; |
|
149
|
|
|
|
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* {@inhereitdoc} |
|
155
|
|
|
*/ |
|
156
|
|
|
public function setDomain(string $domain): self |
|
157
|
|
|
{ |
|
158
|
|
|
$this->domain = $domain; |
|
159
|
|
|
|
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* {@inhereitdoc} |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getDomain(): string |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->domain; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* {@inhereitdoc} |
|
173
|
|
|
*/ |
|
174
|
|
|
public function addDomain(string $domain, ?string $path = null): self |
|
175
|
|
|
{ |
|
176
|
|
|
if ($path === null) { |
|
177
|
|
|
$path = $this->config->get('translation_path'); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if (isset($this->domains[$domain]) && $this->domains[$domain] !== $path) { |
|
181
|
|
|
throw new InvalidArgumentException(sprintf( |
|
182
|
|
|
'Domain [%s] already exists', |
|
183
|
|
|
$domain |
|
184
|
|
|
)); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$this->domains[$domain] = $path; |
|
188
|
|
|
|
|
189
|
|
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|