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;
|
2026-08-23 21:46:14 +02:00
|
|
|
use App\Services\AnagramSearch;
|
2026-08-23 10:04:08 +02:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2026-08-23 21:46:14 +02:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2026-08-22 23:50:21 +02:00
|
|
|
use Illuminate\View\View;
|
|
|
|
|
|
|
|
|
|
class AnagramController extends Controller
|
|
|
|
|
{
|
2026-08-23 21:46:14 +02:00
|
|
|
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
|
|
|
|
|
{
|
2026-08-23 21:46:14 +02:00
|
|
|
return redirect()->route('results', $request->validated('anagram'));
|
2026-08-23 10:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 21:46:14 +02:00
|
|
|
public function results(string $anagram): View|RedirectResponse
|
2026-08-23 10:04:08 +02:00
|
|
|
{
|
2026-08-23 21:46:14 +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
|
|
|
|
2026-08-23 21:46:14 +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
|
|
|
}
|