meta data za SMS

This commit is contained in:
Simon Pocrnjič
2025-11-06 22:30:17 +01:00
parent 1395b72ae8
commit 46feba2df7
4 changed files with 195 additions and 4 deletions
@@ -98,6 +98,10 @@ public function show(Package $package, SmsService $sms): Response
'start_date' => (string) ($c->start_date ?? ''),
'end_date' => (string) ($c->end_date ?? ''),
];
// Include contract.meta as flattened key-value pairs
if (is_array($c->meta) && ! empty($c->meta)) {
$vars['contract']['meta'] = $this->flattenMeta($c->meta);
}
if ($c->account) {
$initialRaw = (string) $c->account->initial_amount;
$balanceRaw = (string) $c->account->balance_amount;
@@ -157,6 +161,10 @@ public function show(Package $package, SmsService $sms): Response
'start_date' => (string) ($c->start_date ?? ''),
'end_date' => (string) ($c->end_date ?? ''),
];
// Include contract.meta as flattened key-value pairs
if (is_array($c->meta) && ! empty($c->meta)) {
$vars['contract']['meta'] = $this->flattenMeta($c->meta);
}
if ($c->account) {
$initialRaw = (string) $c->account->initial_amount;
$balanceRaw = (string) $c->account->balance_amount;
@@ -479,4 +487,47 @@ public function storeFromContracts(StorePackageFromContractsRequest $request, Ph
return back()->with('success', 'Package created from contracts');
}
/**
* Flatten nested meta structure into dot-notation key-value pairs.
* Extracts 'value' from objects with {title, value, type} structure.
* Also creates direct access aliases for nested fields (skipping numeric keys).
*/
private function flattenMeta(array $meta, string $prefix = ''): array
{
$result = [];
foreach ($meta as $key => $value) {
$newKey = $prefix === '' ? $key : "{$prefix}.{$key}";
if (is_array($value)) {
// Check if it's a structured meta entry with 'value' field
if (isset($value['value'])) {
$result[$newKey] = $value['value'];
// If parent key is numeric, also create direct alias without the number
if ($prefix !== '' && is_numeric($key)) {
$result[$key] = $value['value'];
}
} else {
// Recursively flatten nested arrays
$nested = $this->flattenMeta($value, $newKey);
$result = array_merge($result, $nested);
// If current key is numeric, also flatten without it for easier access
if (is_numeric($key)) {
$directNested = $this->flattenMeta($value, $prefix);
foreach ($directNested as $dk => $dv) {
// Only add if not already set (prefer first occurrence)
if (! isset($result[$dk])) {
$result[$dk] = $dv;
}
}
}
}
} else {
$result[$newKey] = $value;
}
}
return $result;
}
}