2022-03-13 19:54:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Timetable;
|
|
|
|
|
|
|
|
use \PDO;
|
2022-03-14 10:36:45 +00:00
|
|
|
use \DateTime;
|
2022-03-13 19:54:34 +00:00
|
|
|
|
|
|
|
class TimeMapper
|
|
|
|
{
|
|
|
|
public PDO $dbh;
|
|
|
|
|
|
|
|
public function __construct(PDO $dbh)
|
|
|
|
{
|
|
|
|
$this->dbh = $dbh;
|
|
|
|
}
|
|
|
|
|
2022-03-14 10:36:45 +00:00
|
|
|
private function mapRowToTime(array $row): Time
|
2022-03-13 19:54:34 +00:00
|
|
|
{
|
|
|
|
$team = new Time();
|
2022-03-14 10:36:45 +00:00
|
|
|
$team->id = $row['TidID'];
|
|
|
|
$team->setTeamId($row['LagID']);
|
|
|
|
$team->setDate(new DateTime($row['Tidspunkt']));
|
2022-03-13 19:54:34 +00:00
|
|
|
return $team;
|
|
|
|
}
|
|
|
|
|
2022-03-14 10:36:45 +00:00
|
|
|
public function getLatestByTeamId(int $teamId): ?Time
|
|
|
|
{
|
|
|
|
$sth = $this->dbh->prepare('SELECT * FROM tidtabell WHERE LagID = ? ORDER BY Tidspunkt DESC');
|
|
|
|
$sth->execute([$teamId]);
|
|
|
|
$row = $sth->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($row)
|
|
|
|
{
|
|
|
|
return $this->mapRowToTime($row);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get(int $id): ?Time
|
|
|
|
{
|
|
|
|
$sth = $this->dbh->prepare('SELECT * FROM tidtabell WHERE TidID = ?');
|
|
|
|
$sth->execute([$id]);
|
|
|
|
$row = $sth->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($row)
|
|
|
|
{
|
|
|
|
return $this->mapRowToTime($row);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-13 19:54:34 +00:00
|
|
|
public function create(Time $time): Time
|
|
|
|
{
|
|
|
|
$sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)');
|
|
|
|
$sth->execute([$time->teamId]);
|
2022-03-14 10:36:45 +00:00
|
|
|
$lastId = $this->dbh->lastInsertId();
|
|
|
|
return $this->get($lastId);
|
2022-03-13 19:54:34 +00:00
|
|
|
}
|
2022-03-16 12:37:24 +00:00
|
|
|
|
|
|
|
public function delete(int $id): void
|
|
|
|
{
|
|
|
|
$sth = $this->dbh->prepare('DELETE FROM tidtabell WHERE TidID = ?');
|
|
|
|
$sth->execute([$id]);
|
|
|
|
}
|
2022-03-13 19:54:34 +00:00
|
|
|
}
|