wip FrontController
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
3422ef8780
commit
61a6b5afd5
@ -1,52 +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', []);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public function submitFormTwig(array $form): HttpResponse {
|
||||
return $this->submitForm($form, fn(array $results) => ViewHttpResponse::twig('display_results.html.twig', $results));
|
||||
}
|
||||
|
||||
public function submitFormReact(array $form): HttpResponse {
|
||||
return $this->submitForm($form, fn(array $results) => ViewHttpResponse::react('views/DisplayResults.tsx', $results));
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Gateway;
|
||||
|
||||
use PDO;
|
||||
use App\Connexion;
|
||||
|
||||
/**
|
||||
* A sample gateway, that stores the sample form's result.
|
||||
*/
|
||||
class FormResultGateway {
|
||||
|
||||
private Connexion $con;
|
||||
|
||||
public function __construct(Connexion $con) {
|
||||
$this->con = $con;
|
||||
}
|
||||
|
||||
|
||||
function insert(string $username, string $description) {
|
||||
$this->con->exec(
|
||||
"INSERT INTO FormEntries VALUES (:name, :description)",
|
||||
[
|
||||
":name" => [$username, PDO::PARAM_STR],
|
||||
"description" => [$description, PDO::PARAM_STR]
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
function listResults(): array {
|
||||
return $this->con->fetch("SELECT * FROM FormEntries", []);
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Twig view</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>Hello, this is a sample form made in Twig !</h1>
|
||||
|
||||
<form action="submit-twig" method="POST">
|
||||
<label for="name">your name: </label>
|
||||
<input type="text" id="name" name="name"/>
|
||||
<label for="password">a little description about yourself: </label>
|
||||
<input type="text" id="password" name="description"/>
|
||||
<input type="submit" value="click me to submit!"/>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in new issue