Skip to content

Commit

Permalink
The WORKDAY() function accepts 2 "static" arguments that could be p…
Browse files Browse the repository at this point in the history
…assed 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()`
  • Loading branch information
MarkBaker committed Feb 10, 2022
1 parent ca15e97 commit 5052f40
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/PhpSpreadsheet/Calculation/ArrayEnabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ protected static function evaluateArrayArguments(callable $method, ...$arguments
self::initialiseHelper($arguments);
$arguments = self::$arrayArgumentHelper->arguments();

return self::processArguments($method, ...$arguments);
}

/**
* @param mixed ...$arguments
*/
protected static function evaluateArrayArgumentsSubset(callable $method, int $limit, ...$arguments): array
{
self::initialiseHelper(array_slice($arguments, 0, $limit));
$trailingArguments = array_slice($arguments, $limit);
$arguments = self::$arrayArgumentHelper->arguments();
$arguments = array_merge($arguments, $trailingArguments);

return self::processArguments($method, ...$arguments);
}

/**
* @param mixed ...$arguments
*/
private static function processArguments(callable $method, ...$arguments): array
{
if (self::$arrayArgumentHelper->hasArrayArgument() === false) {
return [$method(...$arguments)];
}
Expand Down
2 changes: 2 additions & 0 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/WorkDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ private static function decrementing(float $startDate, int $endDays, array $holi
--$endDate;
// Adjust the calculated end date if it falls over a weekend
$endDow = self::getWeekDay($endDate, 3);
/** int $endDoW */
if ($endDow >= 5) {
$endDate += 4 - $endDow;
}
Expand Down Expand Up @@ -188,6 +189,7 @@ private static function decrementingArray(float $startDate, float $endDate, arra
}
// Adjust the calculated end date if it falls over a weekend
$endDoW = self::getWeekDay($endDate, 3);
/** int $endDoW */
if ($endDoW >= 5) {
$endDate += -$endDoW + 4;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;

use PhpOffice\PhpSpreadsheet\Calculation\Calculation;

class WorkDayTest extends AllSetupTeardown
{
/**
Expand Down Expand Up @@ -49,4 +51,33 @@ public function providerWORKDAY(): array
{
return require 'tests/data/Calculation/DateTime/WORKDAY.php';
}

/**
* @dataProvider providerWorkDayArray
*/
public function testWorkDayArray(array $expectedResult, string $startDate, string $endDays, ?string $holidays): void
{
$calculation = Calculation::getInstance();

if ($holidays === null) {
$formula = "=WORKDAY({$startDate}, {$endDays})";
} else {
$formula = "=WORKDAY({$startDate}, {$endDays}, {$holidays})";
}
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}

public function providerWorkDayArray(): array
{
return [
'row vector #1' => [[[44595, 44596, 44599]], '{"2022-02-01", "2022-02-02", "2022-02-03"}', '2', null],
'column vector #1' => [[[44595], [44596], [44599]], '{"2022-02-01"; "2022-02-02"; "2022-02-03"}', '2', null],
'matrix #1' => [[[44595, 44596], [44599, 44600]], '{"2022-02-01", "2022-02-02"; "2022-02-03", "2022-02-04"}', '2', null],
'row vector #2' => [[[44595, 44596]], '"2022-02-01"', '{2, 3}', null],
'column vector #2' => [[[44595], [44596]], '"2022-02-01"', '{2; 3}', null],
'row vector with Holiday' => [[[44596, 44599]], '"2022-02-01"', '{2, 3}', '{"2022-02-02"}'],
'row vector with Holidays' => [[[44599, 44600]], '"2022-02-01"', '{2, 3}', '{"2022-02-02", "2022-02-03"}'],
];
}
}

0 comments on commit 5052f40

Please sign in to comment.