Skip to content

Commit

Permalink
Added price formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleBigDev committed Jan 8, 2018
1 parent 4c60de2 commit bfbbda3
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 25 deletions.
61 changes: 55 additions & 6 deletions src/PrestaShopBundle/Localization/Number/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@
use PrestaShop\PrestaShop\Adapter\RoundingMapper;
use PrestaShopBundle\Localization\Exception\LocalizationException;
use PrestaShopBundle\Localization\Specification\NumberInterface as NumberSpecification;
use PrestaShopBundle\Localization\Specification\Price as PriceSpecification;

class Formatter
{
const GROUP_SEPARATOR_PLACEHOLDER = ',';
const CURRENCY_SYMBOL_PLACEHOLDER = '¤';
const DECIMAL_SEPARATOR_PLACEHOLDER = '.';
const GROUP_SEPARATOR_PLACEHOLDER = ',';
const MINUS_SIGN_PLACEHOLDER = '-';
const PERCENT_SYMBOL_PLACEHOLDER = '%';
const PLUS_SIGN_PLACEHOLDER = '+';

/**
* Number specification to be used when formatting a number
Expand Down Expand Up @@ -104,6 +109,8 @@ public function format($number)
$formattedNumber = $this->addPlaceholders($formattedNumber, $pattern);
$formattedNumber = $this->localizeNumber($formattedNumber);

$formattedNumber = $this->performSpecificReplacements($formattedNumber);

return $formattedNumber;
}

Expand Down Expand Up @@ -257,11 +264,11 @@ protected function replaceSymbols($number)
{
$symbols = $this->numberSpecification->getSymbolsByNumberingSystem($this->numberingSystem);
$replacements = [
'.' => $symbols->getDecimal(),
',' => $symbols->getGroup(),
'+' => $symbols->getPlusSign(),
'-' => $symbols->getMinusSign(),
'%' => $symbols->getPercentSign(),
self::DECIMAL_SEPARATOR_PLACEHOLDER => $symbols->getDecimal(),
self::GROUP_SEPARATOR_PLACEHOLDER => $symbols->getGroup(),
self::MINUS_SIGN_PLACEHOLDER => $symbols->getMinusSign(),
self::PERCENT_SYMBOL_PLACEHOLDER => $symbols->getPercentSign(),
self::PLUS_SIGN_PLACEHOLDER => $symbols->getPlusSign(),
];

return strtr($number, $replacements);
Expand Down Expand Up @@ -298,4 +305,46 @@ protected function addPlaceholders($formattedNumber, $pattern)

return $formattedNumber;
}

/**
* Perform some more specific replacements
*
* Specific replacements are needed when number specification is extended.
* For instance, prices have an extended number specification in order to
* add currency symbol to the formatted number.
*
* @param string $formattedNumber
*
* @return mixed
*/
public function performSpecificReplacements($formattedNumber)
{
$formattedNumber = $this->tryCurrencyReplacement($formattedNumber);

return $formattedNumber;
}

/**
* Try to replace currency placeholder by actual currency
*
* Placeholder will be replaced either by the symbol or the ISO code, depending on price specification
*
* @param $formattedNumber
* The number to format
*
* @return string
* The number after currency replacement
*/
protected function tryCurrencyReplacement($formattedNumber)
{
if ($this->numberSpecification instanceof PriceSpecification) {
$currency = PriceSpecification::CURRENCY_DISPLAY_CODE == $this->numberSpecification->getCurrencyDisplay()
? $this->numberSpecification->getCurrencyCode()
: $this->numberSpecification->getCurrencySymbol();

$formattedNumber = str_replace(self::CURRENCY_SYMBOL_PLACEHOLDER, $currency, $formattedNumber);
}

return $formattedNumber;
}
}
52 changes: 42 additions & 10 deletions src/PrestaShopBundle/Localization/Specification/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ class Price extends NumberSpecification
{
/**
* Currency display option : symbol notation
* eg: €
*/
const CURRENCY_DISPLAY_SYMBOL = 'symbol';

/**
* Currency display option : ISO code notation
* eg: EUR
*/
const CURRENCY_DISPLAY_CODE = 'code';

Expand All @@ -56,35 +54,47 @@ class Price extends NumberSpecification
*/
protected $currencyDisplay;

/**
* @var string The currency symbol
* eg : €
*/
protected $currencySymbol;

/**
* @var string The currency code
* eg : EUR
*/
protected $currencyCode;

/**
* Price specification constructor.
*
* @param string $positivePattern
* @param string $positivePattern
* CLDR formatting pattern for positive amounts
*
* @param string $negativePattern
* @param string $negativePattern
* CLDR formatting pattern for negative amounts
*
* @param NumberSymbolList[] $symbols
* List of available number symbols lists (NumberSymbolList objects)
* Each list is indexed by numbering system
*
* @param int $maxFractionDigits
* @param int $maxFractionDigits
* Maximum number of digits after decimal separator
*
* @param int $minFractionDigits
* @param int $minFractionDigits
* Minimum number of digits after decimal separator
*
* @param bool $groupingUsed
* @param bool $groupingUsed
* Is digits grouping used ?
*
* @param int $primaryGroupSize
* @param int $primaryGroupSize
* Size of primary digits group in the number
*
* @param int $secondaryGroupSize
* @param int $secondaryGroupSize
* Size of secondary digits group in the number
*
* @param string $currencyDisplay
* @param string $currencyDisplay
* Type of display for currency symbol
*
* @throws LocalizationException
Expand Down Expand Up @@ -124,6 +134,28 @@ public function getCurrencyDisplay()
return $this->currencyDisplay;
}

/**
* Get the currency symbol
* eg. : €
*
* @return string
*/
public function getCurrencySymbol()
{
return $this->currencySymbol;
}

/**
* Get the currency ISO code
* eg. : EUR
*
* @return string
*/
public function getCurrencyCode()
{
return $this->currencyCode;
}

/**
* Data (attributes) validation
*
Expand Down
Loading

0 comments on commit bfbbda3

Please sign in to comment.