1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
|
7 |
|
|
|
8 |
class Lookup
|
|
|
9 |
{
|
|
|
10 |
use ArrayEnabled;
|
|
|
11 |
|
|
|
12 |
/**
|
|
|
13 |
* LOOKUP
|
|
|
14 |
* The LOOKUP function searches for value either from a one-row or one-column range or from an array.
|
|
|
15 |
*
|
|
|
16 |
* @param mixed $lookupValue The value that you want to match in lookup_array
|
|
|
17 |
* @param mixed $lookupVector The range of cells being searched
|
|
|
18 |
* @param null|mixed $resultVector The column from which the matching value must be returned
|
|
|
19 |
*
|
|
|
20 |
* @return mixed The value of the found cell
|
|
|
21 |
*/
|
|
|
22 |
public static function lookup(mixed $lookupValue, mixed $lookupVector, $resultVector = null): mixed
|
|
|
23 |
{
|
|
|
24 |
if (is_array($lookupValue)) {
|
|
|
25 |
return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $lookupValue, $lookupVector, $resultVector);
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
if (!is_array($lookupVector)) {
|
|
|
29 |
return ExcelError::NA();
|
|
|
30 |
}
|
|
|
31 |
$hasResultVector = isset($resultVector);
|
|
|
32 |
$lookupRows = self::rowCount($lookupVector);
|
|
|
33 |
$lookupColumns = self::columnCount($lookupVector);
|
|
|
34 |
// we correctly orient our results
|
|
|
35 |
if (($lookupRows === 1 && $lookupColumns > 1) || (!$hasResultVector && $lookupRows === 2 && $lookupColumns !== 2)) {
|
|
|
36 |
$lookupVector = Matrix::transpose($lookupVector);
|
|
|
37 |
$lookupRows = self::rowCount($lookupVector);
|
|
|
38 |
$lookupColumns = self::columnCount($lookupVector);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
$resultVector = self::verifyResultVector($resultVector ?? $lookupVector);
|
|
|
42 |
|
|
|
43 |
if ($lookupRows === 2 && !$hasResultVector) {
|
|
|
44 |
$resultVector = array_pop($lookupVector);
|
|
|
45 |
$lookupVector = array_shift($lookupVector);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
if ($lookupColumns !== 2) {
|
|
|
49 |
$lookupVector = self::verifyLookupValues($lookupVector, $resultVector);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
return VLookup::lookup($lookupValue, $lookupVector, 2);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
private static function verifyLookupValues(array $lookupVector, array $resultVector): array
|
|
|
56 |
{
|
|
|
57 |
foreach ($lookupVector as &$value) {
|
|
|
58 |
if (is_array($value)) {
|
|
|
59 |
$k = array_keys($value);
|
|
|
60 |
$key1 = $key2 = array_shift($k);
|
|
|
61 |
++$key2;
|
|
|
62 |
$dataValue1 = $value[$key1];
|
|
|
63 |
} else {
|
|
|
64 |
$key1 = 0;
|
|
|
65 |
$key2 = 1;
|
|
|
66 |
$dataValue1 = $value;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
$dataValue2 = array_shift($resultVector);
|
|
|
70 |
if (is_array($dataValue2)) {
|
|
|
71 |
$dataValue2 = array_shift($dataValue2);
|
|
|
72 |
}
|
|
|
73 |
$value = [$key1 => $dataValue1, $key2 => $dataValue2];
|
|
|
74 |
}
|
|
|
75 |
unset($value);
|
|
|
76 |
|
|
|
77 |
return $lookupVector;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
private static function verifyResultVector(array $resultVector): array
|
|
|
81 |
{
|
|
|
82 |
$resultRows = self::rowCount($resultVector);
|
|
|
83 |
$resultColumns = self::columnCount($resultVector);
|
|
|
84 |
|
|
|
85 |
// we correctly orient our results
|
|
|
86 |
if ($resultRows === 1 && $resultColumns > 1) {
|
|
|
87 |
$resultVector = Matrix::transpose($resultVector);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
return $resultVector;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
private static function rowCount(array $dataArray): int
|
|
|
94 |
{
|
|
|
95 |
return count($dataArray);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
private static function columnCount(array $dataArray): int
|
|
|
99 |
{
|
|
|
100 |
$rowKeys = array_keys($dataArray);
|
|
|
101 |
$row = array_shift($rowKeys);
|
|
|
102 |
|
|
|
103 |
return count($dataArray[$row]);
|
|
|
104 |
}
|
|
|
105 |
}
|