fixed the fixed

This commit is contained in:
Simon Pocrnjič
2026-05-18 12:37:12 +02:00
parent b6405764a9
commit 7ab890005b
+38 -7
View File
@@ -188,13 +188,7 @@ protected function buildMap(array $ctx): array
$meta = json_decode($meta, true) ?? [];
}
if (is_array($meta)) {
// Flatten meta entries. Supports two formats:
// - Structured: { "sklic": { "value": "...", "type": "string", ... } } → extract 'value'
// - Plain: { "sklic": "..." } → use as-is
$out['contract']['meta'] = array_map(
fn ($item) => (is_array($item) && array_key_exists('value', $item)) ? $item['value'] : $item,
$meta
);
$out['contract']['meta'] = $this->flattenMetaForTemplate($meta);
}
}
if (isset($ctx['activity'])) {
@@ -225,4 +219,41 @@ protected function buildMap(array $ctx): array
return $out;
}
/**
* Flatten a contract meta array so every leaf value is accessible by its bare key.
*
* Handles three formats stored in the wild:
* 1. Numeric wrapper: { "1": { "sklic": "SI00…", "job_days": 1 } }
* { "sklic": "SI00…", "job_days": 1 }
* 2. Structured entry: { "sklic": { "value": "SI00…", "type": "string" } }
* { "sklic": "SI00…" }
* 3. Already flat: { "sklic": "SI00…" }
* { "sklic": "SI00…" }
*/
private function flattenMetaForTemplate(array $meta): array
{
$flat = [];
foreach ($meta as $key => $item) {
if (!is_array($item)) {
// Plain scalar — keep as-is (format 3)
if (!array_key_exists($key, $flat)) {
$flat[$key] = $item;
}
} elseif (array_key_exists('value', $item)) {
// Structured { value, type, title } entry (format 2)
$flat[$key] = $item['value'];
} elseif (is_numeric($key)) {
// Numeric wrapper key — recurse and alias without the prefix (format 1)
foreach ($this->flattenMetaForTemplate($item) as $nk => $nv) {
if (!array_key_exists($nk, $flat)) {
$flat[$nk] = $nv;
}
}
}
// Non-numeric nested arrays without a 'value' key are silently skipped
}
return $flat;
}
}