1 | <?php |
||
19 | 1 | class ActiveProfile implements IProfile |
|
20 | { |
||
21 | 1 | use SmartObject; |
|
22 | |||
23 | /** @var \SixtyEightPublishers\Application\Environment\IProfile */ |
||
24 | private $profile; |
||
25 | |||
26 | /** @var \SixtyEightPublishers\Application\Environment\IProfileStorage */ |
||
27 | private $profileStorage; |
||
28 | |||
29 | /** @var NULL|string */ |
||
30 | private $country; |
||
31 | |||
32 | /** @var NULL|string */ |
||
33 | private $language; |
||
34 | |||
35 | /** @var NULL|string */ |
||
36 | private $currency; |
||
37 | |||
38 | /** @var NULL|string */ |
||
39 | private $defaultCountry; |
||
40 | |||
41 | /** @var NULL|string */ |
||
42 | private $defaultLanguage; |
||
43 | |||
44 | /** @var NULL|string */ |
||
45 | private $defaultCurrency; |
||
46 | |||
47 | /** @var NULL|callable[] */ |
||
48 | public $onChangeLanguage; |
||
49 | |||
50 | /** |
||
51 | * @param \SixtyEightPublishers\Application\Environment\IProfile $profile |
||
52 | * @param \SixtyEightPublishers\Application\Environment\IProfileStorage $profileStorage |
||
53 | */ |
||
54 | public function __construct(IProfile $profile, IProfileStorage $profileStorage) |
||
62 | |||
63 | /** |
||
64 | * @param bool $useDefault |
||
65 | * |
||
66 | * @return NULL|string |
||
67 | */ |
||
68 | public function getCountry(bool $useDefault = TRUE) : ?string |
||
72 | |||
73 | /** |
||
74 | * @param bool $useDefault |
||
75 | * |
||
76 | * @return NULL|string |
||
77 | */ |
||
78 | public function getLanguage(bool $useDefault = TRUE) : ?string |
||
82 | |||
83 | /** |
||
84 | * @param bool $useDefault |
||
85 | * |
||
86 | * @return NULL|string |
||
87 | */ |
||
88 | public function getCurrency(bool $useDefault = TRUE) : ?string |
||
92 | |||
93 | /** |
||
94 | * @return NULL|string |
||
95 | */ |
||
96 | public function getDefaultCountry() : ?string |
||
100 | |||
101 | /** |
||
102 | * @return NULL|string |
||
103 | */ |
||
104 | public function getDefaultLanguage() : ?string |
||
108 | |||
109 | /** |
||
110 | * @return NULL|string |
||
111 | */ |
||
112 | public function getDefaultCurrency() : ?string |
||
116 | |||
117 | /** |
||
118 | * @param string $country |
||
119 | * @param bool $persist |
||
120 | * |
||
121 | * @return $this |
||
122 | */ |
||
123 | public function changeCountry($country, $persist = TRUE) |
||
137 | |||
138 | /** |
||
139 | * @param string $language |
||
140 | * @param bool $persist |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function changeLanguage($language, $persist = TRUE) |
||
145 | { |
||
146 | 1 | if (!in_array($language, $this->getLanguages())) { |
|
147 | 1 | if (is_string($language)) { |
|
148 | 1 | foreach ($this->getLanguages() as $available) { |
|
149 | 1 | if (substr($available, 0, 2) === substr($language, 0, 2)) { |
|
150 | return $this->changeLanguage($available, $persist); |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | 1 | throw new ProfileConfigurationException("Language with code \"{$language}\" is not defined in active profile."); |
|
156 | } |
||
157 | |||
158 | 1 | $this->language = $language; |
|
159 | |||
160 | 1 | if ($persist) { |
|
161 | 1 | $this->profileStorage->persist(); |
|
162 | } |
||
163 | 1 | $this->onChangeLanguage($language); |
|
164 | |||
165 | 1 | return $this; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param string $currency |
||
170 | * @param bool $persist |
||
171 | * |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function changeCurrency($currency, $persist = TRUE) |
||
188 | |||
189 | /***************** interface \SixtyEightPublishers\Application\Environment\IProfile *****************/ |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function getName() : string |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function getCountries() : array |
||
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | public function getLanguages() : array |
||
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | public function getCurrencies() : array |
||
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | public function getDomains() : array |
||
230 | } |
||
231 |