Skip to content

Commit 5052f40

Browse files
author
MarkBaker
committed
The WORKDAY() function accepts 2 "static" arguments that could be passed as arrays; but also accepts a set of trailing date arguments that are accepted as an array by the splat operator. Only the first two arguments should be tested for returning array values; but the logic still needs to work with the full argument set.
Provide a separate "subset" method in the `ArrayEnabled` Trait, that allows a subset of arguments to be tested for array returns. Set up basic tests for `WORKDAY()`
1 parent ca15e97 commit 5052f40

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/PhpSpreadsheet/Calculation/ArrayEnabled.php

+21
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ protected static function evaluateArrayArguments(callable $method, ...$arguments
3737
self::initialiseHelper($arguments);
3838
$arguments = self::$arrayArgumentHelper->arguments();
3939

40+
return self::processArguments($method, ...$arguments);
41+
}
42+
43+
/**
44+
* @param mixed ...$arguments
45+
*/
46+
protected static function evaluateArrayArgumentsSubset(callable $method, int $limit, ...$arguments): array
47+
{
48+
self::initialiseHelper(array_slice($arguments, 0, $limit));
49+
$trailingArguments = array_slice($arguments, $limit);
50+
$arguments = self::$arrayArgumentHelper->arguments();
51+
$arguments = array_merge($arguments, $trailingArguments);
52+
53+
return self::processArguments($method, ...$arguments);
54+
}
55+
56+
/**
57+
* @param mixed ...$arguments
58+
*/
59+
private static function processArguments(callable $method, ...$arguments): array
60+
{
4061
if (self::$arrayArgumentHelper->hasArrayArgument() === false) {
4162
return [$method(...$arguments)];
4263
}

src/PhpSpreadsheet/Calculation/DateTimeExcel/WorkDay.php

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ private static function decrementing(float $startDate, int $endDays, array $holi
156156
--$endDate;
157157
// Adjust the calculated end date if it falls over a weekend
158158
$endDow = self::getWeekDay($endDate, 3);
159+
/** int $endDoW */
159160
if ($endDow >= 5) {
160161
$endDate += 4 - $endDow;
161162
}
@@ -188,6 +189,7 @@ private static function decrementingArray(float $startDate, float $endDate, arra
188189
}
189190
// Adjust the calculated end date if it falls over a weekend
190191
$endDoW = self::getWeekDay($endDate, 3);
192+
/** int $endDoW */
191193
if ($endDoW >= 5) {
192194
$endDate += -$endDoW + 4;
193195
}

tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/WorkDayTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6+
57
class WorkDayTest extends AllSetupTeardown
68
{
79
/**
@@ -49,4 +51,33 @@ public function providerWORKDAY(): array
4951
{
5052
return require 'tests/data/Calculation/DateTime/WORKDAY.php';
5153
}
54+
55+
/**
56+
* @dataProvider providerWorkDayArray
57+
*/
58+
public function testWorkDayArray(array $expectedResult, string $startDate, string $endDays, ?string $holidays): void
59+
{
60+
$calculation = Calculation::getInstance();
61+
62+
if ($holidays === null) {
63+
$formula = "=WORKDAY({$startDate}, {$endDays})";
64+
} else {
65+
$formula = "=WORKDAY({$startDate}, {$endDays}, {$holidays})";
66+
}
67+
$result = $calculation->_calculateFormulaValue($formula);
68+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
69+
}
70+
71+
public function providerWorkDayArray(): array
72+
{
73+
return [
74+
'row vector #1' => [[[44595, 44596, 44599]], '{"2022-02-01", "2022-02-02", "2022-02-03"}', '2', null],
75+
'column vector #1' => [[[44595], [44596], [44599]], '{"2022-02-01"; "2022-02-02"; "2022-02-03"}', '2', null],
76+
'matrix #1' => [[[44595, 44596], [44599, 44600]], '{"2022-02-01", "2022-02-02"; "2022-02-03", "2022-02-04"}', '2', null],
77+
'row vector #2' => [[[44595, 44596]], '"2022-02-01"', '{2, 3}', null],
78+
'column vector #2' => [[[44595], [44596]], '"2022-02-01"', '{2; 3}', null],
79+
'row vector with Holiday' => [[[44596, 44599]], '"2022-02-01"', '{2, 3}', '{"2022-02-02"}'],
80+
'row vector with Holidays' => [[[44599, 44600]], '"2022-02-01"', '{2, 3}', '{"2022-02-02", "2022-02-03"}'],
81+
];
82+
}
5283
}

0 commit comments

Comments
 (0)