Skip to content

Commit 4ca6fd7

Browse files
committed
WIP3
1 parent 0173ead commit 4ca6fd7

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public function enrichCustomVars(array &$vars, Model $object): array
4747
$varsToReplace = json_decode(json_encode($directorObject->getVars()), true)
4848
+ json_decode(json_encode($directorObject->getInheritedVars()), true);
4949

50-
5150
foreach ($varsToReplace as $varName => $customVar) {
5251
if (isset($vars[$varName])) {
5352
$newVars[] = $this->resolveCustomVarMapping($varName, $customVar, $connection);
@@ -72,7 +71,7 @@ public function enrichCustomVars(array &$vars, Model $object): array
7271
*
7372
* @return array
7473
*/
75-
protected function resolveCustomVarMapping(string $name, $val, DbConnection $conn, bool $grouping = true): array
74+
protected function resolveCustomVarMapping(?string $name, $val, DbConnection $conn, bool $grouping = true): array
7675
{
7776
if (isset($this->fieldConfig[$name])) {
7877
/** @var DirectorDatafield $field */
@@ -83,24 +82,13 @@ protected function resolveCustomVarMapping(string $name, $val, DbConnection $con
8382
$label = $field->get('caption');
8483
$newVarValue = [];
8584
foreach ($val as $nestedVarName => $nestedVarValue) {
86-
if (isset($this->fieldConfig[$nestedVarName]) && is_array($nestedVarValue)) {
87-
$childValues = [];
88-
foreach ($nestedVarValue as $childName => $childValue) {
89-
$childValues[] = $this->resolveCustomVarMapping($childName, $childValue, $conn, false);
90-
}
91-
92-
$newVarValue[] = [$nestedVarName => array_merge([], ...$childValues)];
93-
} else {
94-
$newVarValue[] = $this->resolveCustomVarMapping(
95-
$nestedVarName,
96-
$nestedVarValue,
97-
$conn,
98-
false
99-
);
100-
}
85+
$newVarValue[$nestedVarName] = $this->buildDictionaryMap(
86+
$nestedVarValue,
87+
$conn
88+
);
10189
}
10290

103-
return [$label => array_merge([], ...$newVarValue)];
91+
return [$label => $newVarValue];
10492
} elseif ($dataType === get_class(new DataTypeDatalist())) {
10593
if (isset($this->datalistMaps[$name])) {
10694
$val = $this->datalistMaps[$name][$val];
@@ -132,6 +120,15 @@ protected function resolveCustomVarMapping(string $name, $val, DbConnection $con
132120
return [$name => $val];
133121
}
134122

123+
private function buildDictionaryMap($val, DbConnection $connection): array
124+
{
125+
foreach ($val as $childName => $childValue) {
126+
$newValue[] = $this->resolveCustomVarMapping($childName, $childValue, $connection, false);
127+
}
128+
129+
return array_merge([], ...$newValue);
130+
}
131+
135132
private function buildDataListMap(DbConnection $db)
136133
{
137134
$fieldsWithDataLists = [];

0 commit comments

Comments
 (0)