Skip to content

Commit 3c64527

Browse files
authored
Merge pull request #4653 from oleibman/extintl
Document NumberFormat Wizard Dependency on Intl Extension
2 parents b982ef9 + 684c677 commit 3c64527

File tree

13 files changed

+96
-28
lines changed

13 files changed

+96
-28
lines changed

.github/workflows/main.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
uses: shivammathur/setup-php@v2
3636
with:
3737
php-version: ${{ matrix.php-version }}
38-
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
38+
extensions: ctype, dom, fileinfo, gd, iconv, intl, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
3939
coverage: none
4040

4141
- name: Get composer cache directory
@@ -82,7 +82,7 @@ jobs:
8282
uses: shivammathur/setup-php@v2
8383
with:
8484
php-version: 8.4
85-
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
85+
extensions: ctype, dom, fileinfo, gd, iconv, intl, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
8686
coverage: none
8787

8888
# This is non-ideal because it only checks for the last commit of the PR, not all of them, but better than nothing
@@ -99,7 +99,7 @@ jobs:
9999
uses: shivammathur/setup-php@v2
100100
with:
101101
php-version: 8.4
102-
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
102+
extensions: ctype, dom, fileinfo, gd, iconv, intl, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
103103
coverage: none
104104
tools: cs2pr
105105

@@ -130,7 +130,7 @@ jobs:
130130
uses: shivammathur/setup-php@v2
131131
with:
132132
php-version: 8.4
133-
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
133+
extensions: ctype, dom, fileinfo, gd, iconv, intl, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
134134
coverage: none
135135
tools: cs2pr
136136

@@ -161,7 +161,7 @@ jobs:
161161
uses: shivammathur/setup-php@v2
162162
with:
163163
php-version: 8.4
164-
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
164+
extensions: ctype, dom, fileinfo, gd, iconv, intl, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
165165
coverage: none
166166
tools: cs2pr
167167

@@ -192,7 +192,7 @@ jobs:
192192
uses: shivammathur/setup-php@v2
193193
with:
194194
php-version: 8.4
195-
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
195+
extensions: ctype, dom, fileinfo, gd, iconv, intl, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
196196
coverage: none
197197
tools: cs2pr
198198

@@ -231,7 +231,7 @@ jobs:
231231
uses: shivammathur/setup-php@v2
232232
with:
233233
php-version: 8.4
234-
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
234+
extensions: ctype, dom, fileinfo, gd, iconv, intl, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
235235
coverage: pcov
236236

