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