This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/app/lib/App/Timetable/TimeMapper.php

94 lines
2.3 KiB
PHP
Raw Permalink Normal View History

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
{
2022-03-20 20:36:35 +00:00
$time= new Time();
$time->id = $row['TidID'];
$time->setTeamId($row['LagID']);
$time->setDate(new DateTime($row['Tidspunkt']));
return $time;
}
public function getAll(): array
{
$sth = $this->dbh->query('SELECT * FROM tidtabell');
$assoc_array = $sth->fetchAll(PDO::FETCH_ASSOC);
$times = [];
foreach ($assoc_array as $key => $row)
{
array_push($times, $this->mapRowToTime($row));
}
return $times;
2022-03-13 19:54:34 +00:00
}
2022-03-21 10:39:50 +00:00
public function getLatest(): ?Time
{
$sth = $this->dbh->prepare('SELECT * FROM tidtabell ORDER BY Tidspunkt DESC LIMIT 1');
$sth->execute();
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row)
{
return $this->mapRowToTime($row);
}
return NULL;
}
2022-04-14 20:59:42 +00:00
public function getLatestByTeamId(int $team_id): ?Time
2022-03-14 10:36:45 +00:00
{
2022-03-21 10:39:50 +00:00
$sth = $this->dbh->prepare('SELECT * FROM tidtabell WHERE LagID = ? ORDER BY Tidspunkt DESC LIMIT 1');
2022-04-14 20:59:42 +00:00
$sth->execute([$team_id]);
2022-03-14 10:36:45 +00:00
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row)
{
return $this->mapRowToTime($row);
}
return NULL;
}
public function get(int $id): ?Time
{
2022-03-21 10:39:50 +00:00
$sth = $this->dbh->prepare('SELECT * FROM tidtabell WHERE TidID = ? LIMIT 1');
2022-03-14 10:36:45 +00:00
$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 (?)');
2022-04-14 20:59:42 +00:00
$sth->execute([$time->team_id]);
2022-04-20 09:02:19 +00:00
$last_id = $this->dbh->lastInsertId();
return $this->get($last_id);
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-04-20 18:42:19 +00:00
public function deleteByTeamId(int $team_id): void
{
$sth = $this->dbh->prepare('DELETE FROM tidtabell WHERE LagID = ?');
$sth->execute([$team_id]);
}
2022-03-13 19:54:34 +00:00
}