| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
|
8 |
|
|
|
9 |
class Selection
|
|
|
10 |
{
|
|
|
11 |
use ArrayEnabled;
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* CHOOSE.
|
|
|
15 |
*
|
|
|
16 |
* Uses lookup_value to return a value from the list of value arguments.
|
|
|
17 |
* Use CHOOSE to select one of up to 254 values based on the lookup_value.
|
|
|
18 |
*
|
|
|
19 |
* Excel Function:
|
|
|
20 |
* =CHOOSE(index_num, value1, [value2], ...)
|
|
|
21 |
*
|
|
|
22 |
* @param mixed $chosenEntry The entry to select from the list (indexed from 1)
|
|
|
23 |
* @param mixed ...$chooseArgs Data values
|
|
|
24 |
*
|
|
|
25 |
* @return mixed The selected value
|
|
|
26 |
*/
|
|
|
27 |
public static function choose(mixed $chosenEntry, mixed ...$chooseArgs): mixed
|
|
|
28 |
{
|
|
|
29 |
if (is_array($chosenEntry)) {
|
|
|
30 |
return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $chosenEntry, ...$chooseArgs);
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
$entryCount = count($chooseArgs) - 1;
|
|
|
34 |
|
|
|
35 |
if (is_numeric($chosenEntry)) {
|
|
|
36 |
--$chosenEntry;
|
|
|
37 |
} else {
|
|
|
38 |
return ExcelError::VALUE();
|
|
|
39 |
}
|
|
|
40 |
$chosenEntry = floor($chosenEntry);
|
|
|
41 |
if (($chosenEntry < 0) || ($chosenEntry > $entryCount)) {
|
|
|
42 |
return ExcelError::VALUE();
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
if (is_array($chooseArgs[$chosenEntry])) {
|
|
|
46 |
return Functions::flattenArray($chooseArgs[$chosenEntry]);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
return $chooseArgs[$chosenEntry];
|
|
|
50 |
}
|
|
|
51 |
}
|