*/ #[ORM\Column(type: Types::JSON)] private array $answers = []; /** * @var array */ #[ORM\Column(type: Types::JSON)] private array $context = []; #[ORM\ManyToOne] #[ORM\JoinColumn(onDelete: 'CASCADE')] private ?Disposition $disposition = null; /** * A BusLeg value, or '' for a questionnaire without legs rather than null: part of the unique * key, and MariaDB lets NULLs repeat in one. */ #[ORM\Column(length: 16, options: ['default' => ''])] private string $leg = ''; private function __construct(Definition $definition) { $this->uuid = Uuid::v4(); $this->questionnaire = $definition->key; $this->version = $definition->version; } /** * @param BusLeg|null $leg required for a per-bus-leg questionnaire, refused for any other */ public static function forDisposition(Definition $definition, Disposition $disposition, ?BusLeg $leg = null): static { if ($definition->perBusLeg !== (null !== $leg)) { throw new \LogicException(sprintf('Questionnaire "%s" %s a bus leg', $definition->key, $definition->perBusLeg ? 'needs' : 'takes no')); } $instance = new static($definition); $instance->disposition = $disposition; $instance->leg = $leg->value ?? ''; return $instance; } /** * Keeps every answer, including those to questions that are hidden right now: switching an * answer back restores the follow-up already written. * * @param array $answers * @param array $context */ public function saveDraft(array $answers, array $context): static { $this->assertDraft(); $this->answers = $answers; $this->context = $context; return $this; } /** * Final. Keeps only the answers to questions that are shown, and records the definition version * they were given under - the draft may have been started under an older one. * * @param array $answers * @param array $context */ public function submit(Definition $definition, array $answers, array $context, \DateTimeImmutable $now): static { $this->assertDraft(); if ($definition->key !== $this->questionnaire) { throw new \LogicException(sprintf('Response to "%s" cannot be submitted with "%s"', $this->questionnaire, $definition->key)); } $this->answers = $definition->withoutHiddenAnswers($answers); $this->context = $context; $this->version = $definition->version; $this->status = QuestionnaireResponseStatus::SUBMITTED; $this->submittedAt = $now; return $this; } public function isSubmitted(): bool { return QuestionnaireResponseStatus::SUBMITTED === $this->status; } public function getId(): ?int { return $this->id; } public function getUuid(): string { return $this->uuid; } public function getQuestionnaire(): string { return $this->questionnaire; } public function getVersion(): int { return $this->version; } public function getStatus(): QuestionnaireResponseStatus { return $this->status; } public function getSubmittedAt(): ?\DateTimeImmutable { return $this->submittedAt; } /** * @return array */ public function getAnswers(): array { return $this->answers; } /** * @return array */ public function getContext(): array { return $this->context; } public function getDisposition(): ?Disposition { return $this->disposition; } public function getLeg(): ?BusLeg { return BusLeg::tryFrom($this->leg); } private function assertDraft(): void { if ($this->isSubmitted()) { throw new \LogicException(sprintf('Questionnaire response %s is already submitted', $this->uuid)); } } }