add autoloader, router, and data components

pull/2/head
Override-6 2 years ago
parent 22840a037c
commit 7ef39298c4
Signed by untrusted user who does not match committer: maxime.batista
GPG Key ID: 8002CC4B4DD9ECA5

3
.gitignore vendored

@ -1 +1,4 @@
.idea
vendor
composer.lock
*.phar

@ -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<int, MemberRole>
+ getName(): String
+ getPicture(): URI
+ getPicture(): Url
+ getMainColor(): Color
+ getSecondColor(): Color
+ getCoachs(): array
+ getPlayers(): array
+ listMembers(): array<Member>
}
Team --> "- players *" Player
Team --> "- coachs *" Coach
Team --> "- mainColor" Color
Team --> "- secondaryColor" Color
class Color {
value: int
getValue(): int
}
@enduml

@ -1,2 +1,3 @@
# IQBall - Web Application
This repository hosts the IQBall application for web

@ -0,0 +1,10 @@
{
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"require": {
"altorouter/altorouter": "1.2.0"
}
}

@ -1,16 +1,22 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Hello world</h1>
<?php
echo "<h2>hello world</h2>"
?>
</body>
</html>
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']);

@ -0,0 +1,10 @@
<?php
namespace App\Controller;
class HelloPageController {
public function display() {
require_once "src/View/hello.html";
}
}

@ -0,0 +1,61 @@
<?php
namespace App\Data;
use http\Exception\InvalidArgumentException;
class Account {
private string $email;
private string $phoneNumber;
private AccountUser $user;
private array $teams;
/**
* @param string $email
* @param string $phoneNumber
* @param AccountUser $user
*/
public function __construct(string $email, string $phoneNumber, AccountUser $user, array $teams) {
$this->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;
}
}

@ -0,0 +1,59 @@
<?php
namespace App\Data;
use http\Url;
class AccountUser implements User {
private string $name;
private Url $profilePicture;
private int $age;
private array $teams;
private int $id;
/**
* @param string $name
* @param Url $profilePicture
* @param int $age
*/
public function __construct(string $name, Url $profilePicture, int $age, array $teams) {
$this->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;
}
}

@ -0,0 +1,30 @@
<?php
namespace App\Data;
use http\Exception\InvalidArgumentException;
class Color {
/**
* @var int 6 bytes unsigned int that represents an RGB color
*/
private int $value;
/**
* @param int $value 6 bytes unsigned int that represents an RGB color
* @throws \InvalidArgumentException if the value is negative or greater than 0xFFFFFF
*/
public function __constructor(int $value) {
if ($value < 0 || $value > 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;
}
}

@ -0,0 +1,32 @@
<?php
namespace App\Data;
class Member {
private int $userId;
private int $role;
/**
* @param int $userId
* @param int $role
*/
public function __construct(int $userId, int $role) {
$this->userId = $userId;
$this->role = $role;
}
/**
* @return int
*/
public function getUserId(): int {
return $this->userId;
}
/**
* @return int
*/
public function getRole(): int {
return $this->role;
}
}

@ -0,0 +1,35 @@
<?php
namespace App\Data;
use http\Exception\InvalidArgumentException;
final class MemberRole {
private const ROLE_PLAYER = 0;
private const ROLE_COACH = 1;
private const MIN = self::ROLE_PLAYER;
private const MAX = self::ROLE_COACH;
private int $value;
private function __construct(int $val) {
if (!$this->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);
}
}

@ -0,0 +1,63 @@
<?php
namespace App\Data;
class Team {
private string $name;
private Url $picture;
private Color $mainColor;
private Color $secondColor;
/**
* @var array maps users with their role
*/
private array $members;
/**
* @param string $name
* @param Url $picture
* @param Color $mainColor
* @param Color $secondColor
* @param array $members
*/
public function __construct(string $name, Url $picture, Color $mainColor, Color $secondColor, array $members) {
$this->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);
}
}

@ -0,0 +1,13 @@
<?php
namespace App\Data;
use http\Url;
interface User {
public function getName(): string;
public function getProfilePicture(): Url;
public function getAge(): int;
}

@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Hello</h1>
<h3>World</h3>
</body>
</html>
Loading…
Cancel
Save