ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Lookup.php
Go to the documentation of this file.
1<?php
2
4
7
8class Lookup
9{
20 public static function lookup($lookupValue, $lookupVector, $resultVector = null)
21 {
22 $lookupValue = Functions::flattenSingleValue($lookupValue);
23
24 if (!is_array($lookupVector)) {
25 return Functions::NA();
26 }
27 $hasResultVector = isset($resultVector);
28 $lookupRows = self::rowCount($lookupVector);
29 $lookupColumns = self::columnCount($lookupVector);
30 // we correctly orient our results
31 if (($lookupRows === 1 && $lookupColumns > 1) || (!$hasResultVector && $lookupRows === 2 && $lookupColumns !== 2)) {
32 $lookupVector = LookupRef\Matrix::transpose($lookupVector);
33 $lookupRows = self::rowCount($lookupVector);
34 $lookupColumns = self::columnCount($lookupVector);
35 }
36
37 $resultVector = self::verifyResultVector($lookupVector, $resultVector);
38
39 if ($lookupRows === 2 && !$hasResultVector) {
40 $resultVector = array_pop($lookupVector);
41 $lookupVector = array_shift($lookupVector);
42 }
43
44 if ($lookupColumns !== 2) {
45 $lookupVector = self::verifyLookupValues($lookupVector, $resultVector);
46 }
47
48 return VLookup::lookup($lookupValue, $lookupVector, 2);
49 }
50
51 private static function verifyLookupValues(array $lookupVector, array $resultVector): array
52 {
53 foreach ($lookupVector as &$value) {
54 if (is_array($value)) {
55 $k = array_keys($value);
56 $key1 = $key2 = array_shift($k);
57 ++$key2;
58 $dataValue1 = $value[$key1];
59 } else {
60 $key1 = 0;
61 $key2 = 1;
62 $dataValue1 = $value;
63 }
64
65 $dataValue2 = array_shift($resultVector);
66 if (is_array($dataValue2)) {
67 $dataValue2 = array_shift($dataValue2);
68 }
69 $value = [$key1 => $dataValue1, $key2 => $dataValue2];
70 }
71 unset($value);
72
73 return $lookupVector;
74 }
75
76 private static function verifyResultVector(array $lookupVector, $resultVector)
77 {
78 if ($resultVector === null) {
79 $resultVector = $lookupVector;
80 }
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 = LookupRef\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}
An exception for terminatinating execution or to throw for unit testing.
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
static verifyResultVector(array $lookupVector, $resultVector)
Definition: Lookup.php:76
static lookup($lookupValue, $lookupVector, $resultVector=null)
LOOKUP The LOOKUP function searches for value either from a one-row or one-column range or from an ar...
Definition: Lookup.php:20
static verifyLookupValues(array $lookupVector, array $resultVector)
Definition: Lookup.php:51
static transpose($matrixData)
TRANSPOSE.
Definition: Matrix.php:17
static lookup($lookupValue, $lookupArray, $indexNumber, $notExactMatch=true)
VLOOKUP The VLOOKUP function searches for value in the left-most column of lookup_array and returns t...
Definition: VLookup.php:24
$row