61 lines
1.6 KiB
PHP
61 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Mummert\SurveyBundle\QuestionType;
|
|
|
|
use Mummert\SurveyBundle\Model\SurveyContentModel;
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
use Symfony\Component\Validator\Constraints\Choice;
|
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
|
|
|
final class YesNoMaybeQuestionType implements QuestionTypeInterface
|
|
{
|
|
public function getName(): string
|
|
{
|
|
return 'yes_no_maybe';
|
|
}
|
|
|
|
public function getConstraints(SurveyContentModel $question): array
|
|
{
|
|
$choices = ['yes', 'no'];
|
|
|
|
if ('1' === (string) $question->allowMaybe) {
|
|
$choices[] = 'maybe';
|
|
}
|
|
|
|
$constraints = [new Choice(['choices' => $choices])];
|
|
|
|
if ('1' === (string) $question->mandatory) {
|
|
$constraints[] = new NotBlank();
|
|
}
|
|
|
|
return $constraints;
|
|
}
|
|
|
|
public function buildField(FormBuilderInterface $builder, SurveyContentModel $question, string $fieldName = 'answer'): void
|
|
{
|
|
$choices = [
|
|
'Ja' => 'yes',
|
|
'Nein' => 'no',
|
|
];
|
|
|
|
if ('1' === (string) $question->allowMaybe) {
|
|
$choices['Vielleicht'] = 'maybe';
|
|
}
|
|
|
|
$builder->add($fieldName, ChoiceType::class, [
|
|
'label' => false,
|
|
'expanded' => true,
|
|
'multiple' => false,
|
|
'choices' => $choices,
|
|
'constraints' => $this->getConstraints($question),
|
|
]);
|
|
}
|
|
|
|
public function normalizeAnswer(mixed $value, SurveyContentModel $question): string
|
|
{
|
|
return mb_strtolower(trim((string) $value));
|
|
}
|
|
} |