| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard; |
| 4 |
|
| 5 |
use NumberFormatter; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Exception; |
| 7 |
|
| 8 |
class Accounting extends CurrencyBase |
| 9 |
{ |
| 10 |
protected ?bool $overrideSpacing = true; |
| 11 |
|
| 12 |
protected ?CurrencyNegative $overrideNegative = CurrencyNegative::parentheses; |
| 13 |
|
| 14 |
/** |
| 15 |
* @throws Exception if the Intl extension and ICU version don't support Accounting formats |
| 16 |
*/ |
| 17 |
protected function getLocaleFormat(): string |
| 18 |
{ |
| 19 |
if (version_compare(PHP_VERSION, '7.4.1', '<')) { |
| 20 |
// @codeCoverageIgnoreStart |
| 21 |
throw new Exception('The Intl extension does not support Accounting Formats below PHP 7.4.1'); |
| 22 |
// @codeCoverageIgnoreEnd |
| 23 |
} |
| 24 |
|
| 25 |
if (self::icuVersion() < 53.0) { |
| 26 |
// @codeCoverageIgnoreStart |
| 27 |
throw new Exception('The Intl extension does not support Accounting Formats without ICU 53'); |
| 28 |
// @codeCoverageIgnoreEnd |
| 29 |
} |
| 30 |
|
| 31 |
// Scrutinizer does not recognize CURRENCY_ACCOUNTING |
| 32 |
$formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY_ACCOUNTING); |
| 33 |
$mask = $formatter->format($this->stripLeadingRLM); |
| 34 |
if ($this->decimals === 0) { |
| 35 |
$mask = (string) preg_replace('/\.0+/miu', '', $mask); |
| 36 |
} |
| 37 |
|
| 38 |
return str_replace('¤', $this->formatCurrencyCode(), $mask); |
| 39 |
} |
| 40 |
|
| 41 |
public static function icuVersion(): float |
| 42 |
{ |
| 43 |
[$major, $minor] = explode('.', INTL_ICU_VERSION); |
| 44 |
|
| 45 |
return (float) "{$major}.{$minor}"; |
| 46 |
} |
| 47 |
|
| 48 |
private function formatCurrencyCode(): string |
| 49 |
{ |
| 50 |
if ($this->locale === null) { |
| 51 |
return $this->currencyCode . '*'; |
| 52 |
} |
| 53 |
|
| 54 |
return "[\${$this->currencyCode}-{$this->locale}]"; |
| 55 |
} |
| 56 |
} |
| 57 |
|