Composable Commerce PHP SDKPHP SDK
  • Namespace
  • Class
  • Tree

Namespaces

  • Commercetools
    • Commons
      • Helper
    • Core
      • Builder
        • Request
        • Update
      • Cache
      • Client
        • Adapter
        • OAuth
      • Error
      • Helper
        • Annotate
        • State
          • Renderer
        • Subscriber
          • Log
      • Model
        • ApiClient
        • Cart
        • CartDiscount
        • Category
        • Channel
        • Common
        • Customer
        • CustomerGroup
        • CustomField
        • CustomObject
        • DiscountCode
        • Extension
        • Inventory
        • Message
        • Order
        • OrderEdit
        • Payment
        • Product
          • Search
        • ProductDiscount
        • ProductSelection
        • ProductType
        • Project
        • Review
        • ShippingMethod
        • ShoppingList
        • State
        • Store
        • Subscription
        • TaxCategory
        • Type
        • Zone
      • Request
        • ApiClients
        • CartDiscounts
          • Command
        • Carts
          • Command
        • Categories
          • Command
        • Channels
          • Command
        • CustomerGroups
          • Command
        • Customers
          • Command
        • CustomField
          • Command
        • CustomObjects
        • DiscountCodes
          • Command
        • Extensions
          • Command
        • GraphQL
        • InStores
        • Inventory
          • Command
        • Me
          • Command
        • Messages
        • OrderEdits
          • Command
          • StagedOrder
            • Command
        • Orders
          • Command
        • Payments
          • Command
        • ProductDiscounts
          • Command
        • Products
          • Command
        • ProductSelections
          • Command
        • ProductTypes
          • Command
        • Project
          • Command
        • Query
        • Reviews
          • Command
        • ShippingMethods
          • Command
        • ShoppingLists
          • Command
        • States
          • Command
        • Stores
          • Command
        • Subscriptions
          • Command
        • TaxCategories
          • Command
        • Types
          • Command
        • Zones
          • Command
      • Response

Classes

  • AbstractJsonDeserializeObject
  • Address
  • AddressCollection
  • Asset
  • AssetCollection
  • AssetDimension
  • AssetDraft
  • AssetDraftCollection
  • AssetSource
  • AssetSourceCollection
  • Attribute
  • AttributeCollection
  • CentPrecisionMoney
  • ClientLogging
  • Collection
  • ContainerAndKey
  • Context
  • CreatedBy
  • DateDecorator
  • DateTimeDecorator
  • DiscountedPrice
  • DiscountValue
  • Enum
  • EnumCollection
  • GeoLocation
  • GeoPoint
  • HighPrecisionMoney
  • Image
  • ImageCollection
  • ImageDimension
  • JsonObject
  • KeyReference
  • KeyResourceIdentifier
  • LastModifiedBy
  • LocalizedEnum
  • LocalizedEnumCollection
  • LocalizedString
  • Money
  • MoneyCollection
  • Price
  • PriceCollection
  • PriceDraft
  • PriceDraftCollection
  • PriceTier
  • PriceTierCollection
  • Reference
  • ReferenceCollection
  • Resource
  • ResourceIdentifier
  • ScopedPrice
  • Set
  • TaxedItemPrice
  • TaxedPrice
  • TaxPortion
  • TaxPortionCollection
  • TimeDecorator

Interfaces

  • ContextAwareInterface
  • JsonDeserializeInterface
  • ObjectTreeInterface
  • ReferenceObjectInterface
  • TypeableInterface

Traits

  • ContextTrait
  • LocaleTrait
  • ObjectTreeTrait

Class Context

Commercetools\Core\Model\Common\Context implements ArrayAccess
Namespace: Commercetools\Core\Model\Common
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
# __construct( )
public boolean
# isGraceful( )

Returns

boolean
public Commercetools\Core\Model\Common\Context
# setGraceful( boolean $graceful )

Parameters

$graceful

Returns

Commercetools\Core\Model\Common\Context
public array
# getLanguages( )

Returns

array
public Commercetools\Core\Model\Common\Context
# setLanguages( array $languages )

Parameters

$languages

Returns

Commercetools\Core\Model\Common\Context
public Commercetools\Core\Helper\CurrencyFormatterInterface
# getCurrencyFormatter( )

Returns

Commercetools\Core\Helper\CurrencyFormatterInterface
public Commercetools\Core\Model\Common\Context
# setCurrencyFormatter( Commercetools\Core\Helper\CurrencyFormatterInterface $currencyFormatter )

Parameters

$currencyFormatter

Returns

Commercetools\Core\Model\Common\Context
public string
# getLocale( )

Returns

string
public Commercetools\Core\Model\Common\Context
# setLocale( string $locale )

Parameters

$locale

Returns

Commercetools\Core\Model\Common\Context
public Psr\Log\LoggerInterface
# getLogger( )

Returns

Psr\Log\LoggerInterface
public Commercetools\Core\Model\Common\Context
# setLogger( Psr\Log\LoggerInterface $logger )

Parameters

$logger

Returns

Commercetools\Core\Model\Common\Context
public static
# of( )
public
# offsetExists( $offset )

Inheritdoc

Implementation of

ArrayAccess::offsetExists()
public
# offsetGet( $offset )

Inheritdoc

Implementation of

ArrayAccess::offsetGet()
public
# offsetSet( $offset, $value )

Inheritdoc

Implementation of

ArrayAccess::offsetSet()
public
# offsetUnset( $offset )

Inheritdoc

Implementation of

ArrayAccess::offsetUnset()

Constants summary

string GRACEFUL
# 'graceful'
string LANGUAGES
# 'languages'
string CURRENCY_FORMATTER
# 'currencyFormatter'
string LOCALE
# 'locale'
string LOGGER
# 'logger'
PHP SDK API documentation generated by ApiGen