237237
- name: Get composer cache directory

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
"tecnickcom/tcpdf": "^6.5"
104104
},
105105
"suggest": {
106-
"ext-intl": "PHP Internationalization Functions",
106+
"ext-intl": "PHP Internationalization Functions, regquired for NumberFormat Wizard",
107107
"mpdf/mpdf": "Option for rendering PDF with PDF Writer",
108108
"dompdf/dompdf": "Option for rendering PDF with PDF Writer",
109109
"tecnickcom/tcpdf": "Option for rendering PDF with PDF Writer",

docs/topics/Behind the Mask.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ But if we use the "pull down" for that block, we access the "Number" tab of "For
194194
This gives us access to a number of "Wizards" for different "Categories" of masking, as well as "Custom", which allows us to build our own masks.
195195

196196
Since version 1.28.0, PhpSpreadsheet has also provided a set of "Wizards", allowing for the easier creation of Mask values for most Categories.
197+
In many cases, you will need to enable PHP's `Intl` extension in order to use the Wizards.
197198

198199
## Mask Categories
199200

@@ -232,7 +233,7 @@ var_dump($worksheet->getCell('C20')
232233
var_dump($worksheet->getCell('C20')->getFormattedValue()); // "-12,345.679"
233234
```
234235

235-
PhpSpreadsheet's "Wizard" doesn't yet offer options for displaying negative values; they will simply be masked so that they always display the sign.
236+
PhpSpreadsheet's Number Wizard doesn't yet offer options for displaying negative values; they will simply be masked so that they always display the sign.
236237
But alternative masking for negative values is an option that may be added in the future.
237238

238239
### Currency
@@ -245,6 +246,7 @@ The "Symbol" dropdown provides a lot of locale-specific variants of the same cur
245246

246247
The PhpSpreadsheet Currency "Wizard" allows you to specify the currency code, number of decimals, and the use of a thousands separator.
247248
In addition, optionally, you can also specify whether the currency symbol should be leading or trailing, and whether it should be separated from the value or not.
249+
Finally, you have a choice of 4 ways of specifying negative values - minus sign, minus sign with the field in red, parentheses, and parentheses with the field in red.
248250

249251
```php
250252
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\Currency;
@@ -302,9 +304,6 @@ var_dump($worksheet->getCell('C21')->getFormattedValue()); // -12,345.68 €
302304
If we use the locale in the "Wizard", then a typical mask might look like '#,##0.00 [$€-de-DE]', with the currency wrapped in braces, a `$` to indicate that this is a localised value, and the locale included.
303305
> Note: The Wizard does not accept LCIDs.
304306
305-
PhpSpreadsheet's "Wizard" doesn't yet offer options for displaying negative values; they will simply be masked so that they always display the sign.
306-
But alternative masking for negative values is an option that may be added in the future.
307-
308307
### Accounting
309308

310309
Excel's Accounting "Wizard" is like the Currency "Wizard", but without the options for presenting negative values.
@@ -340,7 +339,6 @@ var_dump($worksheet->getCell('C20')->getFormattedValue()); // -12,345.68 €
340339
```
341340
A typical Accounting mask might look something like '_-#,##0.00 €*_-', with the currency symbol as a literal; and with placement indicators like `_-`, that ensure the alignment of the currency symbols and decimal points of numbers in a column.
342341

343-
At the moment, none of the PhpSpreadsheet Wizards provide different masks for zero and negative values; unless you have the PHP `Intl` extension enabled, and can use the locale to generate the Mask.
344342
As with using a locale with the Currency "Wizard", when you use a locale with the Accounting "Wizard" the locale value must be valid, and any additional options will be ignored.
345343
```php
346344
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\Accounting;
@@ -391,8 +389,6 @@ I've written in detail about Date Format Masks elsewhere in "The Dating Game"; b
391389
| yyyy | Year (4 digits) | 2023 |
392390

393391

394-
There is currently no PhpSpreadsheet "Wizard" for Date Masks; but this will be introduced in the 1.29.0 release.
395-
396392
### Time
397393

398394
As with Dates, when you use the Excel Time "Wizard", you can select a locale and you'll then be presented with a number of time format options that are appropriate for that locale.
@@ -411,7 +407,7 @@ I've written in detail about Time Format Masks elsewhere in "The Dating Game"; b
411407
| ss | Seconds with a leading zero | 00-59 |
412408
| AM/PM | Periods of the day <br/>(if omitted, 24-hour time format is used) | AM or PM |
413409

414-
Excel also supports Masks for Time Durations, although there is no "Wizard" for this; but the following Mask codes can be used to display Durations.
410+
Excel also supports Masks for Time Durations (note that spreadsheets using the 1904 base date can display negative durations, but those using the 1900 base date cannot). There is no "Wizard" for this; but the following Mask codes can be used to display Durations.
415411

416412
| Code | Description | Displays as |
417413
|---------|----------------------------------------------------------------|-------------|
@@ -422,8 +418,6 @@ Excel also supports Masks for Time Durations, although there is no "Wizard" for
422418
| [s] | Elapsed time in seconds | |
423419
| [ss] | Elapsed time in seconds<br>with a leading zero if less than 10 | |
424420

425-
There is currently no PhpSpreadsheet "Wizard" for Time Masks, or for Durations; but these will be introduced in the 1.29.0 release.
426-
427421
### Percentage
428422

429423
This is among the simplest of the Excel "Wizards", only allowing you to specify the number of decimals to be displayed.

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ final class Locale
1919

2020
public function __construct(?string $locale, int $style)
2121
{
22-
if (class_exists(NumberFormatter::class) === false) {
23-
throw new Exception();
24-
}
25-
2622
$formatterLocale = str_replace('-', '_', $locale ?? '');
2723
$this->formatter = new NumberFormatter($formatterLocale, $style);
2824
if ($this->formatter->getLocale() !== $formatterLocale) {

tests/PhpSpreadsheetTests/Style/NumberFormat/Wizard/AccountingTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\Currency;
1212
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\CurrencyNegative;
1313
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\Number;
14+
use PHPUnit\Framework\Attributes\DataProvider;
1415
use PHPUnit\Framework\TestCase;
16+
use ReflectionMethod;
1517

1618
class AccountingTest extends TestCase
1719
{
18-
#[\PHPUnit\Framework\Attributes\DataProvider('providerAccounting')]
20+
#[DataProvider('providerAccounting')]
1921
public function testAccounting(
2022
string $expectedResultPositive,
2123
string $expectedResultNegative,
@@ -45,7 +47,7 @@ public static function providerAccounting(): array
4547
];
4648
}
4749

48-
#[\PHPUnit\Framework\Attributes\DataProvider('providerAccountingLocale')]
50+
#[DataProvider('providerAccountingLocale')]
4951
public function testAccountingLocale(
5052
string $expectedResult,
5153
string $currencyCode,
@@ -105,7 +107,7 @@ public function testIcu721(): void
105107
}
106108
}
107109

108-
#[\PHPUnit\Framework\Attributes\DataProvider('providerAccountingLocaleNoDecimals')]
110+
#[DataProvider('providerAccountingLocaleNoDecimals')]
109111
public function testAccountingLocaleNoDecimals(
110112
string $expectedResult,
111113
string $currencyCode,
@@ -170,4 +172,12 @@ public function testAccountingLocaleInvalidCode(): void
170172
$wizard = new Accounting('');
171173
$wizard->setLocale($locale);
172174
}
175+
176+
public function testLocaleNull2(): void
177+
{
178+
$wizard = new Accounting('$', 2);
179+
$reflectionMethod = new ReflectionMethod($wizard, 'formatCurrencyCode');
180+
$result = $reflectionMethod->invokeArgs($wizard, []);
181+
self::assertSame('$*', $result);
182+
}
173183
}

tests/PhpSpreadsheetTests/Style/NumberFormat/Wizard/CurrencyTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\Currency;
1212
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\CurrencyNegative;
1313
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\Number;
14+
use PHPUnit\Framework\Attributes\DataProvider;
1415
use PHPUnit\Framework\TestCase;
16+
use ReflectionMethod;
1517

1618
class CurrencyTest extends TestCase
1719
{
18-
#[\PHPUnit\Framework\Attributes\DataProvider('providerCurrency')]
20+
#[DataProvider('providerCurrency')]
1921
public function testCurrency(
2022
string $expectedResultPositive,
2123
string $expectedResultNegative,
@@ -43,10 +45,25 @@ public static function providerCurrency(): array
4345
[' 1,234.56€ ', ' -1,234.56€ ', ' 0.00€ ', '', 2, Number::WITH_THOUSANDS_SEPARATOR, Currency::TRAILING_SYMBOL, Currency::SYMBOL_WITH_SPACING],
4446
[' 1234.56€ ', ' -1234.56€ ', ' 0.00€ ', '', 2, Number::WITHOUT_THOUSANDS_SEPARATOR, Currency::TRAILING_SYMBOL, Currency::SYMBOL_WITHOUT_SPACING],
4547
[' 1234.56€ ', ' (1234.56)€ ', ' 0.00€ ', '', 2, Number::WITHOUT_THOUSANDS_SEPARATOR, Currency::TRAILING_SYMBOL, Currency::SYMBOL_WITHOUT_SPACING, CurrencyNegative::parentheses],
48+
[' 1234.56 GBP ', ' (1234.56) GBP ', ' 0.00GBP ', 'GBP', 2, Number::WITHOUT_THOUSANDS_SEPARATOR, Currency::TRAILING_SYMBOL, Currency::SYMBOL_WITHOUT_SPACING, CurrencyNegative::parentheses],
4649
];
4750
}
4851

49-
#[\PHPUnit\Framework\Attributes\DataProvider('providerCurrencyLocale')]
52+
public function testSetNegative(): void
53+
{
54+
$expectedResultNegative = ' (1234.56)€ ';
55+
$currencyCode = '';
56+
$decimals = 2;
57+
$thousandsSeparator = Number::WITHOUT_THOUSANDS_SEPARATOR;
58+
$currencySymbolPosition = Currency::TRAILING_SYMBOL;
59+
$currencySymbolSpacing = Currency::SYMBOL_WITHOUT_SPACING;
60+
$negative = CurrencyNegative::parentheses;
61+
$wizard = new Currency($currencyCode, $decimals, $thousandsSeparator, $currencySymbolPosition, $currencySymbolSpacing);
62+
$wizard->setNegative($negative);
63+
self::assertSame($expectedResultNegative, Formatter::toFormattedString(-1234.56, $wizard->format()));
64+
}
65+
66+
#[DataProvider('providerCurrencyLocale')]
5067
public function testCurrencyLocale(
5168
string $expectedResult,
5269
string $currencyCode,
@@ -106,7 +123,7 @@ public function testIcu721(): void
106123
}
107124
}
108125

109-
#[\PHPUnit\Framework\Attributes\DataProvider('providerCurrencyLocaleNoDecimals')]
126+
#[DataProvider('providerCurrencyLocaleNoDecimals')]
110127
public function testCurrencyLocaleNoDecimals(
111128
string $expectedResult,
112129
string $currencyCode,
@@ -171,4 +188,12 @@ public function testCurrencyLocaleInvalidCode(): void
171188
$wizard = new Currency('');
172189
$wizard->setLocale($locale);
173190
}
191+
192+
public function testLocaleNull3(): void
193+
{
194+
$wizard = new Currency('$', 2);
195+
$reflectionMethod = new ReflectionMethod($wizard, 'formatCurrencyCode');
196+
$result = $reflectionMethod->invokeArgs($wizard, []);
197+
self::assertSame('$', $result);
198+
}
174199
}

