From 76e64a5ae5511f7f1198358f6fb67564225558de Mon Sep 17 00:00:00 2001 From: "mael.daim" Date: Wed, 22 Nov 2023 12:23:45 +0100 Subject: [PATCH] formated the code and verification plus some more css --- public/index.php | 12 +++++----- src/Controller/TeamController.php | 20 +++++++++------- src/Data/Color.php | 13 +++++----- src/Data/MemberRole.php | 5 ++-- src/Data/Team.php | 2 +- src/Gateway/TeamGateway.php | 40 +++++++++++++++++++++---------- src/Model/TeamModel.php | 5 ++-- src/Validation/Validators.php | 7 ++---- src/Views/display_team.html.twig | 13 +++++----- 9 files changed, 66 insertions(+), 51 deletions(-) diff --git a/public/index.php b/public/index.php index 3e4bf87..6633860 100644 --- a/public/index.php +++ b/public/index.php @@ -49,14 +49,14 @@ $router->map("GET", "/tactic/new", fn() => $editorController->makeNew()); $router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => $editorController->openEditorFor($id)); $router->map("GET", "/tactic/[i:id]", fn(int $id) => $visualizerController->openVisualizer($id)); -$teamController = new \App\Controller\TeamController(new \App\Model\TeamModel(new \App\Gateway\TeamGateway($con)),$twig); -$router->map("GET","/team/new", fn()=>$teamController->displaySubmitTeam()); -$router->map("POST","/team/new", fn()=>$teamController->SubmitTeam($_POST)); +$teamController = new \App\Controller\TeamController(new \App\Model\TeamModel(new \App\Gateway\TeamGateway($con))); +$router->map("GET", "/team/new", fn() => $teamController->displaySubmitTeam()); +$router->map("POST", "/team/new", fn() => $teamController->SubmitTeam($_POST)); -$router->map("GET","/team/list", fn()=>$teamController->displayListTeamByName()); -$router->map("POST","/team/list", fn()=>$teamController->ListTeamByName($_POST)); +$router->map("GET", "/team/list", fn() => $teamController->displayListTeamByName()); +$router->map("POST", "/team/list", fn() => $teamController->ListTeamByName($_POST)); -$router->map("GET","/team/[i:id]", fn(int $id)=>$teamController->getTeam($id)); +$router->map("GET", "/team/[i:id]", fn(int $id) => $teamController->getTeam($id)); $match = $router->match(); diff --git a/src/Controller/TeamController.php b/src/Controller/TeamController.php index d4c7bfe..08e22cc 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/TeamController.php @@ -8,10 +8,8 @@ use App\Http\ViewHttpResponse; use App\Model\TeamModel; use App\Validation\FieldValidationFail; use App\Validation\Validators; -use \Twig\Environment; -class TeamController -{ +class TeamController { private TeamModel $model; /** @@ -21,10 +19,14 @@ class TeamController $this->model = $model; } - public function displaySubmitTeam() : HttpResponse { + public function displaySubmitTeam(): HttpResponse { return ViewHttpResponse::twig("insert_team.html.twig", []); } + /** + * @param array $request + * @return HttpResponse + */ public function submitTeam(array $request): HttpResponse { $errors = []; @@ -32,7 +34,7 @@ class TeamController "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()], "mainColor" => [Validators::regex('/#(?:[0-9a-fA-F]{6})/')], "secondColor" => [Validators::regex('/#(?:[0-9a-fA-F]{6})/')], - "picture" => [Validators::isURL()] + "picture" => [Validators::isURL()], ]); if (!empty($errors)) { $badFields = []; @@ -50,10 +52,14 @@ class TeamController return ViewHttpResponse::twig("list_team_by_name.html.twig", []); } + /** + * @param array $request + * @return HttpResponse + */ public function listTeamByName(array $request): HttpResponse { $errors = []; $request = HttpRequest::from($request, $errors, [ - "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()] + "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()], ]); if (!empty($errors) && $errors[0] instanceof FieldValidationFail) { @@ -75,5 +81,3 @@ class TeamController return ViewHttpResponse::twig('display_team.html.twig', ['team' => $result]); } } - - diff --git a/src/Data/Color.php b/src/Data/Color.php index 875b615..b12e27a 100755 --- a/src/Data/Color.php +++ b/src/Data/Color.php @@ -2,31 +2,31 @@ namespace App\Data; -use \InvalidArgumentException; +use InvalidArgumentException; class Color { /** - * @var string 6 bytes unsigned int that represents an RGB color + * @var string that represents an hexadecimal color code */ private string $hex; /** * @param string $value 6 bytes unsigned int that represents an RGB color - * @throws \InvalidArgumentException if the value is negative or greater than 0xFFFFFF + * @throws InvalidArgumentException if the value is negative or greater than 0xFFFFFF */ private function __construct(string $value) { if ($value < 0 || $value > 0xFFFFFF) { throw new InvalidArgumentException("int color value is invalid, must be positive and lower than 0xFFFFFF"); } - $this->value = $value; + $this->hex = $value; } /** * @return string */ public function getValue(): string { - return $this->value; + return $this->hex; } public static function from(string $value): Color { @@ -39,10 +39,9 @@ class Color { } public static function tryFrom(string $value): ?Color { - if (!preg_match('/#(?:[0-9a-fA-F]{6})/',$value)) { + if (!preg_match('/#(?:[0-9a-fA-F]{6})/', $value)) { return null; } return new Color($value); } } - diff --git a/src/Data/MemberRole.php b/src/Data/MemberRole.php index 9838b92..559d516 100755 --- a/src/Data/MemberRole.php +++ b/src/Data/MemberRole.php @@ -10,7 +10,6 @@ use http\Exception\InvalidArgumentException; * encapsulates an integer value and use it as an enumeration discriminant */ final class MemberRole { - private const ROLE_PLAYER = 0; private const ROLE_COACH = 1; private const MIN = self::ROLE_PLAYER; @@ -25,11 +24,11 @@ final class MemberRole { $this->value = $val; } - public static function player(){ + public static function player(): MemberRole { return new MemberRole(MemberRole::ROLE_PLAYER); } - public static function coach(){ + public static function coach(): MemberRole { return new MemberRole(MemberRole::ROLE_COACH); } diff --git a/src/Data/Team.php b/src/Data/Team.php index a31829f..e50ad40 100755 --- a/src/Data/Team.php +++ b/src/Data/Team.php @@ -21,7 +21,7 @@ class Team { * @param Color $secondColor * @param Member[] $members */ - public function __construct(int $id,string $name, string $picture, Color $mainColor, Color $secondColor, array $members =[]) { + public function __construct(int $id, string $name, string $picture, Color $mainColor, Color $secondColor, array $members = []) { $this->id = $id; $this->name = $name; $this->picture = $picture; diff --git a/src/Gateway/TeamGateway.php b/src/Gateway/TeamGateway.php index 1cecbe1..c3d22dc 100644 --- a/src/Gateway/TeamGateway.php +++ b/src/Gateway/TeamGateway.php @@ -12,52 +12,68 @@ class TeamGateway { $this->con = $con; } - public function insert(string $name, string $picture, string $mainColor, string $secondColor) { + public function insert(string $name, string $picture, string $mainColor, string $secondColor): void { $this->con->exec( "INSERT INTO Team(name, picture, mainColor, secondColor) VALUES (:teamName , :picture, :mainColor, :secondColor)", [ ":teamName" => [$name, PDO::PARAM_STR], ":picture" => [$picture, PDO::PARAM_STR], ":mainColor" => [$mainColor, PDO::PARAM_STR], - ":secondColor" => [$secondColor, PDO::PARAM_STR] + ":secondColor" => [$secondColor, PDO::PARAM_STR], ] ); } + /** + * @param string $name + * @return array[] + */ public function listByName(string $name): array { return $this->con->fetch( "SELECT id,name,picture,mainColor,secondColor FROM Team WHERE name LIKE '%' || :name || '%'", [ - ":name" => [$name, PDO::PARAM_STR] + ":name" => [$name, PDO::PARAM_STR], ] ); } - public function getTeamById(int $id): array{ + /** + * @param int $id + * @return array[] + */ + public function getTeamById(int $id): array { return $this->con->fetch( "SELECT id,name,picture,mainColor,secondColor FROM Team WHERE id = :id", [ - ":id" => [$id, PDO::PARAM_INT] + ":id" => [$id, PDO::PARAM_INT], ] ); } - public function getIdTeamByName(string $name): array{ + /** + * @param string $name + * @return array[] + */ + public function getIdTeamByName(string $name): array { return $this->con->fetch( "SELECT id FROM Team WHERE name = :name", [ - ":name" => [$name, PDO::PARAM_STR] + ":name" => [$name, PDO::PARAM_STR], ] ); } - public function getMembersById($id):array{ + /** + * @param int $id + * @return array[] + */ + public function getMembersById(int $id): array { return $this->con->fetch( - "SELECT m.role,u.id FROM User u,Team t,Member m WHERE t.id = :id AND m.idTeam = t.id AND m.idMember = u.id", - [ - ":id" => [$id, PDO::PARAM_INT] + "SELECT m.role,u.id FROM User u,Team t,Member m WHERE t.id = :id AND m.idTeam = t.id AND m.idMember = u.id", + [ + ":id" => [$id, PDO::PARAM_INT], ] ); } -} \ No newline at end of file +} diff --git a/src/Model/TeamModel.php b/src/Model/TeamModel.php index c5521eb..b6e4ab1 100644 --- a/src/Model/TeamModel.php +++ b/src/Model/TeamModel.php @@ -8,8 +8,7 @@ use App\Data\Member; use App\Data\MemberRole; use App\Data\Color; -class TeamModel -{ +class TeamModel { private TeamGateway $gateway; /** @@ -48,4 +47,4 @@ class TeamModel } return new Team(intval($result['id']), $result['name'], $result['picture'], Color::from($result['mainColor']), Color::from($result['secondColor']), $members); } -} \ No newline at end of file +} diff --git a/src/Validation/Validators.php b/src/Validation/Validators.php index b1d1eaf..cb28007 100644 --- a/src/Validation/Validators.php +++ b/src/Validation/Validators.php @@ -56,7 +56,7 @@ class Validators { return self::regex("/^[0-9]+$/"); } - public static function isIntInRange(int $min,int $max): Validator { + public static function isIntInRange(int $min, int $max): Validator { return new SimpleFunctionValidator( fn(string $val) => intval($val) >= $min && intval($val) <= $max, fn(string $name) => [new FieldValidationFail($name, "The value is not in the range $min to $max ")] @@ -65,11 +65,8 @@ class Validators { public static function isURL(): Validator { return new SimpleFunctionValidator( - fn($val) => filter_var($val, FILTER_VALIDATE_URL) , + fn($val) => filter_var($val, FILTER_VALIDATE_URL), fn(string $name) => [new FieldValidationFail($name, "The value is not an URL")] ); } } - - - diff --git a/src/Views/display_team.html.twig b/src/Views/display_team.html.twig index 732fd19..ada8566 100644 --- a/src/Views/display_team.html.twig +++ b/src/Views/display_team.html.twig @@ -22,9 +22,15 @@ #mainColor{ background-color: {{ team.mainColor.getValue() }}; + {% if team.mainColor.getValue() == "#ffffff" %} + border-color: #666666; + {% endif %} } #secondColor{ background-color: {{ team.secondColor.getValue() }}; + {% if team.secondColor.getValue() == "#ffffff" %} + border-color: #666666; + {% endif %} } .container{ @@ -39,9 +45,6 @@ } - .colors{ - justify-content: space-between; - } .color{ flex-direction: row; justify-content: space-between; @@ -65,12 +68,10 @@

{{ team.name }}

-
+

Couleur principale :

Couleur secondaire :

- - {% for m in team.members %}

m.id

{% endfor %}