57 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			1.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
 | |
|     {
 | |
|         $team = new Time();
 | |
|         $team->id = $row['TidID'];
 | |
|         $team->setTeamId($row['LagID']);
 | |
|         $team->setDate(new DateTime($row['Tidspunkt']));
 | |
|         return $team;
 | |
|     }
 | |
| 
 | |
|     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;
 | |
|     }
 | |
| 
 | |
|     public function create(Time $time): Time 
 | |
|     {
 | |
|         $sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)');
 | |
|         $sth->execute([$time->teamId]);
 | |
|         $lastId = $this->dbh->lastInsertId();
 | |
|         return $this->get($lastId);
 | |
|     }
 | |
| } |