ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
HLookup.php
Go to the documentation of this file.
1<?php
2
4
8
9class HLookup extends LookupBase
10{
24 public static function lookup($lookupValue, $lookupArray, $indexNumber, $notExactMatch = true)
25 {
26 $lookupValue = Functions::flattenSingleValue($lookupValue);
27 $indexNumber = Functions::flattenSingleValue($indexNumber);
28 $notExactMatch = ($notExactMatch === null) ? true : Functions::flattenSingleValue($notExactMatch);
29
30 try {
31 $indexNumber = self::validateIndexLookup($lookupArray, $indexNumber);
32 } catch (Exception $e) {
33 return $e->getMessage();
34 }
35
36 $f = array_keys($lookupArray);
37 $firstRow = reset($f);
38 if ((!is_array($lookupArray[$firstRow])) || ($indexNumber > count($lookupArray))) {
39 return Functions::REF();
40 }
41
42 $firstkey = $f[0] - 1;
43 $returnColumn = $firstkey + $indexNumber;
44 $firstColumn = array_shift($f);
45 $rowNumber = self::hLookupSearch($lookupValue, $lookupArray, $firstColumn, $notExactMatch);
46
47 if ($rowNumber !== null) {
48 // otherwise return the appropriate value
49 return $lookupArray[$returnColumn][$rowNumber];
50 }
51
52 return Functions::NA();
53 }
54
55 private static function hLookupSearch($lookupValue, $lookupArray, $column, $notExactMatch)
56 {
57 $lookupLower = StringHelper::strToLower($lookupValue);
58
59 $rowNumber = null;
60 foreach ($lookupArray[$column] as $rowKey => $rowData) {
61 // break if we have passed possible keys
62 $bothNumeric = is_numeric($lookupValue) && is_numeric($rowData);
63 $bothNotNumeric = !is_numeric($lookupValue) && !is_numeric($rowData);
64 $cellDataLower = StringHelper::strToLower($rowData);
65
66 if (
67 $notExactMatch &&
68 (($bothNumeric && $rowData > $lookupValue) || ($bothNotNumeric && $cellDataLower > $lookupLower))
69 ) {
70 break;
71 }
72
73 $rowNumber = self::checkMatch(
74 $bothNumeric,
75 $bothNotNumeric,
76 $notExactMatch,
77 $rowKey,
78 $cellDataLower,
79 $lookupLower,
80 $rowNumber
81 );
82 }
83
84 return $rowNumber;
85 }
86}
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 hLookupSearch($lookupValue, $lookupArray, $column, $notExactMatch)
Definition: HLookup.php:55
static lookup($lookupValue, $lookupArray, $indexNumber, $notExactMatch=true)
HLOOKUP The HLOOKUP function searches for value in the top-most row of lookup_array and returns the v...
Definition: HLookup.php:24
static validateIndexLookup($lookup_array, $index_number)
Definition: LookupBase.php:10
static checkMatch(bool $bothNumeric, bool $bothNotNumeric, $notExactMatch, int $rowKey, string $cellDataLower, string $lookupLower, ?int $rowNumber)
Definition: LookupBase.php:25
static strToLower($pValue)
Convert a UTF-8 encoded string to lower case.