CoolCMS API

PriceFormat
in package

FinalYes

Resolved amount format: what is actually used when rendering.

Number formatting is a property of the locale, not of the currency: the same euro is "1.234,56 €" in Germany and "€1,234.56" in Ireland. The whole format used to live on cms_system_currencies, so it is merged here from two sources:

  1. the localization (cms_system_localizations) takes precedence, but only in the fields it has filled in; null means "not handled here",
  2. the currency is the permanent fallback (the behavior before localizations existed).

Thanks to that the migration did not have to be atomic: until a localization is filled in, the system behaves exactly as before.

IMPORTANT: it does not belong in Utils/Contexts; that folder is in the DI search and autowiring would fail on a scalar constructor without defaults.

Table of Contents

Properties

$currencySymbolPosition  : string
$decimalSeparator  : string
$negativeSymbolPosition  : string
$precision  : int
$separator  : string
$showSuffix  : bool
$suffix  : string

Methods

__construct()  : mixed
attachSymbol()  : string
Attaches the currency symbol to an already formatted amount by the format position.
isSignSymbol()  : bool
A single-character currency sign without letters (€, $, £, ¥), not a letter abbreviation.
resolve()  : self
Builds the format from the localization (precedence) and the currency (fallback).
pickBool()  : bool
pickInt()  : int
pickString()  : string
Returns the value from the localization, otherwise from the currency, otherwise the default.

Properties

$currencySymbolPosition read-only

public string $currencySymbolPosition

$decimalSeparator read-only

public string $decimalSeparator

$negativeSymbolPosition read-only

public string $negativeSymbolPosition

Methods

__construct()

public __construct(string $separator, string $decimalSeparator, int $precision, string $currencySymbolPosition, string $negativeSymbolPosition, bool $showSuffix, string $suffix) : mixed
Parameters
$separator : string
$decimalSeparator : string
$precision : int
$currencySymbolPosition : string
$negativeSymbolPosition : string
$showSuffix : bool
$suffix : string

attachSymbol()

Attaches the currency symbol to an already formatted amount by the format position.

public attachSymbol(string $amount, string $symbol) : string

The position is a property of the localization (with the currency as fallback, see resolve()): the same euro is "12,10 €" on /sk/ and /cs/ and "€12.10" on /en/. Spacing:

  • after the amount always ("12 500 €"),
  • before the amount only for a letter abbreviation of the currency; a sign symbol sticks to the number in that convention ("€12.10", "$5"); "€ 12.10" is a typographic error.

IMPORTANT: the front JS components/i18n.js formatPrice() does the same; change the rule in both places.

Parameters
$amount : string
$symbol : string
Return values
string

isSignSymbol()

A single-character currency sign without letters (€, $, £, ¥), not a letter abbreviation.

public static isSignSymbol(string $symbol) : bool
Parameters
$symbol : string
Return values
bool

resolve()

Builds the format from the localization (precedence) and the currency (fallback).

public static resolve(object|null $currency[, object|null $localization = null ]) : self

Both entities are ?object because they arrive from different layers (Front/Admin/Api descendants), as $currency in PriceHelper always was. The decision uses ??, so values such as 0, '' or false on the localization do count; "not filled in" means only null.

Parameters
$currency : object|null
$localization : object|null = null
Return values
self

pickBool()

private static pickBool(object|null $currency, object|null $localization, string $getter, bool $default) : bool
Parameters
$currency : object|null
$localization : object|null
$getter : string
$default : bool
Return values
bool

pickInt()

private static pickInt(object|null $currency, object|null $localization, string $getter, int $default) : int
Parameters
$currency : object|null
$localization : object|null
$getter : string
$default : int
Return values
int

pickString()

Returns the value from the localization, otherwise from the currency, otherwise the default.

private static pickString(object|null $currency, object|null $localization, string $getter, string $default) : string

Entities arrive as ?object (each layer has its own descendants), so the getter is found via method_exists. The result is type-checked, not cast: if a layer returned an unexpected type, falling back to the default is better than converting silently (e.g. (int) 'abc' is 0, which would silently drop the decimal places).

Parameters
$currency : object|null
$localization : object|null
$getter : string
$default : string
Return values
string
On this page

Search results