Skip to content

Commit

Permalink
Installed currencies + Locale integration tests (numbers formatting)
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleBigDev committed Apr 5, 2018
1 parent 2ddd02d commit 8295933
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 69 deletions.
32 changes: 26 additions & 6 deletions src/Core/Localization/CLDR/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,19 @@ protected function mainPath($filename = '')
* @param string $localeTag The wanted locale. Can be either a language code (e.g.: fr) of an IETF tag (e.g.: en-US)
*
* @return LocaleData
* @throws LocalizationException
*/
protected function getLocaleData($localeTag)
{
$xmlData = $this->getMainXmlData($localeTag);
try {
$xmlData = $this->getMainXmlData($localeTag);
} catch (LocalizationException $e) {
// Sometimes a file can be missing.
// Example for Chinese : zh_CN.xml doesn't exist. There is only a zh.xml file.
// That's why we can't let this exception bubble up.

return new LocaleData;
}

$supplementalData = ['digits' => $this->getDigitsData()];

return $this->mapLocaleData($xmlData, $supplementalData);
Expand All @@ -286,7 +294,8 @@ protected function getLocaleData($localeTag)
* @return LocaleData
* The mapped locale data
*
* @todo use root aliases to fill up missing values (e.g.: missing symbols for exotic numbering systems).
* @todo use $supplementalData for non-occidental digits
*
* @see http://cldr.unicode.org/development/development-process/design-proposals/resolution-of-cldr-files
*/
protected function mapLocaleData(SimplexmlElement $xmlLocaleData, $supplementalData)
Expand Down Expand Up @@ -324,12 +333,23 @@ protected function mapLocaleData(SimplexmlElement $xmlLocaleData, $supplementalD
}
// Symbols (by numbering system)
if (isset($numbersData->symbols)) {
/** @var SimpleXMLElement $symbolsNode */
foreach ($numbersData->symbols as $symbolsNode) {
if (!isset($symbolsNode['numberSystem'])) {
continue;
}
$thisNumberingSystem = (string)$symbolsNode['numberSystem'];

// Copying data from another node when relevant (alias)
if (isset($symbolsNode->alias)) {
// TODO here is the alias pointing to the data to be used for this specific numbering system (xpath)
// @see <project root>/localization/CLDR/core/common/main/root.xml
continue;
$results = $symbolsNode->xpath($symbolsNode->alias['path']);
if (empty($results)) {
continue;
}
$symbolsNode = $results[0];
}

$symbolsList = new NumberSymbolsData();
if (isset($symbolsNode->decimal)) {
$symbolsList->decimal = (string)$symbolsNode->decimal;
Expand Down Expand Up @@ -374,7 +394,7 @@ protected function mapLocaleData(SimplexmlElement $xmlLocaleData, $supplementalD
$symbolsList->currencyGroup = (string)$symbolsNode->currencyGroup;
}

$localeData->numberSymbols[(string)$symbolsNode['numberSystem']] = $symbolsList;
$localeData->numberSymbols[$thisNumberingSystem] = $symbolsList;
}
}
// Decimal patterns (by numbering system)
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Localization/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function getNumericIsoCode()
public function getSymbol($localeCode)
{
if (!isset($this->symbols[$localeCode])) {
throw new LocalizationException("Unknown locale code");
throw new LocalizationException("Unknown locale code : $localeCode");
}

return $this->symbols[$localeCode];
Expand All @@ -203,7 +203,7 @@ public function getDecimalPrecision()
public function getName($localeCode)
{
if (!isset($this->names[$localeCode])) {
throw new LocalizationException("Unknown locale code");
throw new LocalizationException("Unknown locale code : $localeCode");
}

return $this->names[$localeCode];
Expand Down
25 changes: 20 additions & 5 deletions src/Core/Localization/Currency/CurrencyDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace PrestaShop\PrestaShop\Core\Localization\Currency;

use PrestaShop\PrestaShop\Core\Localization\Currency\DataLayer\CurrencyInstalled as CurrencyInstalledDataLayer;
use PrestaShop\PrestaShop\Core\Localization\Currency\DataLayer\CurrencyInstalled;

/**
* Localization CurrencyData source
Expand All @@ -51,10 +52,12 @@ class CurrencyDataSource implements DataSourceInterface
* This top layer might be chained with lower layers and will be the entry point of this middleware stack.
*
* @param CurrencyDataLayerInterface $topLayer
* @param CurrencyInstalled $installedDataLayer
*/
public function __construct(CurrencyDataLayerInterface $topLayer)
public function __construct(CurrencyDataLayerInterface $topLayer, CurrencyInstalled $installedDataLayer)
{
$this->topLayer = $topLayer;
$this->topLayer = $topLayer;
$this->installedDataLayer = $installedDataLayer;
}

/**
Expand All @@ -70,13 +73,25 @@ public function getDataByCurrencyCode($currencyCode)
return $this->topLayer->read($currencyCode);
}

public function isCurrencyInstalled($currencyCode)
public function isCurrencyAvailable($currencyCode)
{
return $this->installedDataLayer->isInstalled($currencyCode);
}

public function getInstalledCurrencies()
/**
* Get all the available (installed + active) currencies' data
*
* @return CurrencyData[]
* The available currencies' data
*/
public function getAvailableCurrenciesData()
{
// TODO
$currencyCodes = $this->installedDataLayer->getAvailableCurrencyCodes();
$currenciesData = [];
foreach ($currencyCodes as $currencyCode) {
$currenciesData[] = $this->getDataByCurrencyCode($currencyCode);
}

return $currenciesData;
}
}
11 changes: 3 additions & 8 deletions src/Core/Localization/Currency/DataLayer/CurrencyInstalled.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,10 @@ public function isInstalled($currencyCode)
*
* @return string[]
*/
public function getInstalledCurrencyCodes()
public function getAvailableCurrencyCodes()
{
$currencies = $this->dataProvider->getCurrencies();

$extractIsoCodes = function (Currency $currency) {
return $currency->iso_code;
};

$currencyIds = array_map($extractIsoCodes, $currencies);
$currencies = $this->dataProvider->getCurrencies();
$currencyIds = array_column($currencies, 'iso_code');

return $currencyIds;
}
Expand Down
16 changes: 16 additions & 0 deletions src/Core/Localization/Currency/DataSourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,20 @@ interface DataSourceInterface
* The currency data
*/
public function getDataByCurrencyCode($currencyCode);

/**
* @param $currencyCode
*
* @return bool
* True if currency is installed
*/
public function isCurrencyAvailable($currencyCode);

/**
* Get all the available (installed + active) currencies' data
*
* @return CurrencyData[]
* The available currencies' data
*/
public function getAvailableCurrenciesData();
}
23 changes: 20 additions & 3 deletions src/Core/Localization/Currency/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class Repository implements CurrencyRepositoryInterface
*/
protected $dataSource;


public function __construct(CurrencyDataSourceInterface $dataSource)
{
$this->dataSource = $dataSource;
Expand Down Expand Up @@ -79,10 +78,28 @@ public function getCurrency($currencyCode)
}

/**
* Get all the available currencies (installed + active)
*
* @return CurrencyCollection
* The available currencies
*/
public function getInstalledCurrencies()
public function getAvailableCurrencies()
{
$currenciesData = $this->dataSource->getInstalledCurrencies(); // TODO
$currencies = new CurrencyCollection();
$currenciesData = $this->dataSource->getAvailableCurrenciesData();

foreach ($currenciesData as $currencyDatum) {
$currencies->add(new Currency(
$currencyDatum->isActive,
$currencyDatum->conversionRate,
$currencyDatum->isoCode,
$currencyDatum->numericIsoCode,
$currencyDatum->symbols,
$currencyDatum->precision,
$currencyDatum->names
));
}

return $currencies;
}
}
8 changes: 8 additions & 0 deletions src/Core/Localization/Currency/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ interface RepositoryInterface
* The wanted Currency instance
*/
public function getCurrency($currencyCode);

/**
* Get all the available currencies (installed + active)
*
* @return CurrencyCollection
* The available currencies
*/
public function getAvailableCurrencies();
}
16 changes: 13 additions & 3 deletions src/Core/Localization/Locale/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ class Repository implements RepositoryInterface
*/
protected $currencyRepository;

