| @@ -356,28 +356,46 @@ | ||
| 356 | 356 | * @param string $format Export format |
| 357 | 357 | * @return string Formatted content |
| 358 | 358 | */ |
| 359 | 359 | private function format_brief_for_export(array $brief, string $format): string { |
| 360 | - $brief_data = $brief['brief_data']; | |
| 361 | - | |
| 362 | - $content = "Content Brief: " . $brief['title'] . "\n\n"; | |
| 363 | - $content .= "Target Keywords: " . implode(', ', $brief['target_keywords']) . "\n"; | |
| 364 | - $content .= "Content Type: " . $brief['content_type'] . "\n\n"; | |
| 365 | - | |
| 366 | - if (!empty($brief_data['outline'])) { | |
| 360 | + // Every read here is a field of json_decode() output, so nothing about | |
| 361 | + // its shape is guaranteed. implode() on null and str_repeat() on a | |
| 362 | + // negative count are a TypeError and a ValueError respectively, and | |
| 363 | + // neither is an \Exception — so the catch around this call never | |
| 364 | + // matched and an export of a malformed brief was a fatal (#394). | |
| 365 | + $brief_data = is_array($brief['brief_data'] ?? null) ? $brief['brief_data'] : []; | |
| 366 | + $keywords = is_array($brief['target_keywords'] ?? null) ? $brief['target_keywords'] : []; | |
| 367 | + | |
| 368 | + $content = "Content Brief: " . (string) ($brief['title'] ?? '') . "\n\n"; | |
| 369 | + $content .= "Target Keywords: " . implode(', ', array_map('strval', $keywords)) . "\n"; | |
| 370 | + $content .= "Content Type: " . (string) ($brief['content_type'] ?? '') . "\n\n"; | |
| 371 | + | |
| 372 | + if (!empty($brief_data['outline']) && is_array($brief_data['outline'])) { | |
| 367 | 373 | $content .= "Content Outline:\n"; |
| 374 | + | |
| 368 | 375 | foreach ($brief_data['outline'] as $item) { |
| 369 | - $indent = str_repeat(' ', $item['level'] - 1); | |
| 370 | - $content .= $indent . "H{$item['level']}: " . $item['heading']; | |
| 371 | - if ($item['word_count'] > 0) { | |
| 372 | - $content .= " ({$item['word_count']} words)"; | |
| 376 | + if (!is_array($item)) { | |
| 377 | + continue; | |
| 373 | 378 | } |
| 379 | + | |
| 380 | + // Clamped: a level of 0 or a missing one made the repeat count | |
| 381 | + // negative. | |
| 382 | + $level = max(1, min(6, (int) ($item['level'] ?? 1))); | |
| 383 | + $word_count = (int) ($item['word_count'] ?? 0); | |
| 384 | + $indent = str_repeat(' ', $level - 1); | |
| 385 | + | |
| 386 | + $content .= $indent . "H{$level}: " . (string) ($item['heading'] ?? ''); | |
| 387 | + | |
| 388 | + if ($word_count > 0) { | |
| 389 | + $content .= " ({$word_count} words)"; | |
| 390 | + } | |
| 391 | + | |
| 374 | 392 | $content .= "\n"; |
| 375 | 393 | } |
| 376 | 394 | } |
| 377 | - | |
| 378 | - $content .= "\nGenerated on: " . $brief['created_at']; | |
| 379 | - | |
| 395 | + | |
| 396 | + $content .= "\nGenerated on: " . (string) ($brief['created_at'] ?? ''); | |
| 397 | + | |
| 380 | 398 | return $content; |
| 381 | 399 | } |
| 382 | 400 | |
| 383 | 401 | /** |