| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\LookupRef; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 8 |
|
| 9 |
class Matrix |
| 10 |
{ |
| 11 |
use ArrayEnabled; |
| 12 |
|
| 13 |
/** |
| 14 |
* Helper function; NOT an implementation of any Excel Function. |
| 15 |
* |
| 16 |
* @param mixed[] $values |
| 17 |
*/ |
| 18 |
public static function isColumnVector(array $values): bool |
| 19 |
{ |
| 20 |
return count($values, COUNT_RECURSIVE) === (count($values, COUNT_NORMAL) * 2); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Helper function; NOT an implementation of any Excel Function. |
| 25 |
* |
| 26 |
* @param mixed[] $values |
| 27 |
*/ |
| 28 |
public static function isRowVector(array $values): bool |
| 29 |
{ |
| 30 |
return count($values, COUNT_RECURSIVE) > 1 |
| 31 |
&& (count($values, COUNT_NORMAL) === 1 || count($values, COUNT_RECURSIVE) === count($values, COUNT_NORMAL)); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* TRANSPOSE. |
| 36 |
* |
| 37 |
* @param mixed $matrixData A matrix of values |
| 38 |
* |
| 39 |
* @return mixed[] |
| 40 |
*/ |
| 41 |
public static function transpose($matrixData): array |
| 42 |
{ |
| 43 |
$returnMatrix = []; |
| 44 |
if (!is_array($matrixData)) { |
| 45 |
$matrixData = [[$matrixData]]; |
| 46 |
} |
| 47 |
if (!is_array(end($matrixData))) { |
| 48 |
$matrixData = [$matrixData]; |
| 49 |
} |
| 50 |
|
| 51 |
$column = 0; |
| 52 |
/** @var mixed[][] $matrixData */ |
| 53 |
foreach ($matrixData as $matrixRow) { |
| 54 |
$row = 0; |
| 55 |
foreach ($matrixRow as $matrixCell) { |
| 56 |
$returnMatrix[$row][$column] = $matrixCell; |
| 57 |
++$row; |
| 58 |
} |
| 59 |
++$column; |
| 60 |
} |
| 61 |
|
| 62 |
return $returnMatrix; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* INDEX. |
| 67 |
* |
| 68 |
* Uses an index to choose a value from a reference or array |
| 69 |
* |
| 70 |
* Excel Function: |
| 71 |
* =INDEX(range_array, row_num, [column_num], [area_num]) |
| 72 |
* |
| 73 |
* @param mixed $matrix A range of cells or an array constant |
| 74 |
* @param mixed $rowNum The row in the array or range from which to return a value. |
| 75 |
* If row_num is omitted, column_num is required. |
| 76 |
* Or can be an array of values |
| 77 |
* @param mixed $columnNum The column in the array or range from which to return a value. |
| 78 |
* If column_num is omitted, row_num is required. |
| 79 |
* Or can be an array of values |
| 80 |
* |
| 81 |
* TODO Provide support for area_num, currently not supported |
| 82 |
* |
| 83 |
* @return mixed the value of a specified cell or array of cells |
| 84 |
* If an array of values is passed as the $rowNum and/or $columnNum arguments, then the returned result |
| 85 |
* will also be an array with the same dimensions |
| 86 |
*/ |
| 87 |
public static function index($matrix, $rowNum = 0, $columnNum = null) |
| 88 |
{ |
| 89 |
if (is_array($rowNum) || is_array($columnNum)) { |
| 90 |
return self::evaluateArrayArgumentsSubsetFrom([self::class, __FUNCTION__], 1, $matrix, $rowNum, $columnNum); |
| 91 |
} |
| 92 |
|
| 93 |
$rowNum = $rowNum ?? 0; |
| 94 |
$columnNum = $columnNum ?? 0; |
| 95 |
if (is_scalar($matrix)) { |
| 96 |
if ($rowNum === 0 || $rowNum === 1) { |
| 97 |
if ($columnNum === 0 || $columnNum === 1) { |
| 98 |
if ($columnNum === 1 || $rowNum === 1) { |
| 99 |
return $matrix; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
try { |
| 106 |
$rowNum = LookupRefValidations::validatePositiveInt($rowNum); |
| 107 |
$columnNum = LookupRefValidations::validatePositiveInt($columnNum); |
| 108 |
} catch (Exception $e) { |
| 109 |
return $e->getMessage(); |
| 110 |
} |
| 111 |
|
| 112 |
if (is_array($matrix) && count($matrix) === 1 && $rowNum > 1) { |
| 113 |
$matrixKey = array_keys($matrix)[0]; |
| 114 |
if (is_array($matrix[$matrixKey])) { |
| 115 |
$tempMatrix = []; |
| 116 |
foreach ($matrix[$matrixKey] as $key => $value) { |
| 117 |
$tempMatrix[$key] = [$value]; |
| 118 |
} |
| 119 |
$matrix = $tempMatrix; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if (!is_array($matrix) || ($rowNum > count($matrix))) { |
| 124 |
return ExcelError::REF(); |
| 125 |
} |
| 126 |
|
| 127 |
$rowKeys = array_keys($matrix); |
| 128 |
$columnKeys = @array_keys($matrix[$rowKeys[0]]); //* @phpstan-ignore argument.type (argument 1 of array_keys may be mixed not array) |
| 129 |
|
| 130 |
if ($columnNum > count($columnKeys)) { |
| 131 |
return ExcelError::REF(); |
| 132 |
} |
| 133 |
|
| 134 |
if ($columnNum === 0) { |
| 135 |
return self::extractRowValue($matrix, $rowKeys, $rowNum); |
| 136 |
} |
| 137 |
|
| 138 |
$columnNum = $columnKeys[--$columnNum]; //* @phpstan-ignore offsetAccess.notFound (perhaps index --$columnNum does not exist) |
| 139 |
if ($rowNum === 0) { |
| 140 |
return array_map( |
| 141 |
fn ($value): array => [$value], |
| 142 |
array_column($matrix, $columnNum) |
| 143 |
); |
| 144 |
} |
| 145 |
$rowNum = $rowKeys[--$rowNum]; //* @phpstan-ignore offsetAccess.notFound (perhaps index --$rowNum does not exist) |
| 146 |
/** @var mixed[][] $matrix */ |
| 147 |
|
| 148 |
return $matrix[$rowNum][$columnNum]; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* @param mixed[] $matrix |
| 153 |
* @param array<int, int> $rowKeys |
| 154 |
* @return mixed |
| 155 |
*/ |
| 156 |
private static function extractRowValue(array $matrix, array $rowKeys, int $rowNum) |
| 157 |
{ |
| 158 |
if ($rowNum === 0) { |
| 159 |
return $matrix; |
| 160 |
} |
| 161 |
|
| 162 |
$rowNum = $rowKeys[--$rowNum]; |
| 163 |
$row = $matrix[$rowNum]; |
| 164 |
if (is_array($row)) { |
| 165 |
return [$rowNum => $row]; |
| 166 |
} |
| 167 |
|
| 168 |
return $row; |
| 169 |
} |
| 170 |
} |
| 171 |
|