public function __construct(NumberFormatInterface $numberFormat, $style = self::DECIMAL)
{
if (!extension_loaded('bcmath')) {
throw new \RuntimeException('The bcmath extension is required by NumberFormatter.');
}
$availablePatterns = [self::DECIMAL => $numberFormat->getDecimalPattern(), self::PERCENT => $numberFormat->getPercentPattern(), self::CURRENCY => $numberFormat->getCurrencyPattern(), self::CURRENCY_ACCOUNTING => $numberFormat->getAccountingCurrencyPattern()];
if (!array_key_exists($style, $availablePatterns)) {
// Unknown type.
throw new InvalidArgumentException('Unknown format style provided to NumberFormatter::__construct().');
}
// Split the selected pattern into positive and negative patterns.
$patterns = explode(';', $availablePatterns[$style]);
if (!isset($patterns[1])) {
// No explicit negative pattern was provided, construct it.
$patterns[1] = '-' . $patterns[0];
}
$this->numberFormat = $numberFormat;
$this->positivePattern = $patterns[0];
$this->negativePattern = $patterns[1];
$this->groupingUsed = strpos($this->positivePattern, ',') !== false;
// This pattern has number groups, parse them.
if ($this->groupingUsed) {
preg_match('/#+0/', $this->positivePattern, $primaryGroupMatches);
$this->primaryGroupSize = $this->secondaryGroupSize = strlen($primaryGroupMatches[0]);
$numberGroups = explode(',', $this->positivePattern);
if (count($numberGroups) > 2) {
// This pattern has a distinct secondary group size.
$this->secondaryGroupSize = strlen($numberGroups[1]);
}
}
// Initialize the fraction digit settings for decimal and percent
// styles only. The currency ones will default to the currency values.
if (in_array($style, [self::DECIMAL, self::PERCENT])) {
$this->minimumFractionDigits = 0;
$this->maximumFractionDigits = 3;
}
$this->currencyDisplay = self::CURRENCY_DISPLAY_SYMBOL;
}