| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Engine; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 6 |
|
| 7 |
class ArrayArgumentHelper |
| 8 |
{ |
| 9 |
protected int $indexStart = 0; |
| 10 |
|
| 11 |
/** @var mixed[] */ |
| 12 |
protected array $arguments; |
| 13 |
|
| 14 |
protected int $argumentCount; |
| 15 |
|
| 16 |
/** @var int[] */ |
| 17 |
protected array $rows; |
| 18 |
|
| 19 |
/** @var int[] */ |
| 20 |
protected array $columns; |
| 21 |
|
| 22 |
/** @param mixed[] $arguments */ |
| 23 |
public function initialise(array $arguments): void |
| 24 |
{ |
| 25 |
$keys = array_keys($arguments); |
| 26 |
$this->indexStart = (int) array_shift($keys); |
| 27 |
$this->rows = $this->rows($arguments); |
| 28 |
$this->columns = $this->columns($arguments); |
| 29 |
|
| 30 |
$this->argumentCount = count($arguments); |
| 31 |
$this->arguments = $this->flattenSingleCellArrays($arguments, $this->rows, $this->columns); |
| 32 |
|
| 33 |
$this->rows = $this->rows($arguments); |
| 34 |
$this->columns = $this->columns($arguments); |
| 35 |
|
| 36 |
if ($this->arrayArguments() > 2) { |
| 37 |
throw new Exception('Formulae with more than two array arguments are not supported'); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** @return mixed[] */ |
| 42 |
public function arguments(): array |
| 43 |
{ |
| 44 |
return $this->arguments; |
| 45 |
} |
| 46 |
|
| 47 |
public function hasArrayArgument(): bool |
| 48 |
{ |
| 49 |
return $this->arrayArguments() > 0; |
| 50 |
} |
| 51 |
|
| 52 |
public function getFirstArrayArgumentNumber(): int |
| 53 |
{ |
| 54 |
$rowArrays = $this->filterArray($this->rows); |
| 55 |
$columnArrays = $this->filterArray($this->columns); |
| 56 |
|
| 57 |
for ($index = $this->indexStart; $index < $this->argumentCount; ++$index) { |
| 58 |
if (isset($rowArrays[$index]) || isset($columnArrays[$index])) { |
| 59 |
return ++$index; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
public function getSingleRowVector(): ?int |
| 67 |
{ |
| 68 |
$rowVectors = $this->getRowVectors(); |
| 69 |
|
| 70 |
return count($rowVectors) === 1 ? array_pop($rowVectors) : null; |
| 71 |
} |
| 72 |
|
| 73 |
/** @return int[] */ |
| 74 |
private function getRowVectors(): array |
| 75 |
{ |
| 76 |
$rowVectors = []; |
| 77 |
for ($index = $this->indexStart; $index < ($this->indexStart + $this->argumentCount); ++$index) { |
| 78 |
if ($this->rows[$index] === 1 && $this->columns[$index] > 1) { |
| 79 |
$rowVectors[] = $index; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return $rowVectors; |
| 84 |
} |
| 85 |
|
| 86 |
public function getSingleColumnVector(): ?int |
| 87 |
{ |
| 88 |
$columnVectors = $this->getColumnVectors(); |
| 89 |
|
| 90 |
return count($columnVectors) === 1 ? array_pop($columnVectors) : null; |
| 91 |
} |
| 92 |
|
| 93 |
/** @return int[] */ |
| 94 |
private function getColumnVectors(): array |
| 95 |
{ |
| 96 |
$columnVectors = []; |
| 97 |
for ($index = $this->indexStart; $index < ($this->indexStart + $this->argumentCount); ++$index) { |
| 98 |
if ($this->rows[$index] > 1 && $this->columns[$index] === 1) { |
| 99 |
$columnVectors[] = $index; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return $columnVectors; |
| 104 |
} |
| 105 |
|
| 106 |
/** @return int[] */ |
| 107 |
public function getMatrixPair(): array |
| 108 |
{ |
| 109 |
for ($i = $this->indexStart; $i < ($this->indexStart + $this->argumentCount - 1); ++$i) { |
| 110 |
for ($j = $i + 1; $j < $this->argumentCount; ++$j) { |
| 111 |
if (isset($this->rows[$i], $this->rows[$j])) { |
| 112 |
return [$i, $j]; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return []; |
| 118 |
} |
| 119 |
|
| 120 |
public function isVector(int $argument): bool |
| 121 |
{ |
| 122 |
return $this->rows[$argument] === 1 || $this->columns[$argument] === 1; |
| 123 |
} |
| 124 |
|
| 125 |
public function isRowVector(int $argument): bool |
| 126 |
{ |
| 127 |
return $this->rows[$argument] === 1; |
| 128 |
} |
| 129 |
|
| 130 |
public function isColumnVector(int $argument): bool |
| 131 |
{ |
| 132 |
return $this->columns[$argument] === 1; |
| 133 |
} |
| 134 |
|
| 135 |
public function rowCount(int $argument): int |
| 136 |
{ |
| 137 |
return $this->rows[$argument]; |
| 138 |
} |
| 139 |
|
| 140 |
public function columnCount(int $argument): int |
| 141 |
{ |
| 142 |
return $this->columns[$argument]; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @param mixed[] $arguments |
| 147 |
* |
| 148 |
* @return int[] |
| 149 |
*/ |
| 150 |
private function rows(array $arguments): array |
| 151 |
{ |
| 152 |
return array_map( |
| 153 |
fn ($argument): int => is_countable($argument) ? count($argument) : 1, |
| 154 |
$arguments |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* @param mixed[] $arguments |
| 160 |
* |
| 161 |
* @return int[] |
| 162 |
*/ |
| 163 |
private function columns(array $arguments): array |
| 164 |
{ |
| 165 |
return array_map( |
| 166 |
fn ($argument): int => is_array($argument) && is_array($argument[array_keys($argument)[0]]) |
| 167 |
? count($argument[array_keys($argument)[0]]) |
| 168 |
: 1, |
| 169 |
$arguments |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
public function arrayArguments(): int |
| 174 |
{ |
| 175 |
$count = 0; |
| 176 |
foreach (array_keys($this->arguments) as $argument) { |
| 177 |
if ($this->rows[$argument] > 1 || $this->columns[$argument] > 1) { |
| 178 |
++$count; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return $count; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @param mixed[] $arguments |
| 187 |
* @param int[] $rows |
| 188 |
* @param int[] $columns |
| 189 |
* |
| 190 |
* @return mixed[] |
| 191 |
*/ |
| 192 |
private function flattenSingleCellArrays(array $arguments, array $rows, array $columns): array |
| 193 |
{ |
| 194 |
foreach ($arguments as $index => $argument) { |
| 195 |
if ($rows[$index] === 1 && $columns[$index] === 1) { |
| 196 |
while (is_array($argument)) { |
| 197 |
$argument = array_pop($argument); |
| 198 |
} |
| 199 |
$arguments[$index] = $argument; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return $arguments; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* @param mixed[] $array |
| 208 |
* |
| 209 |
* @return mixed[] |
| 210 |
*/ |
| 211 |
private function filterArray(array $array): array |
| 212 |
{ |
| 213 |
return array_filter( |
| 214 |
$array, |
| 215 |
fn ($value): bool => $value > 1 |
| 216 |
); |
| 217 |
} |
| 218 |
} |
| 219 |
|