| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard; |
| 4 |
|
| 5 |
use NumberFormatter; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Exception; |
| 7 |
|
| 8 |
class CurrencyBase extends Number |
| 9 |
{ |
| 10 |
public const LEADING_SYMBOL = true; |
| 11 |
|
| 12 |
public const TRAILING_SYMBOL = false; |
| 13 |
|
| 14 |
public const SYMBOL_WITH_SPACING = true; |
| 15 |
|
| 16 |
public const SYMBOL_WITHOUT_SPACING = false; |
| 17 |
|
| 18 |
protected string $currencyCode = '$'; |
| 19 |
|
| 20 |
protected bool $currencySymbolPosition = self::LEADING_SYMBOL; |
| 21 |
|
| 22 |
protected bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING; |
| 23 |
|
| 24 |
protected const DEFAULT_STRIP_LEADING_RLM = false; |
| 25 |
|
| 26 |
protected bool $stripLeadingRLM = self::DEFAULT_STRIP_LEADING_RLM; |
| 27 |
|
| 28 |
public const DEFAULT_NEGATIVE = CurrencyNegative::minus; |
| 29 |
|
| 30 |
protected CurrencyNegative $negative = CurrencyNegative::minus; |
| 31 |
|
| 32 |
protected ?bool $overrideSpacing = null; |
| 33 |
|
| 34 |
protected ?CurrencyNegative $overrideNegative = null; |
| 35 |
|
| 36 |
// Not sure why original code uses nbsp |
| 37 |
private string $spaceOrNbsp = ' '; // or "\u{a0}" |
| 38 |
/** |
| 39 |
* @param string $currencyCode the currency symbol or code to display for this mask |
| 40 |
* @param int $decimals number of decimal places to display, in the range 0-30 |
| 41 |
* @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not |
| 42 |
* @param bool $currencySymbolPosition indicates whether the currency symbol comes before or after the value |
| 43 |
* Possible values are Currency::LEADING_SYMBOL and Currency::TRAILING_SYMBOL |
| 44 |
* @param bool $currencySymbolSpacing indicates whether there is spacing between the currency symbol and the value |
| 45 |
* Possible values are Currency::SYMBOL_WITH_SPACING and Currency::SYMBOL_WITHOUT_SPACING |
| 46 |
* However, Currency always uses WITHOUT and Accounting always uses WITH |
| 47 |
* @param ?string $locale Set the locale for the currency format; or leave as the default null. |
| 48 |
* If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF). |
| 49 |
* Note that setting a locale will override any other settings defined in this class |
| 50 |
* other than the currency code; or decimals (unless the decimals value is set to 0). |
| 51 |
* @param bool $stripLeadingRLM remove leading RLM added with |
| 52 |
* ICU 72.1+. |
| 53 |
* @param mixed $negative How to display negative numbers. |
| 54 |
* Always use parentheses for Accounting. |
| 55 |
* 4 options for Currency. |
| 56 |
* |
| 57 |
* @throws Exception If a provided locale code is not a valid format |
| 58 |
* @param \TablePress\PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\CurrencyNegative::* $negative |
| 59 |
*/ |
| 60 |
public function __construct( |
| 61 |
string $currencyCode = '$', |
| 62 |
int $decimals = 2, |
| 63 |
bool $thousandsSeparator = true, |
| 64 |
bool $currencySymbolPosition = self::LEADING_SYMBOL, |
| 65 |
bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING, |
| 66 |
?string $locale = null, |
| 67 |
bool $stripLeadingRLM = self::DEFAULT_STRIP_LEADING_RLM, |
| 68 |
$negative = CurrencyNegative::minus |
| 69 |
) { |
| 70 |
$this->setCurrencyCode($currencyCode); |
| 71 |
$this->setThousandsSeparator($thousandsSeparator); |
| 72 |
$this->setDecimals($decimals); |
| 73 |
$this->setCurrencySymbolPosition($currencySymbolPosition); |
| 74 |
$this->setCurrencySymbolSpacing($currencySymbolSpacing); |
| 75 |
$this->setLocale($locale); |
| 76 |
$this->stripLeadingRLM = $stripLeadingRLM; |
| 77 |
$this->negative = $negative; |
| 78 |
} |
| 79 |
|
| 80 |
public function setCurrencyCode(string $currencyCode): void |
| 81 |
{ |
| 82 |
$this->currencyCode = $currencyCode; |
| 83 |
} |
| 84 |
|
| 85 |
public function setCurrencySymbolPosition(bool $currencySymbolPosition = self::LEADING_SYMBOL): void |
| 86 |
{ |
| 87 |
$this->currencySymbolPosition = $currencySymbolPosition; |
| 88 |
} |
| 89 |
|
| 90 |
public function setCurrencySymbolSpacing(bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING): void |
| 91 |
{ |
| 92 |
$this->currencySymbolSpacing = $currencySymbolSpacing; |
| 93 |
} |
| 94 |
|
| 95 |
public function setStripLeadingRLM(bool $stripLeadingRLM): void |
| 96 |
{ |
| 97 |
$this->stripLeadingRLM = $stripLeadingRLM; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @param mixed $negative |
| 102 |
*/ |
| 103 |
public function setNegative($negative): void |
| 104 |
{ |
| 105 |
$this->negative = $negative; |
| 106 |
} |
| 107 |
|
| 108 |
protected function getLocaleFormat(): string |
| 109 |
{ |
| 110 |
$formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY); |
| 111 |
$mask = $formatter->format($this->stripLeadingRLM); |
| 112 |
if ($this->decimals === 0) { |
| 113 |
$mask = (string) preg_replace('/\.0+/miu', '', $mask); |
| 114 |
} |
| 115 |
|
| 116 |
return str_replace('¤', $this->formatCurrencyCode(), $mask); |
| 117 |
} |
| 118 |
|
| 119 |
private function formatCurrencyCode(): string |
| 120 |
{ |
| 121 |
if ($this->locale === null) { |
| 122 |
return $this->currencyCode; |
| 123 |
} |
| 124 |
|
| 125 |
return "[\${$this->currencyCode}-{$this->locale}]"; |
| 126 |
} |
| 127 |
|
| 128 |
public function format(): string |
| 129 |
{ |
| 130 |
if ($this->localeFormat !== null) { |
| 131 |
return $this->localeFormat; |
| 132 |
} |
| 133 |
$symbolWithSpacing = $this->overrideSpacing ?? ($this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING); |
| 134 |
$negative = $this->overrideNegative ?? $this->negative; |
| 135 |
|
| 136 |
// format if positive |
| 137 |
$format = '_('; |
| 138 |
if ($this->currencySymbolPosition === self::LEADING_SYMBOL) { |
| 139 |
$format .= '"' . $this->currencyCode . '"'; |
| 140 |
if (preg_match('/^[A-Z]{3}$/i', $this->currencyCode) === 1) { |
| 141 |
$format .= $this->spaceOrNbsp; |
| 142 |
} |
| 143 |
if (preg_match('/^[A-Z]{3}$/i', $this->currencyCode) === 1) { |
| 144 |
$format .= $this->spaceOrNbsp; |
| 145 |
} |
| 146 |
if ($symbolWithSpacing) { |
| 147 |
$format .= '*' . $this->spaceOrNbsp; |
| 148 |
} |
| 149 |
} |
| 150 |
$format .= $this->thousandsSeparator ? '#,##0' : '0'; |
| 151 |
if ($this->decimals > 0) { |
| 152 |
$format .= '.' . str_repeat('0', $this->decimals); |
| 153 |
} |
| 154 |
if ($this->currencySymbolPosition === self::TRAILING_SYMBOL) { |
| 155 |
if ($symbolWithSpacing) { |
| 156 |
$format .= $this->spaceOrNbsp; |
| 157 |
} elseif (preg_match('/^[A-Z]{3}$/i', $this->currencyCode) === 1) { |
| 158 |
$format .= $this->spaceOrNbsp; |
| 159 |
} |
| 160 |
$format .= '[$' . $this->currencyCode . ']'; |
| 161 |
} |
| 162 |
$format .= '_)'; |
| 163 |
|
| 164 |
// format if negative |
| 165 |
$format .= ';_('; |
| 166 |
$format .= $negative->color(); |
| 167 |
$negativeStart = $negative->start(); |
| 168 |
if ($this->currencySymbolPosition === self::LEADING_SYMBOL) { |
| 169 |
if ($negativeStart === '-' && !$symbolWithSpacing) { |
| 170 |
$format .= $negativeStart; |
| 171 |
} |
| 172 |
$format .= '"' . $this->currencyCode . '"'; |
| 173 |
if (preg_match('/^[A-Z]{3}$/i', $this->currencyCode) === 1) { |
| 174 |
$format .= $this->spaceOrNbsp; |
| 175 |
} |
| 176 |
if ($symbolWithSpacing) { |
| 177 |
$format .= '*' . $this->spaceOrNbsp; |
| 178 |
} |
| 179 |
if ($negativeStart === '\(' || ($symbolWithSpacing && $negativeStart === '-')) { |
| 180 |
$format .= $negativeStart; |
| 181 |
} |
| 182 |
} else { |
| 183 |
$format .= $negative->start(); |
| 184 |
} |
| 185 |
$format .= $this->thousandsSeparator ? '#,##0' : '0'; |
| 186 |
if ($this->decimals > 0) { |
| 187 |
$format .= '.' . str_repeat('0', $this->decimals); |
| 188 |
} |
| 189 |
$format .= $negative->end(); |
| 190 |
if ($this->currencySymbolPosition === self::TRAILING_SYMBOL) { |
| 191 |
if ($symbolWithSpacing) { |
| 192 |
// Do nothing - I can't figure out how to get |
| 193 |
// everything to align if I put any kind of space here. |
| 194 |
//$format .= "\u{2009}"; |
| 195 |
} elseif (preg_match('/^[A-Z]{3}$/i', $this->currencyCode) === 1) { |
| 196 |
$format .= $this->spaceOrNbsp; |
| 197 |
} |
| 198 |
$format .= '[$' . $this->currencyCode . ']'; |
| 199 |
} |
| 200 |
if ($this->currencySymbolPosition === self::TRAILING_SYMBOL) { |
| 201 |
$format .= '_)'; |
| 202 |
} elseif ($symbolWithSpacing && $negativeStart === '-') { |
| 203 |
$format .= ' '; |
| 204 |
} |
| 205 |
// format if zero |
| 206 |
$format .= ';_('; |
| 207 |
if ($this->currencySymbolPosition === self::LEADING_SYMBOL) { |
| 208 |
$format .= '"' . $this->currencyCode . '"'; |
| 209 |
} |
| 210 |
if ($symbolWithSpacing) { |
| 211 |
if ($this->currencySymbolPosition === self::LEADING_SYMBOL) { |
| 212 |
$format .= '*' . $this->spaceOrNbsp; |
| 213 |
} |
| 214 |
$format .= '"-"'; |
| 215 |
if ($this->decimals > 0) { |
| 216 |
$format .= str_repeat('?', $this->decimals); |
| 217 |
} |
| 218 |
} else { |
| 219 |
if (preg_match('/^[A-Z]{3}$/i', $this->currencyCode) === 1) { |
| 220 |
$format .= $this->spaceOrNbsp; |
| 221 |
} |
| 222 |
$format .= '0'; |
| 223 |
if ($this->decimals > 0) { |
| 224 |
$format .= '.' . str_repeat('0', $this->decimals); |
| 225 |
} |
| 226 |
} |
| 227 |
if ($this->currencySymbolPosition === self::TRAILING_SYMBOL) { |
| 228 |
if ($symbolWithSpacing) { |
| 229 |
$format .= $this->spaceOrNbsp; |
| 230 |
} |
| 231 |
$format .= '[$' . $this->currencyCode . ']'; |
| 232 |
} |
| 233 |
$format .= '_)'; |
| 234 |
// format if text |
| 235 |
$format .= ';_(@_)'; |
| 236 |
|
| 237 |
return $format; |
| 238 |
} |
| 239 |
} |
| 240 |
|