You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.1 KiB
69 lines
2.1 KiB
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Http\HttpRequest;
|
|
use App\Http\HttpResponse;
|
|
use App\Http\ViewHttpResponse;
|
|
use App\Model\TeamModel;
|
|
use App\Validation\FieldValidationFail;
|
|
use App\Validation\Validators;
|
|
use \Twig\Environment;
|
|
use Twig\Error\LoaderError;
|
|
use Twig\Error\RuntimeError;
|
|
use Twig\Error\SyntaxError;
|
|
|
|
class TeamController /* verif si les camp sont assignés, sinon erreur 400*/
|
|
{
|
|
private TeamModel $model;
|
|
private Environment $twig;
|
|
|
|
/**
|
|
* @param TeamModel $model
|
|
* @param Environment $twig
|
|
*/
|
|
public function __construct(TeamModel $model, Environment $twig) {
|
|
$this->model = $model;
|
|
$this->twig = $twig;
|
|
}
|
|
|
|
public function displaySubmitTeam() {
|
|
try {
|
|
$this->twig->display("insertTeam.html.twig", []);
|
|
} catch (LoaderError | RuntimeError | SyntaxError $e) {
|
|
echo " twig error : $e";
|
|
}
|
|
}
|
|
|
|
public function submitTeam(array $request): HttpResponse {
|
|
|
|
$errors = [];
|
|
$request = HttpRequest::from($request, $errors, [
|
|
"name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()],
|
|
"mainColor" => [Validators::isInteger(), Validators::isIntInRange(0, 0xffffff)],
|
|
"secondColor" => [Validators::isInteger(), Validators::isIntInRange(0, 0xffffff)],
|
|
"picture" => [Validators::isURL()]
|
|
]);
|
|
if (!empty($errors)) {
|
|
$badFields = [];
|
|
foreach ($errors as $e) {
|
|
if ($e instanceof FieldValidationFail) {
|
|
$badFields[] = $e->getFieldName();
|
|
}
|
|
}
|
|
return ViewHttpResponse::twig('insertTeam.html.twig',['bad_fields'=> $badFields]);
|
|
}
|
|
$this->model->createTeam($request['name'], $request['picture'], intval($request['mainColor']), intval($request['secondColor']));
|
|
return ViewHttpResponse::twig('sample_form.html.twig',[]);
|
|
}
|
|
|
|
public function listTeamByName(array $request): HttpResponse {
|
|
$errors = [];
|
|
|
|
$results = $this->model->listByName($request['name'], $errors);
|
|
|
|
}
|
|
}
|
|
|
|
|