Snapshot of main at 9f39a6e1e (2026-11-02)

This commit is contained in:
Ronald Huynen committed 2026-11-02 10:41:47 +01:00
commit 6ccf1792d7
2580 files changed
+1040202

No files matched your search

+68
View File
@@ -0,0 +1,68 @@
<?php
namespace App\Polls;
use App\Polls\Contracts\QuestionType;
use InvalidArgumentException;
/**
* The question types enabled for this platform, read from 'polls.question_types'.
* Without that key (a platform config that has not merged the polls section yet)
* all built-in types are enabled; an empty list disables them on purpose.
*/
class QuestionTypeRegistry
{
public const DEFAULT_TYPES = [
'yes_no' => Types\YesNoType::class,
'yes_no_conditional' => Types\YesNoConditionalType::class,
'single_choice' => Types\SingleChoiceType::class,
'multi_choice' => Types\MultiChoiceType::class,
'date_time_slots' => Types\DateTimeSlotsType::class,
'scale' => Types\ScaleType::class,
'text' => Types\TextType::class,
'ranking' => Types\RankingType::class,
];
/** @var array<string, QuestionType>|null */
private ?array $types = null;
/** @return array<string, QuestionType> keyed by type key */
public function all(): array
{
if ($this->types === null) {
$this->types = [];
foreach (timebank_config('polls.question_types') ?? self::DEFAULT_TYPES as $key => $class) {
$type = app($class);
if (!$type instanceof QuestionType) {
throw new InvalidArgumentException("Poll question type [$key] must implement " . QuestionType::class);
}
if ($type->key() !== $key) {
throw new InvalidArgumentException("Poll question type [$key] reports key [{$type->key()}]");
}
$this->types[$key] = $type;
}
}
return $this->types;
}
public function has(string $key): bool
{
return isset($this->all()[$key]);
}
public function get(string $key): QuestionType
{
if (!$this->has($key)) {
throw new InvalidArgumentException("Unknown or disabled poll question type [$key]");
}
return $this->all()[$key];
}
/** @return array<string, string> key => translated label, for the editor's type select */
public function options(): array
{
return array_map(fn (QuestionType $type) => $type->label(), $this->all());
}
}