| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\LookupRef; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\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($lookupValue, $lookupVector, $resultVector = null) |
| 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 |
/** @var mixed[][] $lookupVector */ |
| 32 |
$hasResultVector = isset($resultVector); |
| 33 |
$lookupRows = self::rowCount($lookupVector); |
| 34 |
$lookupColumns = self::columnCount($lookupVector); |
| 35 |
// we correctly orient our results |
| 36 |
if (($lookupRows === 1 && $lookupColumns > 1) || (!$hasResultVector && $lookupRows === 2 && $lookupColumns !== 2)) { |
| 37 |
$lookupVector = Matrix::transpose($lookupVector); |
| 38 |
$lookupRows = self::rowCount($lookupVector); |
| 39 |
/** @var mixed[][] $lookupVector */ |
| 40 |
$lookupColumns = self::columnCount($lookupVector); |
| 41 |
} |
| 42 |
|
| 43 |
$resultVector = self::verifyResultVector($resultVector ?? $lookupVector); //* @phpstan-ignore argument.type (resultVector can be mixed not array<array<mixed>>) |
| 44 |
|
| 45 |
if ($lookupRows === 2 && !$hasResultVector) { |
| 46 |
$resultVector = array_pop($lookupVector); |
| 47 |
$lookupVector = array_shift($lookupVector); |
| 48 |
} |
| 49 |
|
| 50 |
/** @var array<int, mixed> $lookupVector */ |
| 51 |
/** @var array<int, mixed> $resultVector */ |
| 52 |
if ($lookupColumns !== 2) { |
| 53 |
$lookupVector = self::verifyLookupValues($lookupVector, $resultVector); |
| 54 |
} |
| 55 |
|
| 56 |
return VLookup::lookup($lookupValue, $lookupVector, 2); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @param array<int, mixed> $lookupVector |
| 61 |
* @param array<int, mixed> $resultVector |
| 62 |
* |
| 63 |
* @return mixed[] |
| 64 |
*/ |
| 65 |
private static function verifyLookupValues(array $lookupVector, array $resultVector): array |
| 66 |
{ |
| 67 |
foreach ($lookupVector as &$value) { |
| 68 |
if (is_array($value)) { |
| 69 |
$k = array_keys($value); |
| 70 |
$key1 = $key2 = array_shift($k); |
| 71 |
++$key2; |
| 72 |
$dataValue1 = $value[$key1]; |
| 73 |
} else { |
| 74 |
$key1 = 0; |
| 75 |
$key2 = 1; |
| 76 |
$dataValue1 = $value; |
| 77 |
} |
| 78 |
|
| 79 |
$dataValue2 = array_shift($resultVector); |
| 80 |
if (is_array($dataValue2)) { |
| 81 |
$dataValue2 = array_shift($dataValue2); |
| 82 |
} |
| 83 |
/** @var int $key2 */ |
| 84 |
$value = [$key1 => $dataValue1, $key2 => $dataValue2]; |
| 85 |
} |
| 86 |
unset($value); |
| 87 |
|
| 88 |
return $lookupVector; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @param mixed[][] $resultVector |
| 93 |
* |
| 94 |
* @return mixed[] |
| 95 |
*/ |
| 96 |
private static function verifyResultVector(array $resultVector): array |
| 97 |
{ |
| 98 |
$resultRows = self::rowCount($resultVector); |
| 99 |
$resultColumns = self::columnCount($resultVector); |
| 100 |
|
| 101 |
// we correctly orient our results |
| 102 |
if ($resultRows === 1 && $resultColumns > 1) { |
| 103 |
$resultVector = Matrix::transpose($resultVector); |
| 104 |
} |
| 105 |
|
| 106 |
return $resultVector; |
| 107 |
} |
| 108 |
|
| 109 |
/** @param mixed[] $dataArray */ |
| 110 |
private static function rowCount(array $dataArray): int |
| 111 |
{ |
| 112 |
return count($dataArray); |
| 113 |
} |
| 114 |
|
| 115 |
/** @param mixed[][] $dataArray */ |
| 116 |
private static function columnCount(array $dataArray): int |
| 117 |
{ |
| 118 |
$rowKeys = array_keys($dataArray); |
| 119 |
$row = array_shift($rowKeys); |
| 120 |
|
| 121 |
return count($dataArray[$row]); |
| 122 |
} |
| 123 |
} |
| 124 |
|