94 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Timetable;
 | 
						|
 | 
						|
use \PDO;
 | 
						|
use \DateTime;
 | 
						|
 | 
						|
class TimeMapper
 | 
						|
{
 | 
						|
    public PDO $dbh;
 | 
						|
 | 
						|
    public function __construct(PDO $dbh)
 | 
						|
    {
 | 
						|
        $this->dbh = $dbh;
 | 
						|
    }
 | 
						|
 | 
						|
    private function mapRowToTime(array $row): Time
 | 
						|
    {
 | 
						|
        $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;
 | 
						|
    }
 | 
						|
 | 
						|
    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;    
 | 
						|
    }
 | 
						|
 | 
						|
    public function getLatestByTeamId(int $team_id): ?Time
 | 
						|
    {
 | 
						|
        $sth = $this->dbh->prepare('SELECT * FROM tidtabell WHERE LagID = ? ORDER BY Tidspunkt DESC LIMIT 1');
 | 
						|
        $sth->execute([$team_id]);
 | 
						|
        $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 = ? LIMIT 1');
 | 
						|
        $sth->execute([$id]);
 | 
						|
        $row = $sth->fetch(PDO::FETCH_ASSOC);
 | 
						|
        if ($row)
 | 
						|
        {
 | 
						|
            return $this->mapRowToTime($row);
 | 
						|
        }
 | 
						|
        return NULL;
 | 
						|
    }
 | 
						|
 | 
						|
    public function create(Time $time): Time 
 | 
						|
    {
 | 
						|
        $sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)');
 | 
						|
        $sth->execute([$time->team_id]);
 | 
						|
        $last_id = $this->dbh->lastInsertId();
 | 
						|
        return $this->get($last_id);
 | 
						|
    }
 | 
						|
 | 
						|
    public function delete(int $id): void
 | 
						|
    {
 | 
						|
        $sth = $this->dbh->prepare('DELETE FROM tidtabell WHERE TidID = ?');
 | 
						|
        $sth->execute([$id]); 
 | 
						|
    }
 | 
						|
 | 
						|
    public function deleteByTeamId(int $team_id): void
 | 
						|
    {
 | 
						|
        $sth = $this->dbh->prepare('DELETE FROM tidtabell WHERE LagID = ?');
 | 
						|
        $sth->execute([$team_id]);
 | 
						|
    }
 | 
						|
} |