Skip to content

Commit 05847dc

Browse files
committed
Use enum for Negative Formats
1 parent 9d5c676 commit 05847dc

File tree

7 files changed

+84
-64
lines changed

7 files changed

+84
-64
lines changed

samples/Basic4/52_Currency.php

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\Accounting;
88
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\Currency;
99
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\CurrencyBase;
10+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\CurrencyNegative;
1011

1112
$spreadsheet = new Spreadsheet();
1213

@@ -66,45 +67,45 @@
6667

6768
$sheet->getCell('A6')->setValue('AcctWiz¥');
6869
$sheet->getCell('E6')->setValue(true);
69-
$sheet->getCell('F6')->setValue(Currency::NEGATIVE_MINUS);
70+
$sheet->getCell('F6')->setValue(CurrencyNegative::minus->name);
7071
$sheet->getCell('G6')->setValue(1234.56);
7172
$sheet->getCell('H6')->setValue(0);
7273
$sheet->getCell('I6')->setValue(-1234.56);
7374
$sheet->getCell('J6')->setValue('Text');
7475
$sheet->getStyle('G6:J6')->applyFromArray(
7576
[
7677
'numberFormat' => [
77-
'formatCode' => (new Accounting('', currencySymbolSpacing: true, negative: Currency::NEGATIVE_MINUS))->format(),
78+
'formatCode' => (new Accounting('', currencySymbolSpacing: true, negative: CurrencyNegative::minus))->format(),
7879
],
7980
]
8081
);
8182

8283
$sheet->getCell('A7')->setValue('AcctWiz¥');
8384
$sheet->getCell('E7')->setValue(false);
84-
$sheet->getCell('F7')->setValue(Currency::NEGATIVE_MINUS);
85+
$sheet->getCell('F7')->setValue(CurrencyNegative::minus->name);
8586
$sheet->getCell('G7')->setValue(1234.56);
8687
$sheet->getCell('H7')->setValue(0);
8788
$sheet->getCell('I7')->setValue(-1234.56);
8889
$sheet->getCell('J7')->setValue('Text');
8990
$sheet->getStyle('G7:J7')->applyFromArray(
9091
[
9192
'numberFormat' => [
92-
'formatCode' => (new Accounting('', currencySymbolSpacing: false, negative: Currency::NEGATIVE_MINUS))->format(),
93+
'formatCode' => (new Accounting('', currencySymbolSpacing: false, negative: CurrencyNegative::minus))->format(),
9394
],
9495
]
9596
);
9697

9798
$sheet->getCell('A8')->setValue('AcctWiz¥');
9899
$sheet->getCell('E8')->setValue(false);
99-
$sheet->getCell('F8')->setValue(Currency::NEGATIVE_PARENS);
100+
$sheet->getCell('F8')->setValue(CurrencyNegative::parentheses->name);
100101
$sheet->getCell('G8')->setValue(1234.56);
101102
$sheet->getCell('H8')->setValue(0);
102103
$sheet->getCell('I8')->setValue(-1234.56);
103104
$sheet->getCell('J8')->setValue('Text');
104105
$sheet->getStyle('G8:J8')->applyFromArray(
105106
[
106107
'numberFormat' => [
107-
'formatCode' => (new Accounting('', currencySymbolSpacing: false, negative: Currency::NEGATIVE_PARENS))->format(),
108+
'formatCode' => (new Accounting('', currencySymbolSpacing: false, negative: CurrencyNegative::parentheses))->format(),
108109
],
109110
]
110111
);
@@ -125,15 +126,16 @@
125126

