|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jenschude <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Model\Common; |
|
7
|
|
|
|
|
8
|
|
|
use Commercetools\Core\Helper\CurrencyFormatterInterface; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Commercetools\Core\Helper\CurrencyFormatter; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @description |
|
14
|
|
|
* ## Usage |
|
15
|
|
|
* |
|
16
|
|
|
* The context will be set at ContextAware objects like JsonObject and Collection. By adding a context to the client |
|
17
|
|
|
* config the context will be set to all request, responses and other ContextAware objects. Besides that you can always |
|
18
|
|
|
* set a new context to every ContextAware object at any time. |
|
19
|
|
|
* |
|
20
|
|
|
* ```php |
|
21
|
|
|
* $context = Context::of(); |
|
22
|
|
|
* ``` |
|
23
|
|
|
* |
|
24
|
|
|
* For production environments it's advised to set the graceful flag to prevent Exceptions by toString conversions() |
|
25
|
|
|
* |
|
26
|
|
|
* ```php |
|
27
|
|
|
* $context->setGraceful(true); |
|
28
|
|
|
* ``` |
|
29
|
|
|
|
|
30
|
|
|
* ### Languages and Locales |
|
31
|
|
|
* |
|
32
|
|
|
* For automatic fallback string conversion e.g. with LocalizedString you can set the available languages. The |
|
33
|
|
|
* LocalizedString will try to resolve a string in the given order. It's strongly advised to set the locale in |
|
34
|
|
|
* the Context as it is used for example by the CurrencyFormatter. If no locale is set, the default locale given |
|
35
|
|
|
* by php config will be used. |
|
36
|
|
|
* |
|
37
|
|
|
* ```php |
|
38
|
|
|
* $context->setLanguages(['de', 'en'])->setLocale('de_DE'); |
|
39
|
|
|
* ``` |
|
40
|
|
|
* |
|
41
|
|
|
* ### CurrencyFormatter |
|
42
|
|
|
* |
|
43
|
|
|
* The context provides a builtin CurrencyFormatter. The default currency formatter will format a currency with |
|
44
|
|
|
* the help of the intl extension and the locale set. |
|
45
|
|
|
* |
|
46
|
|
|
* Example for custom currency formatter: |
|
47
|
|
|
* ```php |
|
48
|
|
|
* $currencyFormatter = new CurrencyFormatter(); |
|
49
|
|
|
* $currencyFormatter->setFormatCallback(function($centAmount, $currency)) { |
|
50
|
|
|
* $amount = $centAmount / 100; |
|
51
|
|
|
* $currency = mb_strtoupper($currency); |
|
52
|
|
|
* $locale = $this->context->getLocale(); |
|
53
|
|
|
* |
|
54
|
|
|
* $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); |
|
55
|
|
|
* return $formatter->formatCurrency($amount, $currency); |
|
56
|
|
|
* } |
|
57
|
|
|
* $context->setCurrencyFormatter($currencyFormatter); |
|
58
|
|
|
* ``` |
|
59
|
|
|
* |
|
60
|
|
|
* @package Commercetools\Core\Model\Common |
|
61
|
|
|
*/ |
|
62
|
|
|
class Context implements \ArrayAccess |
|
63
|
|
|
{ |
|
64
|
|
|
/** |
|
65
|
|
|
* @var bool |
|
66
|
|
|
*/ |
|
67
|
|
|
private $graceful = false; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var array |
|
71
|
|
|
*/ |
|
72
|
|
|
private $languages = []; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var CurrencyFormatterInterface |
|
76
|
|
|
*/ |
|
77
|
|
|
private $currencyFormatter; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var string |
|
81
|
|
|
*/ |
|
82
|
|
|
private $locale; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var LoggerInterface |
|
86
|
|
|
*/ |
|
87
|
|
|
private $logger; |
|
88
|
|
|
|
|
89
|
|
|
const GRACEFUL = 'graceful'; |
|
90
|
|
|
const LANGUAGES = 'languages'; |
|
91
|
|
|
const CURRENCY_FORMATTER = 'currencyFormatter'; |
|
92
|
|
|
const LOCALE = 'locale'; |
|
93
|
|
|
const LOGGER = 'logger'; |
|
94
|
|
|
|
|
95
|
258 |
|
public function __construct() |
|
96
|
|
|
{ |
|
97
|
|
|
|
|
98
|
258 |
|
$context = $this; |
|
99
|
258 |
|
$this->currencyFormatter = new CurrencyFormatter($context); |
|
100
|
258 |
|
$this->locale = null; |
|
101
|
258 |
|
if (extension_loaded('intl')) { |
|
102
|
253 |
|
$this->locale = \Locale::getDefault(); |
|
103
|
|
|
} |
|
104
|
258 |
|
$this->logger = null; |
|
105
|
258 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return boolean |
|
109
|
|
|
*/ |
|
110
|
84 |
|
public function isGraceful() |
|
111
|
|
|
{ |
|
112
|
84 |
|
return $this->graceful; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param boolean $graceful |
|
117
|
|
|
* @return Context |
|
118
|
|
|
*/ |
|
119
|
149 |
|
public function setGraceful($graceful) |
|
120
|
|
|
{ |
|
121
|
149 |
|
$this->graceful = $graceful; |
|
122
|
149 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
125 |
|
public function getLanguages() |
|
129
|
|
|
{ |
|
130
|
125 |
|
return $this->languages; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param array $languages |
|
135
|
|
|
* @return Context |
|
136
|
|
|
*/ |
|
137
|
166 |
|
public function setLanguages(array $languages) |
|
138
|
|
|
{ |
|
139
|
166 |
|
$this->languages = $languages; |
|
140
|
166 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return CurrencyFormatterInterface |
|
145
|
|
|
*/ |
|
146
|
5 |
|
public function getCurrencyFormatter() |
|
147
|
|
|
{ |
|
148
|
5 |
|
return $this->currencyFormatter; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param CurrencyFormatterInterface $currencyFormatter |
|
153
|
|
|
* @return Context |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function setCurrencyFormatter($currencyFormatter) |
|
156
|
|
|
{ |
|
157
|
1 |
|
$this->currencyFormatter = $currencyFormatter; |
|
158
|
1 |
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
7 |
|
public function getLocale() |
|
165
|
|
|
{ |
|
166
|
7 |
|
return $this->locale; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param string $locale |
|
171
|
|
|
* @return Context |
|
172
|
|
|
*/ |
|
173
|
46 |
|
public function setLocale($locale) |
|
174
|
|
|
{ |
|
175
|
46 |
|
$this->locale = $locale; |
|
176
|
46 |
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return LoggerInterface |
|
181
|
|
|
*/ |
|
182
|
6 |
|
public function getLogger() |
|
183
|
|
|
{ |
|
184
|
6 |
|
return $this->logger; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param LoggerInterface $logger |
|
189
|
|
|
* @return Context |
|
190
|
|
|
*/ |
|
191
|
2 |
|
public function setLogger($logger) |
|
192
|
|
|
{ |
|
193
|
2 |
|
$this->logger = $logger; |
|
194
|
2 |
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
95 |
|
public static function of() |
|
198
|
|
|
{ |
|
199
|
95 |
|
return new static(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @inheritDoc |
|
204
|
|
|
*/ |
|
205
|
1 |
|
public function offsetExists($offset) |
|
206
|
|
|
{ |
|
207
|
1 |
|
return isset($this->$offset); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @inheritDoc |
|
212
|
|
|
*/ |
|
213
|
1 |
|
public function offsetGet($offset) |
|
214
|
|
|
{ |
|
215
|
1 |
|
if ($this->offsetExists($offset)) { |
|
216
|
1 |
|
$method = 'get'.ucfirst($offset); |
|
217
|
|
|
|
|
218
|
1 |
|
return $this->$method(); |
|
219
|
|
|
} |
|
220
|
|
|
return null; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @inheritDoc |
|
225
|
|
|
*/ |
|
226
|
1 |
|
public function offsetSet($offset, $value) |
|
227
|
|
|
{ |
|
228
|
1 |
|
if (property_exists($this, $offset)) { |
|
229
|
1 |
|
$method = 'set'.ucfirst($offset); |
|
230
|
|
|
|
|
231
|
1 |
|
$this->$method($value); |
|
232
|
|
|
} |
|
233
|
1 |
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @inheritDoc |
|
237
|
|
|
*/ |
|
238
|
|
|
public function offsetUnset($offset) |
|
239
|
|
|
{ |
|
240
|
|
|
if ($this->offsetExists($offset)) { |
|
241
|
|
|
$method = 'set'.ucfirst($offset); |
|
242
|
|
|
|
|
243
|
|
|
$this->$method(null); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|