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.
83 lines
3.2 KiB
83 lines
3.2 KiB
<?php
|
|
|
|
namespace Stub;
|
|
|
|
use Model\Athlete;
|
|
use Model\Coach;
|
|
use Model\User;
|
|
use Repository\IUserRepository;
|
|
|
|
class UserRepository implements IUserRepository {
|
|
private array $users = [];
|
|
|
|
public function __construct() {
|
|
$this->users[] = new User(1, "Doe", "John", "john.doe@example.com", "password123", 'M', 1.80, 75, new \DateTime("1985-05-15"), new Coach());
|
|
$this->users[] = new User(2, "Smith", "Jane", "jane.smith@example.com", "secure456", 'F', 1.65, 60, new \DateTime("1990-03-10"), new Athlete());
|
|
$this->users[] = new User(3, "Martin", "Paul", "paul.martin@example.com", "super789", 'M', 1.75, 68, new \DateTime("1988-08-20"), new Coach());
|
|
$this->users[] = new User(4, "Brown", "Anna", "anna.brown@example.com", "test000", 'F', 1.70, 58, new \DateTime("1992-11-25"), new Athlete());
|
|
$this->users[] = new User(5, "Lee", "Bruce", "bruce.lee@example.com", "hello321", 'M', 1.72, 70, new \DateTime("1970-02-05"), new Athlete());
|
|
}
|
|
|
|
public function getItemById(int $id): ?User {
|
|
foreach ($this->users as $user) {
|
|
if ($user->getId() === $id) {
|
|
return $user;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function getItemByEmail(string $email): ?User {
|
|
foreach ($this->users as $user) {
|
|
if ($user->getEmail() === $email) {
|
|
return $user;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function GetNbItems(): int {
|
|
return count($this->users);
|
|
}
|
|
|
|
public function GetItems(int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array {
|
|
// Cette méthode est un exemple simple, on ne gère pas l'ordonnancement ici
|
|
return array_slice($this->users, $index, $count);
|
|
}
|
|
|
|
public function GetItemsByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array {
|
|
$filteredUsers = array_filter($this->users, function ($user) use ($substring) {
|
|
return strpos(strtolower($user->getNom()), strtolower($substring)) !== false || strpos(strtolower($user->getPrenom()), strtolower($substring)) !== false;
|
|
});
|
|
return array_slice($filteredUsers, $index, $count);
|
|
}
|
|
public function GetItemByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): ?User {
|
|
$filteredUsers = $this->GetItemsByName($substring, $index, $count, $orderingPropertyName, $descending);
|
|
return isset($filteredUsers[0]) ? $filteredUsers[0] : null;
|
|
}
|
|
|
|
// should have type here
|
|
public function UpdateItem($oldUser, $newUser): void {
|
|
$index = array_search($oldUser, $this->users);
|
|
if ($index !== false) {
|
|
$this->users[$index] = $newUser;
|
|
}
|
|
}
|
|
|
|
// should have type here
|
|
public function AddItem( $user): void {
|
|
$this->users[] = $user;
|
|
}
|
|
|
|
// should have type here
|
|
public function DeleteItem( $user): bool {
|
|
$index = array_search($user, $this->users);
|
|
if ($index !== false) {
|
|
unset($this->users[$index]);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
?>
|