126127
$sheet->getCell('A10')->setValue('AcctW HUF');
127128
$sheet->getCell('E10')->setValue(true);
128-
$sheet->getCell('F10')->setValue(Currency::NEGATIVE_RED_PARENS);
129+
$sheet->getCell('F10')->setValue(CurrencyNegative::redParentheses->name);
130+
$sheet->getStyle('F10')->getFont()->getColor()->setRgb('FF0000');
129131
$sheet->getCell('G10')->setValue(1234.56);
130132
$sheet->getCell('H10')->setValue(0);
131133
$sheet->getCell('I10')->setValue(-1234.56);
132134
$sheet->getCell('J10')->setValue('Text');
133135
$sheet->getStyle('G10:J10')->applyFromArray(
134136
[
135137
'numberFormat' => [
136-
'formatCode' => (new Accounting('HUF', currencySymbolSpacing: true, negative: Currency::NEGATIVE_RED_PARENS))->format(),
138+
'formatCode' => (new Accounting('HUF', currencySymbolSpacing: true, negative: CurrencyNegative::redParentheses))->format(),
137139
],
138140
]
139141
);
@@ -156,7 +158,8 @@
156158
$sheet->getCell('B12')->setValue(3);
157159
$sheet->getCell('C12')->setValue(false);
158160
$sheet->getCell('D12')->setValue(false);
159-
$sheet->getCell('F12')->setValue(Currency::NEGATIVE_RED_MINUS);
161+
$sheet->getCell('F12')->setValue(CurrencyNegative::redMinus->name);
162+
$sheet->getStyle('F12')->getFont()->getColor()->setRgb('FF0000');
160163
$sheet->getCell('G12')->setValue(1234.56);
161164
$sheet->getCell('H12')->setValue(0);
162165
$sheet->getCell('I12')->setValue(-1234.56);
@@ -166,7 +169,7 @@
166169
decimals: 3,
167170
thousandsSeparator: false,
168171
currencySymbolPosition: Accounting::TRAILING_SYMBOL,
169-
negative: Currency::NEGATIVE_RED_MINUS
172+
negative: CurrencyNegative::redMinus
170173
);
171174

172175
$sheet->getStyle('G12:J12')->applyFromArray(
@@ -202,6 +205,7 @@
202205
);
203206

204207
$sheet->getColumnDimension('A')->setAutoSize(true);
208+
$sheet->getColumnDimension('F')->setAutoSize(true);
205209
$sheet->getColumnDimension('G')->setAutoSize(true);
206210
$sheet->getColumnDimension('I')->setAutoSize(true);
207211
$sheet->getColumnDimension('L')->setAutoSize(true);
@@ -263,45 +267,45 @@
263267

264268
$sheet->getCell('A6')->setValue('CurrWiz¥');
265269
$sheet->getCell('E6')->setValue(true);
266-
$sheet->getCell('F6')->setValue(Currency::NEGATIVE_MINUS);
270+
$sheet->getCell('F6')->setValue(CurrencyNegative::minus->name);
267271
$sheet->getCell('G6')->setValue(1234.56);
268272
$sheet->getCell('H6')->setValue(0);
269273
$sheet->getCell('I6')->setValue(-1234.56);
270274
$sheet->getCell('J6')->setValue('Text');
271275
$sheet->getStyle('G6:J6')->applyFromArray(
272276
[
273277
'numberFormat' => [
274-
'formatCode' => (new Currency('', currencySymbolSpacing: true, negative: Currency::NEGATIVE_MINUS))->format(),
278+
'formatCode' => (new Currency('', currencySymbolSpacing: true, negative: CurrencyNegative::minus))->format(),
275279
],
276280
]
277281
);
278282

279283
$sheet->getCell('A7')->setValue('CurrWiz¥');
280284
$sheet->getCell('E7')->setValue(false);
281-
$sheet->getCell('F7')->setValue(Currency::NEGATIVE_MINUS);
285+
$sheet->getCell('F7')->setValue(CurrencyNegative::minus->name);
282286
$sheet->getCell('G7')->setValue(1234.56);
283287
$sheet->getCell('H7')->setValue(0);
284288
$sheet->getCell('I7')->setValue(-1234.56);
285289
$sheet->getCell('J7')->setValue('Text');
286290
$sheet->getStyle('G7:J7')->applyFromArray(
287291
[
288292
'numberFormat' => [
289-
'formatCode' => (new Currency('', currencySymbolSpacing: false, negative: Currency::NEGATIVE_MINUS))->format(),
293+
'formatCode' => (new Currency('', currencySymbolSpacing: false, negative: CurrencyNegative::minus))->format(),
290294
],
291295
]
292296
);
293297

