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/Teamtable/TeamMapper.php

133 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2022-03-07 06:13:17 +00:00
<?php
namespace App\Teamtable;
use \PDO;
/**
* Normally this kind of logic will be implemented using the Repository pattern.
* However the important part is in mapRowToTeam(), that will create a business object from the
* data fetched from database
*/
class TeamMapper
{
public PDO $dbh;
public function __construct(PDO $dbh)
{
$this->dbh = $dbh;
}
private function mapRowToTeam(array $row): Team
{
$team = new Team();
$team->id = $row['LagID'];
$team->setName($row['LagNavn']);
$team->setCompany($row['Bedrift']);
$team->setCardnumber($row['Kortnummer']);
$team->setLeader($row['Lagleder']);
$team->setPhone($row['Telefon']);
$team->setParticipants($row['Deltagere']);
$team->setRounds($row['Runder']);
2022-04-14 20:59:42 +00:00
$team->best_time = $row['Bestetid'];
2022-03-07 06:13:17 +00:00
return $team;
}
/**
* Returns an array of all teams
*/
public function getAll(): array
{
$sth = $this->dbh->query('SELECT * FROM lagtabell');
$assoc_array = $sth->fetchAll(PDO::FETCH_ASSOC);
$teams = [];
foreach ($assoc_array as $key => $row)
{
array_push($teams, $this->mapRowToTeam($row));
}
return $teams;
#while ($assoc_array)
#{
# array_push($teams, $this->mapRowToTeam($assoc_array[0]));
# array_pop($assoc_array);
#}
#return $teams;
}
2022-03-07 10:11:33 +00:00
public function getByCardnumber(string $cardnumber): ?Team
{
2022-03-21 10:39:25 +00:00
$sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE Kortnummer = ? LIMIT 1');
2022-03-07 10:11:33 +00:00
$sth->execute([$cardnumber]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row)
{
return $this->mapRowToTeam($row);
}
return NULL;
}
2022-03-07 06:13:17 +00:00
public function get(int $id): ?Team
{
2022-03-21 10:39:25 +00:00
$sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE LagID = ? LIMIT 1');
2022-03-07 06:13:17 +00:00
$sth->execute([$id]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row)
{
return $this->mapRowToTeam($row);
}
return NULL;
}
public function create(Team $team): Team
{
$sth = $this->dbh->prepare(
'INSERT INTO lagtabell
2022-03-14 10:36:45 +00:00
(LagNavn, Bedrift, Kortnummer, Lagleder, Telefon, Deltagere, Runder, Bestetid)
2022-03-07 06:13:17 +00:00
VALUES
2022-03-14 10:36:45 +00:00
(?, ?, ?, ?, ?, ?, ?, ?)'
2022-03-07 06:13:17 +00:00
);
$sth->execute([
$team->name,
$team->company,
$team->cardnumber,
$team->leader,
$team->phone,
$team->participants,
2022-03-14 10:36:45 +00:00
$team->rounds,
2022-04-14 20:59:42 +00:00
$team->best_time
2022-03-07 06:13:17 +00:00
]);
2022-05-30 17:43:00 +00:00
$last_id = $this->dbh->lastInsertId();
return $this->get($last_id);
2022-03-07 06:13:17 +00:00
}
public function update(Team $team): Team
{
$sth = $this->dbh->prepare(
'UPDATE lagtabell SET
LagNavn = ?, Bedrift = ?, Kortnummer = ?,
Lagleder = ?, Telefon = ?, Deltagere = ?,
2022-03-14 10:36:45 +00:00
Runder = ?, Bestetid = ?
2022-03-07 06:13:17 +00:00
WHERE
LagID = ?'
);
$sth->execute([
$team->name,
$team->company,
$team->cardnumber,
$team->leader,
$team->phone,
$team->participants,
$team->rounds,
2022-04-14 20:59:42 +00:00
$team->best_time,
2022-03-07 06:13:17 +00:00
$team->id
]);
return $this->get($team->id);
}
public function delete(int $id): void
{
$sth = $this->dbh->prepare('DELETE FROM lagtabell WHERE LagID = ?');
$sth->execute([$id]);
}
}