System = Locale::NUMBERING_SYSTEM_LATIN, $currencyDisplayType = PriceSpecification::CURRENCY_DISPLAY_SYMBOL, $groupingUsed = true, $maxFractionDigits = self::MAX_FRACTION_DIGITS ) { $this->cldrLocaleRepository = $cldrLocaleRepository; $this->currencyRepository = $currencyRepository; $this->roundingMode = $roundingMode; $this->numberingSystem = $numberingSystem; $this->currencyDisplayType = $currencyDisplayType; $this->numberGroupingUsed = $groupingUsed; $this->maxFractionDigits = $maxFractionDigits; $this->specificationFactory = new SpecificationFactory(); } /** * {@inheritdoc} */ public function getLocale($localeCode) { if (!isset($this->locales[$localeCode])) { $this->locales[$localeCode] = new Locale( $localeCode, $this->getNumberSpecification($localeCode), $this->getPriceSpecifications($localeCode), new NumberFormatter($this->roundingMode, $this->numberingSystem) ); } return $this->locales[$localeCode]; } /** * Get the Number specification for a given locale. * * @param string $localeCode * The locale code (simplified IETF tag syntax) * Combination of ISO 639-1 (2-letters language code) and ISO 3166-2 (2-letters region code) * eg: fr-FR, en-US * * @return NumberSpecification * A Number specification * * @throws LocalizationException */ protected function getNumberSpecification($localeCode) { $cldrLocale = $this->cldrLocaleRepository->getLocale($localeCode); if (null === $cldrLocale) { throw new LocalizationException('CLDR locale not found for locale code "' . $localeCode . '"'); } return $this->specificationFactory->buildNumberSpecification( $cldrLocale, $this->maxFractionDigits, $this->numberGroupingUsed ); } /** * Get all the Price specifications for a given locale. * Each installed currency has its own Price specification. * * @param string $localeCode * The locale code (simplified IETF tag syntax) * Combination of ISO 639-1 (2-letters language code) and ISO 3166-2 (2-letters region code) * eg: fr-FR, en-US * * @return PriceSpecificationMap * All installed currencies' Price specifications * * @throws LocalizationException */ protected function getPriceSpecifications($localeCode) { $cldrLocale = $this->cldrLocaleRepository->getLocale($localeCode); if (null === $cldrLocale) { throw new LocalizationException('CLDR locale not found for locale code "' . $localeCode . '"'); } $currencies = $this->currencyRepository->getAllInstalledCurrencies($localeCode); $priceSpecifications = new PriceSpecificationMap(); foreach ($currencies as $currency) { // Build the spec $thisPriceSpecification = (new SpecificationFactory())->buildPriceSpecification( $localeCode, $cldrLocale, $currency, $this->numberGroupingUsed, $this->currencyDisplayType, (int) $currency->getDecimalPrecision() ); // Add the spec to the collection $priceSpecifications->add( $thisPriceSpecification->getCurrencyCode(), $thisPriceSpecification ); } return $priceSpecifications; } /** * @return bool */ public function isNumberGroupingUsed() { return $this->numberGroupingUsed; } /** * @return string */ public function getCurrencyDisplayType() { return $this->currencyDisplayType; } }