294298
$sheet->getCell('A8')->setValue('CurrWiz¥');
295299
$sheet->getCell('E8')->setValue(false);
296-
$sheet->getCell('F8')->setValue(Currency::NEGATIVE_PARENS);
300+
$sheet->getCell('F8')->setValue(CurrencyNegative::parentheses->name);
297301
$sheet->getCell('G8')->setValue(1234.56);
298302
$sheet->getCell('H8')->setValue(0);
299303
$sheet->getCell('I8')->setValue(-1234.56);
300304
$sheet->getCell('J8')->setValue('Text');
301305
$sheet->getStyle('G8:J8')->applyFromArray(
302306
[
303307
'numberFormat' => [
304-
'formatCode' => (new Currency('', currencySymbolSpacing: false, negative: Currency::NEGATIVE_PARENS))->format(),
308+
'formatCode' => (new Currency('', currencySymbolSpacing: false, negative: CurrencyNegative::parentheses))->format(),
305309
],
306310
]
307311
);
@@ -322,15 +326,16 @@
322326

323327
$sheet->getCell('A10')->setValue('CurrW HUF');
324328
$sheet->getCell('E10')->setValue(true);
325-
$sheet->getCell('F10')->setValue(Currency::NEGATIVE_RED_PARENS);
329+
$sheet->getCell('F10')->setValue(CurrencyNegative::redParentheses->name);
330+
$sheet->getStyle('F10')->getFont()->getColor()->setRgb('FF0000');
326331
$sheet->getCell('G10')->setValue(1234.56);
327332
$sheet->getCell('H10')->setValue(0);
328333
$sheet->getCell('I10')->setValue(-1234.56);
329334
$sheet->getCell('J10')->setValue('Text');
330335
$sheet->getStyle('G10:J10')->applyFromArray(
331336
[
332337
'numberFormat' => [
333-
'formatCode' => (new Currency('HUF', currencySymbolSpacing: true, negative: Currency::NEGATIVE_RED_PARENS))->format(),
338+
'formatCode' => (new Currency('HUF', currencySymbolSpacing: true, negative: CurrencyNegative::redParentheses))->format(),
334339
],
335340
]
336341
);
@@ -353,7 +358,8 @@
353358
$sheet->getCell('B12')->setValue(3);
354359
$sheet->getCell('C12')->setValue(false);
355360
$sheet->getCell('D12')->setValue(false);
356-
$sheet->getCell('F12')->setValue(Currency::NEGATIVE_RED_MINUS);
361+
$sheet->getCell('F12')->setValue(CurrencyNegative::redMinus->name);
362+
$sheet->getStyle('F12')->getFont()->getColor()->setRgb('FF0000');
357363
$sheet->getCell('G12')->setValue(1234.56);
358364
$sheet->getCell('H12')->setValue(0);
359365
$sheet->getCell('I12')->setValue(-1234.56);
@@ -363,7 +369,7 @@
363369
decimals: 3,
364370
thousandsSeparator: false,
365371
currencySymbolPosition: Currency::TRAILING_SYMBOL,
366-
negative: Currency::NEGATIVE_RED_MINUS
372+
negative: CurrencyNegative::redMinus
367373
);
368374

369375
$sheet->getStyle('G12:J12')->applyFromArray(
@@ -375,6 +381,7 @@
375381
);
376382

377383
$sheet->getColumnDimension('A')->setAutoSize(true);
384+
$sheet->getColumnDimension('F')->setAutoSize(true);
378385
$sheet->getColumnDimension('G')->setAutoSize(true);
379386
$sheet->getColumnDimension('H')->setAutoSize(true);
380387
$sheet->getColumnDimension('I')->setAutoSize(true);
@@ -406,31 +413,31 @@
406413

