parent
a11dd2e122
commit
038686e2ed
@ -1,26 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
require_once __DIR__ . "/../react-display.php";
|
||||
|
||||
use App\Validation\ValidationFail;
|
||||
use Twig\Environment;
|
||||
use Twig\Error\LoaderError;
|
||||
use Twig\Error\RuntimeError;
|
||||
use Twig\Error\SyntaxError;
|
||||
|
||||
class ErrorController {
|
||||
/**
|
||||
* @param ValidationFail[] $failures
|
||||
* @param Environment $twig
|
||||
* @return void
|
||||
*/
|
||||
public static function displayFailures(array $failures, Environment $twig): void {
|
||||
try {
|
||||
$twig->display("error.html.twig", ['failures' => $failures]);
|
||||
} catch (LoaderError|RuntimeError|SyntaxError $e) {
|
||||
echo "Twig error: $e";
|
||||
}
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
require_once __DIR__ . "/../react-display.php";
|
||||
|
||||
use App\Gateway\FormResultGateway;
|
||||
use App\Http\HttpRequest;
|
||||
use App\Http\HttpResponse;
|
||||
use App\Http\ViewHttpResponse;
|
||||
use App\Validation\Validators;
|
||||
|
||||
class SampleFormController {
|
||||
private FormResultGateway $gateway;
|
||||
|
||||
/**
|
||||
* @param FormResultGateway $gateway
|
||||
*/
|
||||
public function __construct(FormResultGateway $gateway) {
|
||||
$this->gateway = $gateway;
|
||||
}
|
||||
|
||||
|
||||
public function displayFormReact(): HttpResponse {
|
||||
return ViewHttpResponse::react("views/SampleForm.tsx", []);
|
||||
}
|
||||
|
||||
public function displayFormTwig(): HttpResponse {
|
||||
return ViewHttpResponse::twig('sample_form.html.twig', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $form
|
||||
* @param callable(array<array<string, string>>): ViewHttpResponse $response
|
||||
* @return HttpResponse
|
||||
*/
|
||||
private function submitForm(array $form, callable $response): HttpResponse {
|
||||
return Control::runCheckedFrom($form, [
|
||||
"name" => [Validators::lenBetween(0, 32), Validators::name("Le nom ne peut contenir que des lettres, des chiffres et des accents")],
|
||||
"description" => [Validators::lenBetween(0, 512)],
|
||||
], function (HttpRequest $req) use ($response) {
|
||||
$description = htmlspecialchars($req["description"]);
|
||||
$this->gateway->insert($req["name"], $description);
|
||||
$results = ["results" => $this->gateway->listResults()];
|
||||
return call_user_func_array($response, [$results]);
|
||||
}, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $form
|
||||
* @return HttpResponse
|
||||
*/
|
||||
public function submitFormTwig(array $form): HttpResponse {
|
||||
return $this->submitForm($form, fn(array $results) => ViewHttpResponse::twig('display_results.html.twig', $results));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $form
|
||||
* @return HttpResponse
|
||||
*/
|
||||
public function submitFormReact(array $form): HttpResponse {
|
||||
return $this->submitForm($form, fn(array $results) => ViewHttpResponse::react('views/DisplayResults.tsx', $results));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue