ldc-bien-etre-animal/app/Http/Controllers/Front/QuestionsResponsesController.php

73 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2026-08-28 08:40:04 +00:00
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Front;
use Inertia\Inertia;
use Inertia\Response;
2026-09-03 16:52:27 +02:00
use Spatie\SchemaOrg\Question;
use Spatie\SchemaOrg\Schema;
2026-08-28 08:40:04 +00:00
use Uwil\LaravelCommon\Controllers\CommonController;
2026-09-03 16:52:27 +02:00
use Uwil\LaravelSchemaOrg\Services\SchemaOrgService;
2026-08-28 08:40:04 +00:00
class QuestionsResponsesController extends CommonController
{
2026-09-03 16:52:27 +02:00
public function index(SchemaOrgService $schemaOrg): Response
2026-08-28 08:40:04 +00:00
{
2026-09-03 16:52:27 +02:00
$trans = \trans('questions_responses_page');
2026-08-28 08:40:04 +00:00
/** @var array<string, string> $themes */
2026-09-03 16:52:27 +02:00
$themes = $trans['themes'];
/** @var string $rawTitle */
$rawTitle = preg_replace('/\s+/', ' ', str_replace('<br />', ' ', $trans['header']['title']));
$title = trim(strip_tags($rawTitle));
/** @var array<int, array{question: string, theme: string, blocks: array<int, array{type: string, content?: string, label?: string, href?: string, color?: string}>}> $questions */
$questions = $trans['questions'];
$mainEntity = collect($questions)
->map(function (array $item): Question {
/** @var array<int, array{type: string, content?: string, label?: string, href?: string, color?: string}> $blocks */
$blocks = $item['blocks'];
$answerText = collect($blocks)
->where('type', 'text')
->map(fn (array $block) => trim(strip_tags((string) ($block['content'] ?? ''))))
->implode(' ');
return Schema::question()
->name(strip_tags($item['question']))
->acceptedAnswer(
Schema::answer()->text($answerText)
);
})
->all();
$faqPage = Schema::faqPage();
$faqPage->setProperty('@id', request()->url() . '#faqpage');
$faqPage->mainEntity($mainEntity);
$webPage = $schemaOrg->setWebPage()
->headline($trans['meta']['seo_title'])
->description($trans['meta']['seo_description'])
->name($title)
->isAccessibleForFree(true)
->keywords(array_values($themes))
->dateCreated(new \DateTimeImmutable())
->dateModified(new \DateTimeImmutable())
->mainEntity(
Schema::faqPage()->setProperty('@id', request()->url() . '#faqpage')
);
2026-08-31 16:54:51 +02:00
return Inertia::render('front/Pages/QuestionsResponses', [
2026-09-03 16:52:27 +02:00
'themes' => collect($themes)
->map(fn (string $label, string $value) => ['label' => $label, 'value' => $value])
2026-08-28 08:40:04 +00:00
->values()
->all(),
2026-09-03 16:52:27 +02:00
'questions' => $questions,
'schemas' => [$webPage, $faqPage],
2026-08-31 16:54:51 +02:00
]);
2026-08-28 08:40:04 +00:00
}
}