Newer
Older
<?php
namespace TPNOTE2024\Controllers;
use TPNOTE2024\Domain\Convert;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Twig\Environment;
class ConvertController
{
public function __construct(private readonly Convert $convert, private readonly Environment $twig) {}
function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args)
{
if (empty($_POST["unite"]) || empty($_POST["nbr"])) {
throw new \Exception("Erreur: valeur(s) manquante(s)");
}
if (!filter_var($_POST["nbr"], FILTER_VALIDATE_INT)) {
throw new \Exception("Erreur: la valeur doit être un entier.");
}
$convertedValues = $this->convert->convertion($_POST["nbr"], $_POST["unite"]);
$output = $this->twig->render('converted.tpl', [
'nbr' => $_POST["nbr"],
'metric' => $_POST["unite"],
'result' => [
'inches' => $convertedValues->inch,
'feet' => $convertedValues->feet,
'yards' => $convertedValues->yard
]
]);
$response->getBody()->write($output);
return $response;
exit;
}
}