|
5 | 5 | use PhpOffice\PhpSpreadsheet\Settings;
|
6 | 6 | use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
7 | 7 | use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
|
| 8 | +use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\CurrencyNegative; |
| 9 | +use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter; |
8 | 10 |
|
9 | 11 | require __DIR__ . '/../Header.php';
|
10 | 12 |
|
|
15 | 17 | return;
|
16 | 18 | }
|
17 | 19 |
|
| 20 | +$negatives = [ |
| 21 | + CurrencyNegative::minus, |
| 22 | + CurrencyNegative::redMinus, |
| 23 | + CurrencyNegative::parentheses, |
| 24 | + CurrencyNegative::redParentheses, |
| 25 | +]; |
| 26 | +$negativesString = [ |
| 27 | + 'CurrencyNegative::minus', |
| 28 | + 'CurrencyNegative::redMinus', |
| 29 | + 'CurrencyNegative::parentheses', |
| 30 | + 'CurrencyNegative::redParentheses', |
| 31 | +]; |
| 32 | + |
18 | 33 | $currencies = [
|
19 | 34 | '$' => 'US Dollars ($)',
|
20 | 35 | '€' => 'Euro (€)',
|
|
65 | 80 | </div>
|
66 | 81 | </div>
|
67 | 82 | <div class="mb-3 row">
|
68 |
| - <label for="spacing" class="col-sm-2 col-form-label">Currency Spacing</label> |
| 83 | + <label for="negative" class="col-sm-2 col-form-label">Negative Numbers</label> |
69 | 84 | <div class="col-sm-10">
|
70 |
| - <input name="spacing" type="radio" value="1" <?php echo (isset($_POST['spacing']) && $_POST['spacing'] === '1') ? 'checked' : ''; ?>>Yes |
71 |
| - <input name="spacing" type="radio" value="0" <?php echo ((isset($_POST['spacing']) === false) || (isset($_POST['spacing']) && $_POST['spacing'] === '0')) ? 'checked' : ''; ?>>No |
| 85 | + <input name="negative" type="radio" value="0" <?php echo (!isset($_POST['negative']) || $_POST['negative'] === '0') ? 'checked' : ''; ?>>Minus Sign |
| 86 | + <input name="negative" type="radio" value="1" <?php echo (isset($_POST['negative']) && $_POST['negative'] === '1') ? 'checked' : ''; ?>>Red Minus Sign |
| 87 | + <input name="negative" type="radio" value="2" <?php echo (isset($_POST['negative']) && $_POST['negative'] === '2') ? 'checked' : ''; ?>>Parentheses |
| 88 | + <input name="negative" type="radio" value="3" <?php echo (isset($_POST['negative']) && $_POST['negative'] === '3') ? 'checked' : ''; ?>>Red Parentheses |
72 | 89 | </div>
|
73 | 90 | </div>
|
74 | 91 | <div class="mb-3 row">
|
|
85 | 102 | $helper->log('The Sample Number Value must be numeric');
|
86 | 103 | } elseif (!is_numeric($_POST['decimals']) || str_contains((string) $_POST['decimals'], '.') || (int) $_POST['decimals'] < 0) {
|
87 | 104 | $helper->log('The Decimal Places value must be positive integer');
|
| 105 | + } elseif (!in_array($_POST['currency'], array_keys($currencies), true)) { |
| 106 | + $helper->log('Unrecognized currency symbol'); |
88 | 107 | } else {
|
89 | 108 | try {
|
90 |
| - $wizard = new Wizard\Currency($_POST['currency'], (int) $_POST['decimals'], isset($_POST['thousands']), (bool) $_POST['position'], (bool) $_POST['spacing']); |
| 109 | + $negative = $negatives[$_POST['negative']] ?? CurrencyNegative::minus; |
| 110 | + $wizard = new Wizard\Currency($_POST['currency'], (int) $_POST['decimals'], isset($_POST['thousands']), (bool) $_POST['position']); |
| 111 | + $wizard->setNegative($negative); |
91 | 112 | $mask = $wizard->format();
|
92 |
| - $example = (string) NumberFormat::toFormattedString((float) $_POST['number'], $mask); |
| 113 | + $example = (string) NumberFormat::toFormattedString((float) $_POST['number'], $mask, [HtmlWriter::class, 'formatColorStatic']); |
93 | 114 | $helper->log('<hr /><b>Code:</b><br />');
|
94 | 115 | $helper->log('use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;');
|
| 116 | + $helper->log('use PhpOffice\PhpSpreadsheet\Style\NumberFormat\CurrencyNegative;'); |
95 | 117 | $helper->log(
|
96 |
| - "\$mask = Wizard\\Currency('{$_POST['currency']}', {$_POST['decimals']}, Wizard\\Number::" |
| 118 | + "\$wizard = new Wizard\\Currency('{$_POST['currency']}', {$_POST['decimals']}, Wizard\\Number::" |
97 | 119 | . (isset($_POST['thousands']) ? 'WITH_THOUSANDS_SEPARATOR' : 'WITHOUT_THOUSANDS_SEPARATOR')
|
98 | 120 | . ', Wizard\Currency::' . (((bool) $_POST['position']) ? 'LEADING_SYMBOL' : 'TRAILING_SYMBOL')
|
99 |
| - . ', Wizard\Currency::' . (((bool) $_POST['spacing']) ? 'SYMBOL_WITH_SPACING' : 'SYMBOL_WITHOUT_SPACING') |
100 |
| - . ');<br />' |
| 121 | + . ');' |
101 | 122 | );
|
102 |
| - $helper->log('echo (string) $mask;'); |
| 123 | + $helper->log('$wizard->setNegative(' . $negativesString[$_POST['negative']] . ');'); |
| 124 | + $helper->log('$mask = $wizard->format();'); |
| 125 | + $helper->log('<br />echo (string) $mask;'); |
103 | 126 | $helper->log('<hr /><b>Mask:</b><br />');
|
104 | 127 | $helper->log($mask . '<br />');
|
105 | 128 | $helper->log('<br /><b>Example:</b><br />');
|
|
0 commit comments