codeweek/demo/oop/Frage.php
2024-10-12 13:52:06 +02:00

38 lines
836 B
PHP

<?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;
}
}