|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpOffice\PhpSpreadsheet\Calculation; |
| 4 | + |
| 5 | +use PhpOffice\PhpSpreadsheet\Calculation\Engine\ArrayArgumentHelper; |
| 6 | + |
| 7 | +trait ArrayEnabled |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @var ArrayArgumentHelper |
| 11 | + */ |
| 12 | + private static $arrayArgumentHelper; |
| 13 | + |
| 14 | + private static function initialiseHelper(array $arguments): void |
| 15 | + { |
| 16 | + if (self::$arrayArgumentHelper === null) { |
| 17 | + self::$arrayArgumentHelper = new ArrayArgumentHelper(); |
| 18 | + } |
| 19 | + self::$arrayArgumentHelper->initialise($arguments); |
| 20 | + } |
| 21 | + |
| 22 | + protected static function evaluateSingleArgumentArray(callable $method, array $values): array |
| 23 | + { |
| 24 | + $result = []; |
| 25 | + foreach ($values as $value) { |
| 26 | + $result[] = $method($value); |
| 27 | + } |
| 28 | + |
| 29 | + return $result; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @param mixed ...$arguments |
| 34 | + */ |
| 35 | + protected static function evaluateArrayArguments(callable $method, ...$arguments): array |
| 36 | + { |
| 37 | + self::initialiseHelper($arguments); |
| 38 | + $arguments = self::$arrayArgumentHelper->arguments(); |
| 39 | + |
| 40 | + if (self::$arrayArgumentHelper->hasArrayArgument() === false) { |
| 41 | + return [$method(...$arguments)]; |
| 42 | + } |
| 43 | + |
| 44 | + if (self::$arrayArgumentHelper->arrayArgumentCount() === 1) { |
| 45 | + $nthArgument = self::$arrayArgumentHelper->getFirstArrayArgumentNumber(); |
| 46 | + |
| 47 | + return self::evaluateNthArgumentAsArray($method, $nthArgument, ...$arguments); |
| 48 | + } |
| 49 | + |
| 50 | + $singleRowVectorIndex = self::$arrayArgumentHelper->getSingleRowVector(); |
| 51 | + $singleColumnVectorIndex = self::$arrayArgumentHelper->getSingleColumnVector(); |
| 52 | + if ($singleRowVectorIndex !== null && $singleColumnVectorIndex !== null) { |
| 53 | + // Basic logic for a single row vector and a single column vector |
| 54 | + return self::evaluateVectorPair($method, $singleRowVectorIndex, $singleColumnVectorIndex, ...$arguments); |
| 55 | + } |
| 56 | + |
| 57 | + $rowVectorIndexes = self::$arrayArgumentHelper->getRowVectors(); |
| 58 | + $columnVectorIndexes = self::$arrayArgumentHelper->getColumnVectors(); |
| 59 | + |
| 60 | + // Logic for a two row vectors or two column vectors |
| 61 | + if (count($rowVectorIndexes) === 2 && count($columnVectorIndexes) === 0) { |
| 62 | + return self::evaluateRowVectorPair($method, $rowVectorIndexes, ...$arguments); |
| 63 | + } elseif (count($rowVectorIndexes) === 0 && count($columnVectorIndexes) === 2) { |
| 64 | + return self::evaluateColumnVectorPair($method, $columnVectorIndexes, ...$arguments); |
| 65 | + } |
| 66 | + |
| 67 | + // If we have multiple arrays, and they don't match a row vector/column vector pattern, |
| 68 | + // or two row vectors and two column vectors, |
| 69 | + // then we drop through to an error return for the moment |
| 70 | + // Still need to work out the logic for multiple matrices as array arguments, |
| 71 | + // or when we have more than two arrays |
| 72 | + return ['#VALUE!']; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param mixed ...$arguments |
| 77 | + */ |
| 78 | + private static function evaluateRowVectorPair(callable $method, array $vectorIndexes, ...$arguments): array |
| 79 | + { |
| 80 | + $vector2 = array_pop($vectorIndexes); |
| 81 | + $vectorValues2 = Functions::flattenArray($arguments[$vector2]); |
| 82 | + $vector1 = array_pop($vectorIndexes); |
| 83 | + $vectorValues1 = Functions::flattenArray($arguments[$vector1]); |
| 84 | + |
| 85 | + $result = []; |
| 86 | + foreach ($vectorValues1 as $index => $value1) { |
| 87 | + $value2 = $vectorValues2[$index]; |
| 88 | + $arguments[$vector1] = $value1; |
| 89 | + $arguments[$vector2] = $value2; |
| 90 | + |
| 91 | + $result[] = $method(...$arguments); |
| 92 | + } |
| 93 | + |
| 94 | + return [$result]; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @param mixed ...$arguments |
| 99 | + */ |
| 100 | + private static function evaluateColumnVectorPair(callable $method, array $vectorIndexes, ...$arguments): array |
| 101 | + { |
| 102 | + $vector2 = array_pop($vectorIndexes); |
| 103 | + $vectorValues2 = Functions::flattenArray($arguments[$vector2]); |
| 104 | + $vector1 = array_pop($vectorIndexes); |
| 105 | + $vectorValues1 = Functions::flattenArray($arguments[$vector1]); |
| 106 | + |
| 107 | + $result = []; |
| 108 | + foreach ($vectorValues1 as $index => $value1) { |
| 109 | + $value2 = $vectorValues2[$index]; |
| 110 | + $arguments[$vector1] = $value1; |
| 111 | + $arguments[$vector2] = $value2; |
| 112 | + |
| 113 | + $result[] = [$method(...$arguments)]; |
| 114 | + } |
| 115 | + |
| 116 | + return $result; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @param mixed ...$arguments |
| 121 | + */ |
| 122 | + private static function evaluateVectorPair(callable $method, int $rowIndex, int $columnIndex, ...$arguments): array |
| 123 | + { |
| 124 | + $rowVector = Functions::flattenArray($arguments[$rowIndex]); |
| 125 | + $columnVector = Functions::flattenArray($arguments[$columnIndex]); |
| 126 | + |
| 127 | + $result = []; |
| 128 | + foreach ($columnVector as $column) { |
| 129 | + $rowResults = []; |
| 130 | + foreach ($rowVector as $row) { |
| 131 | + $arguments[$rowIndex] = $row; |
| 132 | + $arguments[$columnIndex] = $column; |
| 133 | + |
| 134 | + $rowResults[] = $method(...$arguments); |
| 135 | + } |
| 136 | + $result[] = $rowResults; |
| 137 | + } |
| 138 | + |
| 139 | + return $result; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Note, offset is from 1 (for the first argument) rather than from 0. |
| 144 | + * |
| 145 | + * @param mixed ...$arguments |
| 146 | + */ |
| 147 | + private static function evaluateNthArgumentAsArray(callable $method, int $nthArgument, ...$arguments): array |
| 148 | + { |
| 149 | + $values = array_slice($arguments, $nthArgument - 1, 1); |
| 150 | + /** @var array $values */ |
| 151 | + $values = array_pop($values); |
| 152 | + |
| 153 | + $result = []; |
| 154 | + foreach ($values as $value) { |
| 155 | + $arguments[$nthArgument - 1] = $value; |
| 156 | + $result[] = $method(...$arguments); |
| 157 | + } |
| 158 | + |
| 159 | + return $result; |
| 160 | + } |
| 161 | +} |
0 commit comments