Skip to content

Commit

Permalink
Calculation :: Added switch function (PHPOffice#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
yigitcukuren authored and guillaume-ro-fr committed Jun 12, 2019
1 parent 8a40a05 commit 1bbfe96
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
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!"
],
];

0 comments on commit 1bbfe96

Please sign in to comment.