407414
$sheet->getCell('A3')->setValue('CurBase ¥');
408415
$sheet->getCell('E3')->setValue(true);
409-
$sheet->getCell('F3')->setValue(Currency::NEGATIVE_MINUS);
416+
$sheet->getCell('F3')->setValue(CurrencyNegative::minus->name);
410417
$sheet->getCell('G3')->setValue(1234.56);
411418
$sheet->getCell('H3')->setValue(0);
412419
$sheet->getCell('I3')->setValue(-1234.56);
413420
$sheet->getCell('J3')->setValue('Text');
414421
$sheet->getStyle('G3:J3')->applyFromArray(
415422
[
416423
'numberFormat' => [
417-
'formatCode' => (new CurrencyBase('', currencySymbolSpacing: true, negative: Currency::NEGATIVE_MINUS))->format(),
424+
'formatCode' => (new CurrencyBase('', currencySymbolSpacing: true, negative: CurrencyNegative::minus))->format(),
418425
],
419426
]
420427
);
421428
$sheet->getCell('G4')->setValue(-1234.56);
422429
$sheet->getStyle('G4')->applyFromArray(
423430
[
424431
'numberFormat' => [
425-
'formatCode' => (new CurrencyBase('', currencySymbolSpacing: true, negative: Currency::NEGATIVE_MINUS))->format(),
432+
'formatCode' => (new CurrencyBase('', currencySymbolSpacing: true, negative: CurrencyNegative::minus))->format(),
426433
],
427434
]
428435
);
429436
$sheet->getCell('G5')->setValue(0);
430437
$sheet->getStyle('G5')->applyFromArray(
431438
[
432439
'numberFormat' => [
433-
'formatCode' => (new CurrencyBase('', currencySymbolSpacing: true, negative: Currency::NEGATIVE_MINUS))->format(),
440+
'formatCode' => (new CurrencyBase('', currencySymbolSpacing: true, negative: CurrencyNegative::minus))->format(),
434441
],
435442
]
436443
);
@@ -457,13 +464,13 @@
457464
);
458465

459466
$sheet->getColumnDimension('A')->setAutoSize(true);
467+
$sheet->getColumnDimension('F')->setAutoSize(true);
460468
$sheet->getColumnDimension('G')->setAutoSize(true);
461469
$sheet->getColumnDimension('H')->setAutoSize(true);
462470
$sheet->getColumnDimension('I')->setAutoSize(true);
463471
$sheet->setSelectedCells('J1');
464472

465473
$spreadsheet->setActiveSheetIndex(0);
466474

467-
// Save
468475
$helper->write($spreadsheet, __FILE__, ['Xls', 'Xlsx']);
469476
$spreadsheet->disconnectWorksheets();

