From 7ef39298c42fd994157eaa907d76596d54d46a92 Mon Sep 17 00:00:00 2001 From: Override-6 Date: Wed, 25 Oct 2023 19:03:36 +0200 Subject: [PATCH 1/3] add autoloader, router, and data components --- .gitignore | 3 ++ Documentation/data.puml | 41 ++++++++--------- README.md | 1 + composer.json | 10 ++++ index.php | 36 +++++++++------ src/Controller/HelloPageController.php | 10 ++++ src/Data/Account.php | 61 +++++++++++++++++++++++++ src/Data/AccountUser.php | 59 ++++++++++++++++++++++++ src/Data/Color.php | 30 ++++++++++++ src/Data/Member.php | 32 +++++++++++++ src/Data/MemberRole.php | 35 ++++++++++++++ src/Data/Team.php | 63 ++++++++++++++++++++++++++ src/Data/User.php | 13 ++++++ src/View/hello.html | 16 +++++++ 14 files changed, 374 insertions(+), 36 deletions(-) mode change 100644 => 100755 Documentation/data.puml create mode 100644 composer.json create mode 100755 src/Controller/HelloPageController.php create mode 100755 src/Data/Account.php create mode 100755 src/Data/AccountUser.php create mode 100755 src/Data/Color.php create mode 100755 src/Data/Member.php create mode 100755 src/Data/MemberRole.php create mode 100755 src/Data/Team.php create mode 100755 src/Data/User.php create mode 100755 src/View/hello.html diff --git a/.gitignore b/.gitignore index 485dee6..463794e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ .idea +vendor +composer.lock +*.phar diff --git a/Documentation/data.puml b/Documentation/data.puml old mode 100644 new mode 100755 index d9cc283..038a11a --- a/Documentation/data.puml +++ b/Documentation/data.puml @@ -16,13 +16,13 @@ Account --> "- teams *" Team interface User { + getName(): String - + getProfilePicture(): URI + + getProfilePicture(): Url + getAge(): int } class AccountUser { - name: String - - profilePicture: URI + - profilePicture: Url - age: int + setName(String) @@ -31,39 +31,38 @@ class AccountUser { } AccountUser ..|> User - -abstract class Member { +class Member { getUser(): User + getRole(): MemberRole } Member --> "- user" User +Member --> "- role" MemberRole -class Coach { - 'todo -} - -class Player { - 'todo +enum MemberRole { + PLAYER + COACH } -Player --|> Member -Coach --|> Member - class Team { - name: String - - picture: URI - - mainColor: Color - - secondColor: Color + - picture: Url + - members: array + getName(): String - + getPicture(): URI + + getPicture(): Url + getMainColor(): Color + getSecondColor(): Color - + getCoachs(): array - + getPlayers(): array + + listMembers(): array } -Team --> "- players *" Player -Team --> "- coachs *" Coach +Team --> "- mainColor" Color +Team --> "- secondaryColor" Color + +class Color { + value: int + + getValue(): int +} @enduml diff --git a/README.md b/README.md index 50c3e73..d2edce6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # IQBall - Web Application This repository hosts the IQBall application for web + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d022007 --- /dev/null +++ b/composer.json @@ -0,0 +1,10 @@ +{ + "autoload": { + "psr-4": { + "App\\": "src/" + } + }, + "require": { + "altorouter/altorouter": "1.2.0" + } +} \ No newline at end of file diff --git a/index.php b/index.php index 228703a..0fbd9f7 100644 --- a/index.php +++ b/index.php @@ -1,16 +1,22 @@ - - - - - - - Document - - -

Hello world

hello world" -?> - - \ No newline at end of file + +global $router; +require "./vendor/autoload.php"; + +use App\Controller\HelloPageController; + +// routes initialization +$router = new AltoRouter(); +// hello page controllers +$helloController = new HelloPageController(); +$router->map("GET", "/", fn() => $helloController->display()); + +$match = $router->match(); + +if ($match == null) { + // TODO redirect to a 404 not found page instead (issue #1) + header('HTTP/1.1 404 Not Found'); + exit(1); +} + +call_user_func($match['target']); diff --git a/src/Controller/HelloPageController.php b/src/Controller/HelloPageController.php new file mode 100755 index 0000000..1498afe --- /dev/null +++ b/src/Controller/HelloPageController.php @@ -0,0 +1,10 @@ +email = $email; + $this->phoneNumber = $phoneNumber; + $this->user = $user; + $this->teams = $teams; + } + + + /** + * @return string + */ + public function getEmail(): string { + return $this->email; + } + + /** + * @param string $email + */ + public function setEmail(string $email): void { + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + throw new InvalidArgumentException("Invalid mail address"); + } + $this->email = $email; + } + + /** + * @return string + */ + public function getPhoneNumber(): string { + return $this->phoneNumber; + } + + /** + * @param string $phoneNumber + */ + public function setPhoneNumber(string $phoneNumber): void { + if (!filter_var($phoneNumber, FILTER_VALIDATE_REGEXP, "\\+[0-9]+")) { + throw new InvalidArgumentException("Invalid phone number"); + } + $this->phoneNumber = $phoneNumber; + } + + +} \ No newline at end of file diff --git a/src/Data/AccountUser.php b/src/Data/AccountUser.php new file mode 100755 index 0000000..feed910 --- /dev/null +++ b/src/Data/AccountUser.php @@ -0,0 +1,59 @@ +name = $name; + $this->profilePicture = $profilePicture; + $this->age = $age; + $this->teams = $teams; + } + + + public function getName(): string { + return $this->name; + } + + public function getProfilePicture(): Url { + return $this->profilePicture; + } + + public function getAge(): int { + return $this->age; + } + + public function setName(string $name) { + $this->name = $name; + } + + public function setProfilePicture(Url $profilePicture) { + $this->profilePicture = $profilePicture; + } + + public function setAge(int $age) { + $this->age = $age; + } + + public function getTeams(): array { + return $this->teams; + } + + public function getId(): int { + return $this->id; + } + +} \ No newline at end of file diff --git a/src/Data/Color.php b/src/Data/Color.php new file mode 100755 index 0000000..f841731 --- /dev/null +++ b/src/Data/Color.php @@ -0,0 +1,30 @@ + 0xFFFFFF) { + throw new InvalidArgumentException("int color value is invalid, must be positive and lower than 0xFFFFFF"); + } + $this->value = $value; + } + + /** + * @return int + */ + public function getValue(): int { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Data/Member.php b/src/Data/Member.php new file mode 100755 index 0000000..b2844c3 --- /dev/null +++ b/src/Data/Member.php @@ -0,0 +1,32 @@ +userId = $userId; + $this->role = $role; + } + + /** + * @return int + */ + public function getUserId(): int { + return $this->userId; + } + + /** + * @return int + */ + public function getRole(): int { + return $this->role; + } + +} \ No newline at end of file diff --git a/src/Data/MemberRole.php b/src/Data/MemberRole.php new file mode 100755 index 0000000..7fc09d5 --- /dev/null +++ b/src/Data/MemberRole.php @@ -0,0 +1,35 @@ +isValid($val)) { + throw new InvalidArgumentException("Valeur du rĂ´le invalide"); + } + $this->value = $val; + } + + private function isValid(int $val): bool { + return ($val <= self::MAX and $val >= self::MIN); + } + + public function isPlayer(): bool { + return ($this->value == self::ROLE_PLAYER); + } + + public function isCoach(): bool { + return ($this->value == self::ROLE_COACH); + } + +} \ No newline at end of file diff --git a/src/Data/Team.php b/src/Data/Team.php new file mode 100755 index 0000000..409552a --- /dev/null +++ b/src/Data/Team.php @@ -0,0 +1,63 @@ +name = $name; + $this->picture = $picture; + $this->mainColor = $mainColor; + $this->secondColor = $secondColor; + $this->members = $members; + } + + /** + * @return string + */ + public function getName(): string { + return $this->name; + } + + /** + * @return Url + */ + public function getPicture(): Url { + return $this->picture; + } + + /** + * @return Color + */ + public function getMainColor(): Color { + return $this->mainColor; + } + + /** + * @return Color + */ + public function getSecondColor(): Color { + return $this->secondColor; + } + + public function listMembers(): array { + return array_map(fn ($id, $role) => new Member($id, $role), $this->members); + } + +} \ No newline at end of file diff --git a/src/Data/User.php b/src/Data/User.php new file mode 100755 index 0000000..dfb3034 --- /dev/null +++ b/src/Data/User.php @@ -0,0 +1,13 @@ + + + + + + + Document + + + +

Hello

+

World

+ + + \ No newline at end of file -- 2.36.3 From 9ce609acab0cee1c023e51cee1ac1cff6d96fda0 Mon Sep 17 00:00:00 2001 From: Override-6 Date: Wed, 25 Oct 2023 19:26:52 +0200 Subject: [PATCH 2/3] add documentation --- Documentation/data.puml | 13 +++++++----- src/Data/Account.php | 44 +++++++++++++++++++++++++++++++++++++--- src/Data/AccountUser.php | 8 +++----- src/Data/Member.php | 20 +++++++++++++----- src/Data/MemberRole.php | 5 +++++ src/Data/Team.php | 2 ++ src/Data/User.php | 14 +++++++++++++ 7 files changed, 88 insertions(+), 18 deletions(-) diff --git a/Documentation/data.puml b/Documentation/data.puml index 038a11a..90a7929 100755 --- a/Documentation/data.puml +++ b/Documentation/data.puml @@ -3,12 +3,14 @@ class Account { - email: String - phoneNumber: String + - id: int + setMailAddress(String) + getMailAddress(): String + getPhoneNumber(): String + setPhoneNumber(String) + getUser(): AccountUser + + getId(): int } Account --> "- user" AccountUser @@ -32,11 +34,12 @@ class AccountUser { AccountUser ..|> User class Member { - getUser(): User - getRole(): MemberRole + - userId: int + + + getUserId(): int + + getRole(): MemberRole } -Member --> "- user" User Member --> "- role" MemberRole enum MemberRole { @@ -60,9 +63,9 @@ Team --> "- mainColor" Color Team --> "- secondaryColor" Color class Color { - value: int + - value: int - getValue(): int + + getValue(): int } @enduml diff --git a/src/Data/Account.php b/src/Data/Account.php index bc82808..155f2ae 100755 --- a/src/Data/Account.php +++ b/src/Data/Account.php @@ -4,25 +4,53 @@ namespace App\Data; use http\Exception\InvalidArgumentException; +const PHONE_NUMBER_REGEXP = "\\+[0-9]+"; + +/** + * Base class of a user account. + * Contains the private information that we don't want + * to share to other users, or non-needed public information + */ class Account { + /** + * @var string $email account's mail address + */ private string $email; + /** + * @var string account's phone number. + * its format is specified by the {@link PHONE_NUMBER_REGEXP} constant + * + */ private string $phoneNumber; + + /** + * @var AccountUser account's public and shared information + */ private AccountUser $user; + + /** + * @var array account's teams + */ private array $teams; + /** + * @var int account's unique identifier + */ + private int $id; + /** * @param string $email * @param string $phoneNumber * @param AccountUser $user */ - public function __construct(string $email, string $phoneNumber, AccountUser $user, array $teams) { + public function __construct(string $email, string $phoneNumber, AccountUser $user, array $teams, int $id) { $this->email = $email; $this->phoneNumber = $phoneNumber; $this->user = $user; $this->teams = $teams; + $this->id = $id; } - /** * @return string */ @@ -51,11 +79,21 @@ class Account { * @param string $phoneNumber */ public function setPhoneNumber(string $phoneNumber): void { - if (!filter_var($phoneNumber, FILTER_VALIDATE_REGEXP, "\\+[0-9]+")) { + if (!filter_var($phoneNumber, FILTER_VALIDATE_REGEXP, PHONE_NUMBER_REGEXP)) { throw new InvalidArgumentException("Invalid phone number"); } $this->phoneNumber = $phoneNumber; } + public function getId(): int { + return $this->id; + } + + public function getTeams(): array { + return $this->teams; + } + public function getUser(): AccountUser { + return $this->user; + } } \ No newline at end of file diff --git a/src/Data/AccountUser.php b/src/Data/AccountUser.php index feed910..a825900 100755 --- a/src/Data/AccountUser.php +++ b/src/Data/AccountUser.php @@ -4,12 +4,14 @@ namespace App\Data; use http\Url; +/** + * This class implements the User and + */ class AccountUser implements User { private string $name; private Url $profilePicture; private int $age; private array $teams; - private int $id; /** * @param string $name @@ -52,8 +54,4 @@ class AccountUser implements User { return $this->teams; } - public function getId(): int { - return $this->id; - } - } \ No newline at end of file diff --git a/src/Data/Member.php b/src/Data/Member.php index b2844c3..91b09c4 100755 --- a/src/Data/Member.php +++ b/src/Data/Member.php @@ -2,15 +2,25 @@ namespace App\Data; +/** + * information about a team member + */ class Member { + /** + * @var int The member's user id + */ private int $userId; - private int $role; + + /** + * @var MemberRole the member's role + */ + private MemberRole $role; /** * @param int $userId - * @param int $role + * @param MemberRole $role */ - public function __construct(int $userId, int $role) { + public function __construct(int $userId, MemberRole $role) { $this->userId = $userId; $this->role = $role; } @@ -23,9 +33,9 @@ class Member { } /** - * @return int + * @return MemberRole */ - public function getRole(): int { + public function getRole(): MemberRole { return $this->role; } diff --git a/src/Data/MemberRole.php b/src/Data/MemberRole.php index 7fc09d5..05d746d 100755 --- a/src/Data/MemberRole.php +++ b/src/Data/MemberRole.php @@ -5,6 +5,11 @@ namespace App\Data; use http\Exception\InvalidArgumentException; +/** + * Enumeration class workaround + * As there is no enumerations in php 7.4, this class + * encapsulates an integer value and use it as an enumeration discriminant + */ final class MemberRole { private const ROLE_PLAYER = 0; private const ROLE_COACH = 1; diff --git a/src/Data/Team.php b/src/Data/Team.php index 409552a..48643d9 100755 --- a/src/Data/Team.php +++ b/src/Data/Team.php @@ -2,6 +2,8 @@ namespace App\Data; +use http\Url; + class Team { private string $name; private Url $picture; diff --git a/src/Data/User.php b/src/Data/User.php index dfb3034..15c9995 100755 --- a/src/Data/User.php +++ b/src/Data/User.php @@ -4,10 +4,24 @@ namespace App\Data; use http\Url; + +/** + * Public information about a user + */ interface User { + /** + * @return string the user's name + */ public function getName(): string; + /** + * @return Url The user's profile picture image URL + */ + public function getProfilePicture(): Url; + /** + * @return int The user's age + */ public function getAge(): int; } \ No newline at end of file -- 2.36.3 From 5d1d8076855205d217d0490ff6820f5bc1663188 Mon Sep 17 00:00:00 2001 From: "maxime.batista" Date: Thu, 26 Oct 2023 11:29:13 +0200 Subject: [PATCH 3/3] apply suggestions --- src/Data/AccountUser.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Data/AccountUser.php b/src/Data/AccountUser.php index a825900..7808062 100755 --- a/src/Data/AccountUser.php +++ b/src/Data/AccountUser.php @@ -11,18 +11,16 @@ class AccountUser implements User { private string $name; private Url $profilePicture; private int $age; - private array $teams; /** * @param string $name * @param Url $profilePicture * @param int $age */ - public function __construct(string $name, Url $profilePicture, int $age, array $teams) { + public function __construct(string $name, Url $profilePicture, int $age) { $this->name = $name; $this->profilePicture = $profilePicture; $this->age = $age; - $this->teams = $teams; } @@ -50,8 +48,5 @@ class AccountUser implements User { $this->age = $age; } - public function getTeams(): array { - return $this->teams; - } } \ No newline at end of file -- 2.36.3