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-03-14 10:36:45 +00:00
|
|
|
$team->bestTime = $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
|
|
|
|
{
|
|
|
|
$sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE Kortnummer = ?');
|
|
|
|
$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
|
|
|
|
{
|
|
|
|
$sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE LagID = ?');
|
|
|
|
$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,
|
|
|
|
$team->bestTime
|
2022-03-07 06:13:17 +00:00
|
|
|
]);
|
|
|
|
$lastId = $this->dbh->lastInsertId();
|
|
|
|
return $this->get($lastId);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-14 10:36:45 +00:00
|
|
|
$team->bestTime,
|
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]);
|
|
|
|
}
|
|
|
|
}
|