Calculation :: Added switch function (#983)

This commit is contained in:
Yigit Cukuren 2019-05-30 10:14:58 +03:00 committed by Mark Baker
parent 9a208b31d8
commit cce6e5976f
5 changed files with 122 additions and 0 deletions

View File

@ -1945,6 +1945,11 @@ class Calculation
'functionCall' => [MathTrig::class, 'SUMXMY2'], 'functionCall' => [MathTrig::class, 'SUMXMY2'],
'argumentCount' => '2', 'argumentCount' => '2',
], ],
'SWITCH' => [
'category' => Category::CATEGORY_LOGICAL,
'functionCall' => [Logical::class, 'statementSwitch'],
'argumentCount' => '3+',
],
'SYD' => [ 'SYD' => [
'category' => Category::CATEGORY_FINANCIAL, 'category' => Category::CATEGORY_FINANCIAL,
'functionCall' => [Financial::class, 'SYD'], 'functionCall' => [Financial::class, 'SYD'],

View File

@ -273,6 +273,60 @@ class Logical
return ($condition) ? $returnIfTrue : $returnIfFalse; 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. * IFERROR.
* *

View File

@ -337,6 +337,7 @@ SUMSQ
SUMX2MY2 SUMX2MY2
SUMX2PY2 SUMX2PY2
SUMXMY2 SUMXMY2
SWITCH
SYD SYD
T T
TAN TAN

View File

@ -120,4 +120,20 @@ class LogicalTest extends TestCase
{ {
return require 'data/Calculation/Logical/IFERROR.php'; 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';
}
} }

View File

@ -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!"
],
];