ldc-bien-etre-animal/app/Http/Controllers/Front/QuestionsResponsesController.php
2026-09-04 17:29:23 +02:00

72 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Front;
use Inertia\Inertia;
use Inertia\Response;
use Spatie\SchemaOrg\Question;
use Spatie\SchemaOrg\Schema;
use Uwil\LaravelCommon\Controllers\CommonController;
use Uwil\LaravelSchemaOrg\Services\SchemaOrgService;
class QuestionsResponsesController extends CommonController
{
public function index(SchemaOrgService $schemaOrg): Response
{
$trans = \trans('questions_responses_page');
/** @var array<string, string> $themes */
$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')
);
return Inertia::render('front/Pages/QuestionsResponses', [
'themes' => collect($themes)
->map(fn (string $label, string $value) => ['label' => $label, 'value' => $value])
->values()
->all(),
'questions' => $questions,
'schemas' => [$webPage, $faqPage],
]);
}
}