setCurrencyCode($currencyCode); $this->setThousandsSeparator($thousandsSeparator); $this->setDecimals($decimals); $this->setCurrencySymbolPosition($currencySymbolPosition); $this->setCurrencySymbolSpacing($currencySymbolSpacing); $this->setLocale($locale); } /** * @throws Exception if the Intl extension and ICU version don't support Accounting formats */ protected function getLocaleFormat(): string { if (version_compare(PHP_VERSION, '7.4.1', '<')) { throw new Exception('The Intl extension does not support Accounting Formats below PHP 7.4.1'); } if ($this->icuVersion() < 53.0) { throw new Exception('The Intl extension does not support Accounting Formats without ICU 53'); } // Scrutinizer does not recognize CURRENCY_ACCOUNTING $formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY_ACCOUNTING); $mask = $formatter->format(); if ($this->decimals === 0) { $mask = (string) preg_replace('/\.0+/miu', '', $mask); } return str_replace('ยค', $this->formatCurrencyCode(), $mask); } private function icuVersion(): float { [$major, $minor] = explode('.', INTL_ICU_VERSION); return (float) "{$major}.{$minor}"; } private function formatCurrencyCode(): string { if ($this->locale === null) { return $this->currencyCode . '*'; } return "[\${$this->currencyCode}-{$this->locale}]"; } public function format(): string { if ($this->localeFormat !== null) { return $this->localeFormat; } return sprintf( '_-%s%s%s0%s%s%s_-', $this->currencySymbolPosition === self::LEADING_SYMBOL ? $this->formatCurrencyCode() : null, ( $this->currencySymbolPosition === self::LEADING_SYMBOL && $this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING ) ? "\u{a0}" : '', $this->thousandsSeparator ? '#,##' : null, $this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null, ( $this->currencySymbolPosition === self::TRAILING_SYMBOL && $this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING ) ? "\u{a0}" : '', $this->currencySymbolPosition === self::TRAILING_SYMBOL ? $this->formatCurrencyCode() : null ); } }