1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\i18n\Profile; |
6
|
|
|
|
7
|
|
|
use Nette\SmartObject; |
8
|
|
|
use SixtyEightPublishers\i18n\Storage\ProfileStorageInterface; |
9
|
|
|
use SixtyEightPublishers\i18n\Exception\InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @property-read string $name |
13
|
|
|
* @property-read NULL|string $country |
14
|
|
|
* @property-read NULL|string $language |
15
|
|
|
* @property-read NULL|string $currency |
16
|
|
|
* @property-read string $defaultCountry |
17
|
|
|
* @property-read string $defaultLanguage |
18
|
|
|
* @property-read string $defaultCurrency |
19
|
|
|
*/ |
20
|
|
|
final class ActiveProfile implements ProfileInterface |
21
|
|
|
{ |
22
|
|
|
use SmartObject; |
23
|
|
|
|
24
|
|
|
/** @var \SixtyEightPublishers\i18n\Profile\ProfileInterface */ |
25
|
|
|
private $profile; |
26
|
|
|
|
27
|
|
|
/** @var \SixtyEightPublishers\i18n\Profile\ActiveProfileChangeNotifier */ |
28
|
|
|
private $notifier; |
29
|
|
|
|
30
|
|
|
/** @var \SixtyEightPublishers\i18n\Storage\ProfileStorageInterface */ |
31
|
|
|
private $profileStorage; |
32
|
|
|
|
33
|
|
|
/** @var NULL|string */ |
34
|
|
|
private $country; |
35
|
|
|
|
36
|
|
|
/** @var NULL|string */ |
37
|
|
|
private $language; |
38
|
|
|
|
39
|
|
|
/** @var NULL|string */ |
40
|
|
|
private $currency; |
41
|
|
|
|
42
|
|
|
/** @var string */ |
43
|
|
|
private $defaultCountry; |
44
|
|
|
|
45
|
|
|
/** @var string */ |
46
|
|
|
private $defaultLanguage; |
47
|
|
|
|
48
|
|
|
/** @var string */ |
49
|
|
|
private $defaultCurrency; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param \SixtyEightPublishers\i18n\Profile\ProfileInterface $profile |
53
|
|
|
* @param \SixtyEightPublishers\i18n\Profile\ActiveProfileChangeNotifier $notifier |
54
|
|
|
* @param \SixtyEightPublishers\i18n\Storage\ProfileStorageInterface $profileStorage |
55
|
|
|
*/ |
56
|
|
|
public function __construct( |
57
|
|
|
ProfileInterface $profile, |
58
|
|
|
ActiveProfileChangeNotifier $notifier, |
59
|
|
|
ProfileStorageInterface $profileStorage |
60
|
|
|
) { |
61
|
|
|
if (FALSE === $profile->isEnabled()) { |
62
|
|
|
throw new InvalidArgumentException(sprintf( |
63
|
|
|
'Profile "%s" can\'t be set as active because its disabled.', |
64
|
|
|
$profile->getName() |
65
|
|
|
)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (0 >= count($profile->getCountries()) || 0 >= count($profile->getLanguages()) || 0 >= count($profile->getCurrencies())) { |
69
|
|
|
throw new InvalidArgumentException(sprintf( |
70
|
|
|
'Invalid profile "%s" passed, profile must contains almost one country, language and currency.', |
71
|
|
|
$profile->getName() |
72
|
|
|
)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->profile = $profile; |
76
|
|
|
$this->notifier = $notifier; |
77
|
|
|
$this->profileStorage = $profileStorage; |
78
|
|
|
$this->defaultCountry = $profile->getCountries()[0]; |
|
|
|
|
79
|
|
|
$this->defaultLanguage = $profile->getLanguages()[0]; |
|
|
|
|
80
|
|
|
$this->defaultCurrency = $profile->getCurrencies()[0]; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param bool $useDefault |
85
|
|
|
* |
86
|
|
|
* @return NULL|string |
87
|
|
|
*/ |
88
|
|
|
public function getCountry(bool $useDefault = TRUE): ?string |
89
|
|
|
{ |
90
|
|
|
return TRUE === $useDefault ? ($this->country ?? $this->defaultCountry) : $this->country; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param bool $useDefault |
95
|
|
|
* |
96
|
|
|
* @return NULL|string |
97
|
|
|
*/ |
98
|
|
|
public function getLanguage(bool $useDefault = TRUE): ?string |
99
|
|
|
{ |
100
|
|
|
return TRUE === $useDefault ? ($this->language ?? $this->defaultLanguage) : $this->language; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param bool $useDefault |
105
|
|
|
* |
106
|
|
|
* @return NULL|string |
107
|
|
|
*/ |
108
|
|
|
public function getCurrency(bool $useDefault = TRUE): ?string |
109
|
|
|
{ |
110
|
|
|
return TRUE === $useDefault ? ($this->currency ?? $this->defaultCurrency) : $this->currency; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getDefaultCountry(): string |
117
|
|
|
{ |
118
|
|
|
return $this->defaultCountry; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getDefaultLanguage(): string |
125
|
|
|
{ |
126
|
|
|
return $this->defaultLanguage; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getDefaultCurrency(): string |
133
|
|
|
{ |
134
|
|
|
return $this->defaultCurrency; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $country |
139
|
|
|
* @param bool $persist |
140
|
|
|
* |
141
|
|
|
* @return \SixtyEightPublishers\i18n\Profile\ActiveProfile |
142
|
|
|
* @throws \SixtyEightPublishers\i18n\Exception\InvalidArgumentException |
143
|
|
|
*/ |
144
|
|
|
public function changeCountry(string $country, bool $persist = TRUE): self |
145
|
|
|
{ |
146
|
|
|
if (!in_array($country, $this->getCountries(), TRUE)) { |
147
|
|
|
throw new InvalidArgumentException(sprintf( |
148
|
|
|
'Country with code "%s" is not defined in active profile.', |
149
|
|
|
$country |
150
|
|
|
)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->country = $country; |
|
|
|
|
154
|
|
|
|
155
|
|
|
if (TRUE === $persist) { |
156
|
|
|
$this->profileStorage->persistActiveProfile($this); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$this->notifier->notifyOnCountryChange($this); |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $language |
166
|
|
|
* @param bool $persist |
167
|
|
|
* |
168
|
|
|
* @return \SixtyEightPublishers\i18n\Profile\ActiveProfile |
169
|
|
|
* @throws \SixtyEightPublishers\i18n\Exception\InvalidArgumentException |
170
|
|
|
*/ |
171
|
|
|
public function changeLanguage(string $language, bool $persist = TRUE): self |
172
|
|
|
{ |
173
|
|
|
if (!in_array($language, $this->getLanguages(), TRUE)) { |
174
|
|
|
if (is_string($language)) { |
|
|
|
|
175
|
|
|
foreach ($this->getLanguages() as $available) { |
176
|
|
|
if (substr($available, 0, 2) === substr($language, 0, 2)) { |
177
|
|
|
return $this->changeLanguage($available, $persist); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
throw new InvalidArgumentException(sprintf( |
183
|
|
|
'Language with code "%s" is not defined in active profile.', |
184
|
|
|
$language |
185
|
|
|
)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$this->language = $language; |
|
|
|
|
189
|
|
|
|
190
|
|
|
if (TRUE === $persist) { |
191
|
|
|
$this->profileStorage->persistActiveProfile($this); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$this->notifier->notifyOnLanguageChange($this); |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $currency |
201
|
|
|
* @param bool $persist |
202
|
|
|
* |
203
|
|
|
* @return \SixtyEightPublishers\i18n\Profile\ActiveProfile |
204
|
|
|
* @throws \SixtyEightPublishers\i18n\Exception\InvalidArgumentException |
205
|
|
|
*/ |
206
|
|
|
public function changeCurrency(string $currency, bool $persist = TRUE): self |
207
|
|
|
{ |
208
|
|
|
if (!in_array($currency, $this->getCurrencies(), TRUE)) { |
209
|
|
|
throw new InvalidArgumentException(sprintf( |
210
|
|
|
'Currency with code "%s" is not defined in active profile.', |
211
|
|
|
$currency |
212
|
|
|
)); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$this->currency = $currency; |
|
|
|
|
216
|
|
|
|
217
|
|
|
if (TRUE === $persist) { |
218
|
|
|
$this->profileStorage->persistActiveProfile($this); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$this->notifier->notifyOnCurrencyChange($this); |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* {@inheritdoc} |
228
|
|
|
*/ |
229
|
|
|
public function getName(): string |
230
|
|
|
{ |
231
|
|
|
return $this->profile->getName(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* {@inheritdoc} |
236
|
|
|
*/ |
237
|
|
|
public function getCountries(): array |
238
|
|
|
{ |
239
|
|
|
return $this->profile->getCountries(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* {@inheritdoc} |
244
|
|
|
*/ |
245
|
|
|
public function getLanguages(): array |
246
|
|
|
{ |
247
|
|
|
return $this->profile->getLanguages(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritdoc} |
252
|
|
|
*/ |
253
|
|
|
public function getCurrencies(): array |
254
|
|
|
{ |
255
|
|
|
return $this->profile->getCurrencies(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* {@inheritdoc} |
260
|
|
|
*/ |
261
|
|
|
public function getDomains(): array |
262
|
|
|
{ |
263
|
|
|
return $this->profile->getDomains(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
|
|
public function isEnabled(): bool |
270
|
|
|
{ |
271
|
|
|
return $this->profile->isEnabled(); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|