| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Engine\ArrayArgumentHelper; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Engine\ArrayArgumentProcessor; |
| 7 |
|
| 8 |
trait ArrayEnabled |
| 9 |
{ |
| 10 |
private static bool $initializationNeeded = true; |
| 11 |
|
| 12 |
private static ArrayArgumentHelper $arrayArgumentHelper; |
| 13 |
|
| 14 |
/** |
| 15 |
* @param mixed[] $arguments |
| 16 |
*/ |
| 17 |
private static function initialiseHelper(array $arguments): void |
| 18 |
{ |
| 19 |
if (self::$initializationNeeded === true) { |
| 20 |
self::$arrayArgumentHelper = new ArrayArgumentHelper(); |
| 21 |
self::$initializationNeeded = false; |
| 22 |
} |
| 23 |
self::$arrayArgumentHelper->initialise($arguments); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Handles array argument processing when the function accepts a single argument that can be an array argument. |
| 28 |
* Example use for: |
| 29 |
* DAYOFMONTH() or FACT(). |
| 30 |
* |
| 31 |
* @param mixed[] $values |
| 32 |
* |
| 33 |
* @return mixed[] |
| 34 |
*/ |
| 35 |
protected static function evaluateSingleArgumentArray(callable $method, array $values): array |
| 36 |
{ |
| 37 |
$result = []; |
| 38 |
foreach ($values as $value) { |
| 39 |
$result[] = $method($value); |
| 40 |
} |
| 41 |
|
| 42 |
return $result; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Handles array argument processing when the function accepts multiple arguments, |
| 47 |
* and any of them can be an array argument. |
| 48 |
* Example use for: |
| 49 |
* ROUND() or DATE(). |
| 50 |
* |
| 51 |
* @return mixed[] |
| 52 |
* @param mixed ...$arguments |
| 53 |
*/ |
| 54 |
protected static function evaluateArrayArguments(callable $method, ...$arguments): array |
| 55 |
{ |
| 56 |
self::initialiseHelper($arguments); |
| 57 |
$arguments = self::$arrayArgumentHelper->arguments(); |
| 58 |
|
| 59 |
return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Handles array argument processing when the function accepts multiple arguments, |
| 64 |
* but only the first few (up to limit) can be an array arguments. |
| 65 |
* Example use for: |
| 66 |
* NETWORKDAYS() or CONCATENATE(), where the last argument is a matrix (or a series of values) that need |
| 67 |
* to be treated as a such rather than as an array arguments. |
| 68 |
* |
| 69 |
* @return mixed[] |
| 70 |
* @param mixed ...$arguments |
| 71 |
*/ |
| 72 |
protected static function evaluateArrayArgumentsSubset(callable $method, int $limit, ...$arguments): array |
| 73 |
{ |
| 74 |
self::initialiseHelper(array_slice($arguments, 0, $limit)); |
| 75 |
$trailingArguments = array_slice($arguments, $limit); |
| 76 |
$arguments = self::$arrayArgumentHelper->arguments(); |
| 77 |
$arguments = array_merge($arguments, $trailingArguments); |
| 78 |
|
| 79 |
return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param mixed $value |
| 84 |
*/ |
| 85 |
private static function testFalse($value): bool |
| 86 |
{ |
| 87 |
return $value === false; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Handles array argument processing when the function accepts multiple arguments, |
| 92 |
* but only the last few (from start) can be an array arguments. |
| 93 |
* Example use for: |
| 94 |
* Z.TEST() or INDEX(), where the first argument 1 is a matrix that needs to be treated as a dataset |
| 95 |
* rather than as an array argument. |
| 96 |
* |
| 97 |
* @return mixed[] |
| 98 |
* @param mixed ...$arguments |
| 99 |
*/ |
| 100 |
protected static function evaluateArrayArgumentsSubsetFrom(callable $method, int $start, ...$arguments): array |
| 101 |
{ |
| 102 |
$arrayArgumentsSubset = array_combine( |
| 103 |
range($start, count($arguments) - $start), |
| 104 |
array_slice($arguments, $start) |
| 105 |
); |
| 106 |
if (self::testFalse($arrayArgumentsSubset)) { |
| 107 |
return ['#VALUE!']; |
| 108 |
} |
| 109 |
|
| 110 |
self::initialiseHelper($arrayArgumentsSubset); |
| 111 |
$leadingArguments = array_slice($arguments, 0, $start); |
| 112 |
$arguments = self::$arrayArgumentHelper->arguments(); |
| 113 |
$arguments = array_merge($leadingArguments, $arguments); |
| 114 |
|
| 115 |
return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Handles array argument processing when the function accepts multiple arguments, |
| 120 |
* and any of them can be an array argument except for the one specified by ignore. |
| 121 |
* Example use for: |
| 122 |
* HLOOKUP() and VLOOKUP(), where argument 1 is a matrix that needs to be treated as a database |
| 123 |
* rather than as an array argument. |
| 124 |
* |
| 125 |
* @return mixed[] |
| 126 |
* @param mixed ...$arguments |
| 127 |
*/ |
| 128 |
protected static function evaluateArrayArgumentsIgnore(callable $method, int $ignore, ...$arguments): array |
| 129 |
{ |
| 130 |
$leadingArguments = array_slice($arguments, 0, $ignore); |
| 131 |
$ignoreArgument = array_slice($arguments, $ignore, 1); |
| 132 |
$trailingArguments = array_slice($arguments, $ignore + 1); |
| 133 |
|
| 134 |
self::initialiseHelper(array_merge($leadingArguments, [[null]], $trailingArguments)); |
| 135 |
$arguments = self::$arrayArgumentHelper->arguments(); |
| 136 |
|
| 137 |
array_splice($arguments, $ignore, 1, $ignoreArgument); |
| 138 |
|
| 139 |
return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments); |
| 140 |
} |
| 141 |
} |
| 142 |
|