Skip to content

Commit 6f60191

Browse files
committed
Add defaults for Date and Time NumberFormat Wizards
1 parent 58ac4ca commit 6f60191

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ class Date extends DateTimeWizard
7373
public const SEPARATOR_SPACE_NONBREAKING = "\u{a0}";
7474
public const SEPARATOR_SPACE = ' ';
7575

76+
protected const DATE_DEFAULT = [
77+
self::YEAR_FULL,
78+
self::MONTH_NUMBER_LONG,
79+
self::DAY_NUMBER_LONG,
80+
];
81+
7682
/**
7783
* @var string[]
7884
*/
@@ -89,8 +95,11 @@ class Date extends DateTimeWizard
8995
* if you wish to use different separators, then they should be passed as an array.
9096
* If you want to use only a single format block, then pass a null as the separator argument
9197
*/
92-
public function __construct($separators, string ...$formatBlocks)
98+
public function __construct($separators = self::SEPARATOR_DASH, string ...$formatBlocks)
9399
{
100+
$separators ??= self::SEPARATOR_DASH;
101+
$formatBlocks = (count($formatBlocks) === 0) ? self::DATE_DEFAULT : $formatBlocks;
102+
94103
$this->separators = $this->padSeparatorArray(
95104
is_array($separators) ? $separators : [$separators],
96105
count($formatBlocks) - 1

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ class Time extends DateTimeWizard
5050
public const SEPARATOR_SPACE_NONBREAKING = "\u{a0}";
5151
public const SEPARATOR_SPACE = ' ';
5252

53+
protected const TIME_DEFAULT = [
54+
self::HOURS_LONG,
55+
self::MINUTES_LONG,
56+
self::SECONDS_LONG,
57+
];
58+
5359
/**
5460
* @var string[]
5561
*/
@@ -66,8 +72,11 @@ class Time extends DateTimeWizard
6672
* if you wish to use different separators, then they should be passed as an array.
6773
* If you want to use only a single format block, then pass a null as the separator argument
6874
*/
69-
public function __construct($separators, string ...$formatBlocks)
75+
public function __construct($separators = self::SEPARATOR_COLON, string ...$formatBlocks)
7076
{
77+
$separators ??= self::SEPARATOR_COLON;
78+
$formatBlocks = (count($formatBlocks) === 0) ? self::TIME_DEFAULT : $formatBlocks;
79+
7180
$this->separators = $this->padSeparatorArray(
7281
is_array($separators) ? $separators : [$separators],
7382
count($formatBlocks) - 1

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class DateTest extends TestCase
1313
* @param null|string|string[] $separators
1414
* @param string[] $formatBlocks
1515
*/
16-
public function testDate(string $expectedResult, $separators, array $formatBlocks): void
16+
public function testDate(string $expectedResult, $separators = null, array $formatBlocks = []): void
1717
{
1818
$wizard = new Date($separators, ...$formatBlocks);
1919
self::assertSame($expectedResult, (string) $wizard);
@@ -30,6 +30,8 @@ public function providerDate(): array
3030
['dddd dd.mm.yyyy', [Date::SEPARATOR_SPACE, Date::SEPARATOR_DOT], [Date::WEEKDAY_NAME_LONG, Date::DAY_NUMBER_LONG, Date::MONTH_NUMBER_LONG, Date::YEAR_FULL]],
3131
['dd-mm "in the year" yyyy', [Date::SEPARATOR_DASH, Date::SEPARATOR_SPACE], [Date::DAY_NUMBER_LONG, Date::MONTH_NUMBER_LONG, 'in the year', Date::YEAR_FULL]],
3232
["yyyy-mm-dd\u{a0}(ddd)", [Date::SEPARATOR_DASH, Date::SEPARATOR_DASH, Date::SEPARATOR_SPACE_NONBREAKING, null], [Date::YEAR_FULL, Date::MONTH_NUMBER_LONG, Date::DAY_NUMBER_LONG, '(', Date::WEEKDAY_NAME_SHORT, ')']],
33+
['yyyy-mm-dd', null, [Date::YEAR_FULL, Date::MONTH_NUMBER_LONG, Date::DAY_NUMBER_LONG]],
34+
['yyyy-mm-dd'],
3335
];
3436
}
3537
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TimeTest extends TestCase
1313
* @param null|string|string[] $separators
1414
* @param string[] $formatBlocks
1515
*/
16-
public function testTime(string $expectedResult, $separators, array $formatBlocks): void
16+
public function testTime(string $expectedResult, $separators = null, array $formatBlocks = []): void
1717
{
1818
$wizard = new Time($separators, ...$formatBlocks);
1919
self::assertSame($expectedResult, (string) $wizard);
@@ -26,6 +26,8 @@ public function providerTime(): array
2626
['hh:mm', Time::SEPARATOR_COLON, [Time::HOURS_LONG, Time::MINUTES_LONG]],
2727
["hh:mm\u{a0}AM/PM", [Time::SEPARATOR_COLON, Time::SEPARATOR_SPACE_NONBREAKING], [Time::HOURS_LONG, Time::MINUTES_LONG, Time::MORNING_AFTERNOON]],
2828
['h "hours and" m "minutes past midnight"', Time::SEPARATOR_SPACE, [Time::HOURS_SHORT, 'hours and', Time::MINUTES_SHORT, 'minutes past midnight']],
29+
['hh:mm:ss', null, [Time::HOURS_LONG, Time::MINUTES_LONG, Time::SECONDS_LONG]],
30+
['hh:mm:ss'],
2931
];
3032
}
3133
}

0 commit comments

Comments
 (0)