/**
* The current locale code will be used to localize (translate) provided data.
* The provided Locale instances' data will be translated in this locale.
*
* @var string
*/
protected $currentLocaleCode;

/**
* Rounding mode to use when formatting numbers
* Possible values are listed in PrestaShop\Decimal\Operation\Rounding::ROUND_* constants
Expand Down Expand Up @@ -117,6 +125,7 @@ class Repository implements RepositoryInterface
public function __construct(
CldrLocaleRepository $cldrLocaleRepository,
CurrencyRepository $currencyRepository,
$currentLocaleCode,
$roundingMode = Rounding::ROUND_HALF_UP,
$numberingSystem = Locale::NUMBERING_SYSTEM_LATIN,
$currencyDisplayType = PriceSpecification::CURRENCY_DISPLAY_SYMBOL,
Expand All @@ -125,6 +134,7 @@ public function __construct(
) {
$this->cldrLocaleRepository = $cldrLocaleRepository;
$this->currencyRepository = $currencyRepository;
$this->currentLocaleCode = $currentLocaleCode;
$this->roundingMode = $roundingMode;
$this->numberingSystem = $numberingSystem;
$this->currencyDisplayType = $currencyDisplayType;
Expand Down Expand Up @@ -170,7 +180,7 @@ protected function getNumberSpecification($localeCode)
throw new LocalizationException('CLDR locale not found for locale code "' . $localeCode . '"');
}

return (new SpecificationFactory)->buildNumberSpecification(
return (new SpecificationFactory($this->currentLocaleCode))->buildNumberSpecification(
$cldrLocale,
$this->maxFractionDigits,
$this->numberGroupingUsed
Expand Down Expand Up @@ -198,12 +208,12 @@ protected function getPriceSpecifications($localeCode)
throw new LocalizationException('CLDR locale not found for locale code "' . $localeCode . '"');
}

$currencies = $this->currencyRepository->getInstalledCurrencies();
$currencies = $this->currencyRepository->getAvailableCurrencies();

$priceSpecifications = new PriceSpecificationMap();
foreach ($currencies as $currency) {
// Build the spec
$thisPriceSpecification = (new SpecificationFactory)->buildPriceSpecification(
$thisPriceSpecification = (new SpecificationFactory($this->currentLocaleCode))->buildPriceSpecification(
$cldrLocale,
$currency,
$localeCode,
Expand Down
14 changes: 13 additions & 1 deletion src/Core/Localization/Specification/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@
*/
class Factory
{
/**
* Provided specification instances contain data that must be translated in a given language.
* This locale code is the language in which specification data will be localized.
*
* @var string
*/
protected $currentLocaleCode;

public function __construct($currentLocaleCode)
{
$this->currentLocaleCode = $currentLocaleCode;
}
/**
* Build a Number specification from a CLDR Locale object
*
Expand Down Expand Up @@ -117,7 +129,7 @@ public function buildPriceSpecification(
$this->getPrimaryGroupSize($currencyPattern),
$this->getSecondaryGroupSize($currencyPattern),
$currencyDisplayType,
$currency->getSymbol($localeCode),
$currency->getSymbol($this->currentLocaleCode),
$currency->getIsoCode()
);
}
Expand Down
24 changes: 13 additions & 11 deletions src/Core/Localization/Specification/NumberSymbolList.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,67 +322,69 @@ protected function validateData()
if (!isset($this->decimal)
|| !is_string($this->decimal)
) {
throw new LocalizationException('Invalid decimal');
throw new LocalizationException('Invalid decimal : ' . var_export($this->decimal, true));
}

if (!isset($this->group)
|| !is_string($this->group)
) {
throw new LocalizationException('Invalid group');
throw new LocalizationException('Invalid group : ' . var_export($this->group, true));
}

if (!isset($this->list)
|| !is_string($this->list)
) {
throw new LocalizationException('Invalid list');
throw new LocalizationException('Invalid symbols list : ' . var_export($this->list, true));
}

if (!isset($this->percentSign)
|| !is_string($this->percentSign)
) {
throw new LocalizationException('Invalid percentSign');
throw new LocalizationException('Invalid percentSign : ' . var_export($this->percentSign, true));
}

if (!isset($this->minusSign)
|| !is_string($this->minusSign)
) {
throw new LocalizationException('Invalid minusSign');
throw new LocalizationException('Invalid minusSign : ' . var_export($this->minusSign, true));
}

if (!isset($this->plusSign)
|| !is_string($this->plusSign)
) {
throw new LocalizationException('Invalid plusSign');
throw new LocalizationException('Invalid plusSign : ' . var_export($this->plusSign, true));
}

if (!isset($this->exponential)
|| !is_string($this->exponential)
) {
throw new LocalizationException('Invalid exponential');
throw new LocalizationException('Invalid exponential : ' . var_export($this->exponential, true));
}

if (!isset($this->superscriptingExponent)
|| !is_string($this->superscriptingExponent)
) {
throw new LocalizationException('Invalid superscriptingExponent');
throw new LocalizationException(
'Invalid superscriptingExponent : ' . var_export($this->superscriptingExponent, true)
);
}

if (!isset($this->perMille)
|| !is_string($this->perMille)
) {
throw new LocalizationException('Invalid perMille');
throw new LocalizationException('Invalid perMille : ' . var_export($this->perMille, true));
}

if (!isset($this->infinity)
|| !is_string($this->infinity)
) {
throw new LocalizationException('Invalid infinity');
throw new LocalizationException('Invalid infinity : ' . var_export($this->infinity, true));
}

if (!isset($this->nan)
|| !is_string($this->nan)
) {
throw new LocalizationException('Invalid nan');
throw new LocalizationException('Invalid nan : ' . var_export($this->nan, true));
}
}
}
Loading

0 comments on commit 8295933

Please sign in to comment.