This commit is contained in:
csimonis 2024-10-12 13:45:04 +02:00
commit 9ffda1656d
4 changed files with 69 additions and 0 deletions

37
demo/oop/Frage.php Normal file
View file

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
readonly class Frage implements Stringable
{
public function __construct(
private string $frage,
private string $richtigeAntwort,
private array $antworten,
private string $frageNr,
) {
}
public function getFrageNr(): string
{
return $this->frageNr;
}
public function istRichtig(string $antwort): bool
{
return $antwort === $this->richtigeAntwort;
}
public function __toString(): string
{
$fragen = '';
foreach ($this->antworten as $antwort) {
$fragen .= '<br>';
$fragen .= sprintf('<input type="radio" name="%s" value="%s">%s</input>', $this->frageNr, $antwort, $antwort);
}
$fragen .= '<hr>';
return $this->frage . '<br>' . $fragen;
}
}