src/PhpSpreadsheet/Style/NumberFormat/Wizard/Accounting.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Accounting extends CurrencyBase
99
{
1010
protected ?bool $overrideSpacing = true;
1111

12-
protected ?string $overrideNegative = self::NEGATIVE_PARENS;
12+
protected ?CurrencyNegative $overrideNegative = CurrencyNegative::parentheses;
1313

1414
/**
1515
* @throws Exception if the Intl extension and ICU version don't support Accounting formats

src/PhpSpreadsheet/Style/NumberFormat/Wizard/Currency.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ class Currency extends CurrencyBase
66
{
77
protected ?bool $overrideSpacing = false;
88

9-
protected ?string $overrideNegative = null;
9+
protected ?CurrencyNegative $overrideNegative = null;
1010
}

src/PhpSpreadsheet/Style/NumberFormat/Wizard/CurrencyBase.php

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,13 @@ class CurrencyBase extends Number
2525

2626
protected bool $stripLeadingRLM = self::DEFAULT_STRIP_LEADING_RLM;
2727

28-
public const NEGATIVE_MINUS = '-';
29-
public const NEGATIVE_RED_MINUS = 'red-';
30-
public const NEGATIVE_PARENS = '()';
31-
public const NEGATIVE_RED_PARENS = 'red()';
28+
public const DEFAULT_NEGATIVE = CurrencyNegative::minus;
3229

33-
protected const NEGATIVE_START = [
34-
self::NEGATIVE_MINUS => '-',
35-
self::NEGATIVE_RED_MINUS => '-',
36-
self::NEGATIVE_PARENS => '\\(',
37-
self::NEGATIVE_RED_PARENS => '\\(',
38-
];
39-
40-
protected const NEGATIVE_END = [
41-
self::NEGATIVE_MINUS => '',
42-
self::NEGATIVE_RED_MINUS => '',
43-
self::NEGATIVE_PARENS => '\\)',
44-
self::NEGATIVE_RED_PARENS => '\\)',
45-
];
46-
47-
protected const NEGATIVE_COLOR = [
48-
self::NEGATIVE_RED_MINUS => '[Red]',
49-
self::NEGATIVE_RED_PARENS => '[Red]',
50-
];
51-
52-
public const DEFAULT_NEGATIVE = self::NEGATIVE_MINUS;
53-
54-
protected string $negative = self::NEGATIVE_MINUS;
30+
protected CurrencyNegative $negative = CurrencyNegative::minus;
5531

5632
protected ?bool $overrideSpacing = null;
5733

58-
protected ?string $overrideNegative = null;
34+
protected ?CurrencyNegative $overrideNegative = null;
5935

6036
// Not sure why original code uses nbsp
6137
private string $spaceOrNbsp = ' '; // or "\u{a0}"
@@ -75,7 +51,7 @@ class CurrencyBase extends Number
7551
* other than the currency code; or decimals (unless the decimals value is set to 0).
7652
* @param bool $stripLeadingRLM remove leading RLM added with
7753
* ICU 72.1+.
78-
* @param string $negative How to display negative numbers.
54+
* @param CurrencyNegative $negative How to display negative numbers.
7955
* Always use parentheses for Accounting.
8056
* 4 options for Currency.
8157
*
@@ -89,7 +65,7 @@ public function __construct(
8965
bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING,
9066
?string $locale = null,
9167
bool $stripLeadingRLM = self::DEFAULT_STRIP_LEADING_RLM,
92-
string $negative = self::NEGATIVE_MINUS
68+
CurrencyNegative $negative = CurrencyNegative::minus
9369
) {
9470
$this->setCurrencyCode($currencyCode);
9571
$this->setThousandsSeparator($thousandsSeparator);
@@ -121,7 +97,7 @@ public function setStripLeadingRLM(bool $stripLeadingRLM): void
12197
$this->stripLeadingRLM = $stripLeadingRLM;
12298
}
12399

124-
public function setNegative(string $negative): void
100+
public function setNegative(CurrencyNegative $negative): void
125101
{
126102
$this->negative = $negative;
127103
}
@@ -184,8 +160,8 @@ public function format(): string
184160

185161
// format if negative
186162
$format .= ';_(';
187-
$format .= self::NEGATIVE_COLOR[$negative] ?? '';
188-
$negativeStart = self::NEGATIVE_START[$negative] ?? '';
163+
$format .= $negative->color();
164+
$negativeStart = $negative->start();
189165
if ($this->currencySymbolPosition === self::LEADING_SYMBOL) {
190166
if ($negativeStart === '-' && !$symbolWithSpacing) {
191167
$format .= $negativeStart;
@@ -201,13 +177,13 @@ public function format(): string
201177
$format .= $negativeStart;
202178
}
203179
} else {
204-
$format .= self::NEGATIVE_START[$negative] ?? '';
180+
$format .= $negative->start();
205181
}
206182
$format .= $this->thousandsSeparator ? '#,##0' : '0';
207183
if ($this->decimals > 0) {
208184
$format .= '.' . str_repeat('0', $this->decimals);
209185
}
210-
$format .= self::NEGATIVE_END[$negative] ?? '';
186+
$format .= $negative->end();
211187
if ($this->currencySymbolPosition === self::TRAILING_SYMBOL) {
212188
if ($symbolWithSpacing) {
213189
// Do nothing - I can't figure out how to get

0 commit comments

Comments
 (0)