tests/PhpSpreadsheetTests/Style/NumberFormat/Wizard/DurationTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\Duration;
88
use PHPUnit\Framework\TestCase;
9+
use ReflectionMethod;
910

1011
class DurationTest extends TestCase
1112
{
@@ -33,4 +34,12 @@ public static function providerTime(): array
3334
['[h]:mm:ss'],
3435
];
3536
}
37+
38+
public function testOddCase(): void
39+
{
40+
$wizard = new Duration(null, Duration::HOURS_DURATION, Duration::MINUTES_LONG, Duration::SECONDS_LONG);
41+
$reflectionMethod = new ReflectionMethod($wizard, 'mapFormatBlocks');
42+
$result = $reflectionMethod->invokeArgs($wizard, ['%%%']);
43+
self::assertSame('"%%%"', $result);
44+
}
3645
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Style\NumberFormat\Wizard;
6+
7+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\NumberBase;
8+
9+
class NumberBase2 extends NumberBase
10+
{
11+
protected function getLocaleFormat(): string
12+
{
13+
return 'none';
14+
}
15+
}

tests/PhpSpreadsheetTests/Style/NumberFormat/Wizard/NumberTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,10 @@ public function testNumberLocaleInvalidFormat(): void
6565
$wizard = new Number(2);
6666
$wizard->setLocale($locale);
6767
}
68+
69+
public function testNonOverriddenFormat(): void
70+
{
71+
$wizard = new NumberBase2();
72+
self::assertSame('General', $wizard->format());
73+
}
6874
}

tests/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
setlocale(LC_ALL, 'en_US.utf8');
6+
ini_set('error_reporting', (string) E_ALL);
67

78
function phpunit10ErrorHandler(int $errno, string $errstr, string $filename, int $lineno): bool
89
{

0 commit comments

Comments
 (0)