web/app/Http/Controllers/AnagramController.php

49 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2026-08-22 23:50:21 +02:00
<?php
namespace App\Http\Controllers;
2026-08-23 10:04:08 +02:00
use App\Http\Requests\FindAnagramWordsRequest;
use App\Services\AnagramSearch;
2026-08-23 10:04:08 +02:00
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Validator;
2026-08-22 23:50:21 +02:00
use Illuminate\View\View;
class AnagramController extends Controller
{
public function __construct(private readonly AnagramSearch $search) {}
2026-08-23 10:04:08 +02:00
public function index(): View
2026-08-22 23:50:21 +02:00
{
return view('index');
}
2026-08-23 10:04:08 +02:00
public function find(FindAnagramWordsRequest $request): RedirectResponse
{
return redirect()->route('results', $request->validated('anagram'));
2026-08-23 10:04:08 +02:00
}
public function results(string $anagram): View|RedirectResponse
2026-08-23 10:04:08 +02:00
{
// The URL segment bypasses the form request, so it gets the same rules.
$anagram = strtolower($anagram);
$validator = Validator::make(
['anagram' => $anagram],
(new FindAnagramWordsRequest)->rules(),
(new FindAnagramWordsRequest)->messages(),
);
if ($validator->fails()) {
return redirect()
->route('home')
->withErrors($validator)
->withInput(['anagram' => $anagram]);
}
2026-08-23 10:04:08 +02:00
return view('index', [
'anagram' => $anagram,
'matches' => $this->search->find($anagram),
]);
2026-08-23 10:04:08 +02:00
}
2026-08-22 23:50:21 +02:00
}