Initial survey bundle import
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?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\Count;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
|
||||
final class ChoiceQuestionType implements QuestionTypeInterface
|
||||
{
|
||||
public function getName(): string
|
||||
{
|
||||
return 'choice';
|
||||
}
|
||||
|
||||
public function getConstraints(SurveyContentModel $question): array
|
||||
{
|
||||
$choices = $this->getAnswerOptions($question);
|
||||
$multiple = '1' === (string) $question->allowMultiple;
|
||||
$constraints = [new Choice(['choices' => $choices, 'multiple' => $multiple])];
|
||||
|
||||
if ('1' === (string) $question->mandatory) {
|
||||
$constraints[] = $multiple ? new Count(['min' => 1]) : new NotBlank();
|
||||
}
|
||||
|
||||
return $constraints;
|
||||
}
|
||||
|
||||
public function buildField(FormBuilderInterface $builder, SurveyContentModel $question, string $fieldName = 'answer'): void
|
||||
{
|
||||
$choices = [];
|
||||
|
||||
foreach ($this->getAnswerOptions($question) as $answerOption) {
|
||||
$choices[$answerOption] = $answerOption;
|
||||
}
|
||||
|
||||
$builder->add($fieldName, ChoiceType::class, [
|
||||
'label' => false,
|
||||
'expanded' => true,
|
||||
'multiple' => '1' === (string) $question->allowMultiple,
|
||||
'choices' => $choices,
|
||||
'constraints' => $this->getConstraints($question),
|
||||
]);
|
||||
}
|
||||
|
||||
public function normalizeAnswer(mixed $value, SurveyContentModel $question): string
|
||||
{
|
||||
if (!\is_array($value)) {
|
||||
return trim((string) $value);
|
||||
}
|
||||
|
||||
$answers = array_values(array_filter(array_map(static fn (mixed $entry): string => trim((string) $entry), $value), static fn (string $entry): bool => '' !== $entry));
|
||||
|
||||
return implode(' | ', $answers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function getAnswerOptions(SurveyContentModel $question): array
|
||||
{
|
||||
$options = [];
|
||||
|
||||
for ($index = 1; $index <= 10; ++$index) {
|
||||
$value = trim((string) $question->{'answerOption'.$index});
|
||||
|
||||
if ('' !== $value) {
|
||||
$options[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mummert\SurveyBundle\QuestionType;
|
||||
|
||||
use Mummert\SurveyBundle\Model\SurveyContentModel;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
interface QuestionTypeInterface
|
||||
{
|
||||
public function getName(): string;
|
||||
|
||||
/**
|
||||
* @return list<object>
|
||||
*/
|
||||
public function getConstraints(SurveyContentModel $question): array;
|
||||
|
||||
public function buildField(FormBuilderInterface $builder, SurveyContentModel $question, string $fieldName = 'answer'): void;
|
||||
|
||||
public function normalizeAnswer(mixed $value, SurveyContentModel $question): string;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mummert\SurveyBundle\QuestionType;
|
||||
|
||||
final class QuestionTypeRegistry
|
||||
{
|
||||
/**
|
||||
* @var array<string, QuestionTypeInterface>
|
||||
*/
|
||||
private array $questionTypes = [];
|
||||
|
||||
public function __construct(iterable $questionTypes)
|
||||
{
|
||||
foreach ($questionTypes as $questionType) {
|
||||
if (!$questionType instanceof QuestionTypeInterface) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->questionTypes[$questionType->getName()] = $questionType;
|
||||
}
|
||||
}
|
||||
|
||||
public function get(string $type): QuestionTypeInterface
|
||||
{
|
||||
if (!isset($this->questionTypes[$type])) {
|
||||
throw new \InvalidArgumentException(sprintf('Unbekannter Fragetyp "%s".', $type));
|
||||
}
|
||||
|
||||
return $this->questionTypes[$type];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getChoices(): array
|
||||
{
|
||||
return [
|
||||
'Ja-Nein-Frage' => 'yes_no_maybe',
|
||||
'Offene Frage' => 'text',
|
||||
'Bewertungsfrage' => 'range',
|
||||
'Single/Multiple-Choice' => 'choice',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mummert\SurveyBundle\QuestionType;
|
||||
|
||||
use Mummert\SurveyBundle\Model\SurveyContentModel;
|
||||
use Symfony\Component\Form\Extension\Core\Type\RangeType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
use Symfony\Component\Validator\Constraints\Range;
|
||||
|
||||
final class RangeQuestionType implements QuestionTypeInterface
|
||||
{
|
||||
public function getName(): string
|
||||
{
|
||||
return 'range';
|
||||
}
|
||||
|
||||
public function getConstraints(SurveyContentModel $question): array
|
||||
{
|
||||
$constraints = [
|
||||
new Range([
|
||||
'min' => (int) $question->rangeMin,
|
||||
'max' => (int) $question->rangeMax,
|
||||
]),
|
||||
];
|
||||
|
||||
if ('1' === (string) $question->mandatory) {
|
||||
$constraints[] = new NotBlank();
|
||||
}
|
||||
|
||||
return $constraints;
|
||||
}
|
||||
|
||||
public function buildField(FormBuilderInterface $builder, SurveyContentModel $question, string $fieldName = 'answer'): void
|
||||
{
|
||||
$builder->add($fieldName, RangeType::class, [
|
||||
'label' => false,
|
||||
'constraints' => $this->getConstraints($question),
|
||||
'attr' => [
|
||||
'min' => (int) $question->rangeMin,
|
||||
'max' => (int) $question->rangeMax,
|
||||
'step' => max(1, (int) $question->rangeStep),
|
||||
'class' => 'range range-primary w-full',
|
||||
'data-controller' => 'range-preview',
|
||||
'data-action' => 'input->range-preview#update',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function normalizeAnswer(mixed $value, SurveyContentModel $question): string
|
||||
{
|
||||
return (string) (int) $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mummert\SurveyBundle\QuestionType;
|
||||
|
||||
use Mummert\SurveyBundle\Model\SurveyContentModel;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Validator\Constraints\Length;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
|
||||
final class TextQuestionType implements QuestionTypeInterface
|
||||
{
|
||||
public function getName(): string
|
||||
{
|
||||
return 'text';
|
||||
}
|
||||
|
||||
public function getConstraints(SurveyContentModel $question): array
|
||||
{
|
||||
$constraints = [new Length(['max' => 5000])];
|
||||
|
||||
if ('1' === (string) $question->mandatory) {
|
||||
$constraints[] = new NotBlank();
|
||||
}
|
||||
|
||||
return $constraints;
|
||||
}
|
||||
|
||||
public function buildField(FormBuilderInterface $builder, SurveyContentModel $question, string $fieldName = 'answer'): void
|
||||
{
|
||||
$builder->add($fieldName, TextareaType::class, [
|
||||
'label' => false,
|
||||
'constraints' => $this->getConstraints($question),
|
||||
'attr' => [
|
||||
'rows' => 6,
|
||||
'placeholder' => 'Ihre Antwort',
|
||||
'class' => 'textarea textarea-bordered w-full min-h-40',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function normalizeAnswer(mixed $value, SurveyContentModel $question): string
|
||||
{
|
||||
return trim((string) $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user