Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculation/Logical :: Added SWITCH function #983

Merged
merged 1 commit into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,11 @@ class Calculation
'functionCall' => [MathTrig::class, 'SUMXMY2'],
'argumentCount' => '2',
],
'SWITCH' => [
'category' => Category::CATEGORY_LOGICAL,
'functionCall' => [Logical::class, 'statementSwitch'],
'argumentCount' => '3+',
],
'SYD' => [
'category' => Category::CATEGORY_FINANCIAL,
'functionCall' => [Financial::class, 'SYD'],
Expand Down
54 changes: 54 additions & 0 deletions src/PhpSpreadsheet/Calculation/Logical.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,60 @@ public static function statementIf($condition = true, $returnIfTrue = 0, $return
return ($condition) ? $returnIfTrue : $returnIfFalse;
}

/**
* STATEMENT_SWITCH.
*
* Returns corresponding with first match (any data type such as a string, numeric, date, etc).
*
* Excel Function:
* =SWITCH (expression, value1, result1, value2, result2, ... value_n, result_n [, default])
*
* Expression
* The expression to compare to a list of values.
* value1, value2, ... value_n
* A list of values that are compared to expression. The SWITCH function is looking for the first value that matches the expression.
* result1, result2, ... result_n
* A list of results. The SWITCH function returns the corresponding result when a value matches expression.
* default
* Optional. It is the default to return if expression does not match any of the values (value1, value2, ... value_n).
*
* @category Logical Functions
*
* @param mixed $arguments Statement arguments
*
* @return mixed The value of matched expression
*/
public static function statementSwitch(...$arguments)
{
$result = Functions::VALUE();

if (count($arguments) > 0) {
$targetValue = Functions::flattenSingleValue($arguments[0]);
$argc = count($arguments) - 1;
$switchCount = floor($argc / 2);
$switchSatisfied = false;
$hasDefaultClause = $argc % 2 !== 0;
$defaultClause = $argc % 2 === 0 ? null : $arguments[count($arguments) - 1];

if ($switchCount) {
for ($index = 0; $index < $switchCount; ++$index) {
if ($targetValue == $arguments[$index * 2 + 1]) {
$result = $arguments[$index * 2 + 2];
$switchSatisfied = true;

break;
}
}
}

if (!$switchSatisfied) {
$result = $hasDefaultClause ? $defaultClause : Functions::NA();
}
}

return $result;
}

/**
* IFERROR.
*
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Calculation/functionlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ SUMSQ
SUMX2MY2
SUMX2PY2
SUMXMY2
SWITCH
SYD
T
TAN
Expand Down
16 changes: 16 additions & 0 deletions tests/PhpSpreadsheetTests/Calculation/LogicalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,20 @@ public function providerIFERROR()
{
return require 'data/Calculation/Logical/IFERROR.php';
}

/**
* @dataProvider providerSwitch
*
* @param mixed $expectedResult
*/
public function testSWITCH($expectedResult, ...$args)
{
$result = Logical::statementSwitch(...$args);
self::assertEquals($expectedResult, $result);
}

public function providerSwitch()
{
return require 'data/Calculation/Logical/SWITCH.php';
}
}
46 changes: 46 additions & 0 deletions tests/data/Calculation/Logical/SWITCH.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

return [
// Must be C
[
"C",
"A",
"A",
"C",
"B",
"D",
"??"
],
// Must be Female
[
"Female",
2,
"1",
"Male",
"2",
"Female"
],
// Must be X using default
[
"X",
"U",
"ABC",
"Y",
"DEF",
"Z",
"X"
],
// Must be N/A default value not defined
[
"#N/A",
"U",
"ABC",
"Y",
"DEF",
"Z"
],
// Must be value - no parameter
[
"#VALUE!"
],
];