Class Context
- Commercetools\Core\Model\Common\Context implements ArrayAccess
Description:
Usage
The context will be set at ContextAware objects like JsonObject and Collection. By adding a context to the client config the context will be set to all request, responses and other ContextAware objects. Besides that you can always set a new context to every ContextAware object at any time.
$context = Context::of();
For production environments it's advised to set the graceful flag to prevent Exceptions by toString conversions()
$context->setGraceful(true);
Languages and Locales
For automatic fallback string conversion e.g. with LocalizedString you can set the available languages. The LocalizedString will try to resolve a string in the given order. It's strongly advised to set the locale in the Context as it is used for example by the CurrencyFormatter. If no locale is set, the default locale given by php config will be used.
$context->setLanguages(['de', 'en'])->setLocale('de_DE');
CurrencyFormatter
The context provides a builtin CurrencyFormatter. The default currency formatter will format a currency with the help of the intl extension and the locale set.
Example for custom currency formatter:
$currencyFormatter = new CurrencyFormatter(); $currencyFormatter->setFormatCallback(function($centAmount, $currency)) { $amount = $centAmount / 100; $currency = mb_strtoupper($currency); $locale = $this->context->getLocale(); $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); return $formatter->formatCurrency($amount, $currency); } $context->setCurrencyFormatter($currencyFormatter);
Located at Core/Model/Common/Context.php
Methods summary
public
|
|
public
boolean
|
|
public
|
|
public
array
|
|
public
|
|
public
|
|
public
|
|
public
string
|
|
public
|
|
public
Psr\Log\LoggerInterface
|
|
public
|
|
public static
|
|
public
|
|
public
|
|
public
|
|
public
|