*/ private array $options = []; /** * Display order within the script, from 0. Set by drag and drop in the form. */ private int $position = 0; public static function loadMetadata(OrmClassMetadata $metadata): void { $builder = new ClassMetadataBuilder($metadata); $builder->setTable(self::TABLE_NAME); $builder->addId(); $builder->createManyToOne('callScript', CallScript::class) ->inversedBy('outcomes') ->addJoinColumn('call_script_id', 'id', false, false, 'CASCADE') ->build(); $builder->createField('label', Types::STRING) ->length(self::LABEL_MAX_LENGTH) ->build(); // Mapped on the raw metadata: ClassMetadataBuilder forces VARCHAR(191) on strings and // doesn't accept enum or JSON types. $metadata->mapField([ 'fieldName' => 'inputType', 'columnName' => 'input_type', 'type' => Types::STRING, 'length' => 20, 'enumType' => CallScriptOutcomeInputType::class, 'options' => ['default' => CallScriptOutcomeInputType::FreeText->value], ]); $metadata->mapField([ 'fieldName' => 'options', 'columnName' => 'options', 'type' => Types::JSON, ]); $builder->createField('position', Types::INTEGER) ->option('default', 0) ->build(); } public static function loadValidatorMetadata(ClassMetadata $metadata): void { $metadata->addPropertyConstraint('label', new NotBlank(message: 'mt3s.call_script.outcome.label.required')); $metadata->addPropertyConstraint('label', new Length(max: self::LABEL_MAX_LENGTH)); $metadata->addConstraint(new ValidCallScriptOutcomeOptions()); } public function getId(): ?int { return $this->id; } public function getCallScript(): ?CallScript { return $this->callScript; } public function setCallScript(?CallScript $callScript): static { $this->callScript = $callScript; return $this; } public function getLabel(): ?string { return $this->label; } public function setLabel(?string $label): static { $this->label = $label; return $this; } public function getInputType(): CallScriptOutcomeInputType { return $this->inputType; } /** * Null (a type field missing from the submitted form) keeps the current type. */ public function setInputType(?CallScriptOutcomeInputType $inputType): static { if (null !== $inputType) { $this->inputType = $inputType; } return $this; } public function hasOptions(): bool { return $this->inputType->hasOptions(); } /** * @return list */ public function getOptions(): array { return $this->options; } /** * Keeps the {value, label} rows of the submitted options, dropping the empty ones and * defaulting an empty label to the value. * * @param list|array|null $options */ public function setOptions(?array $options): static { $clean = []; foreach (array_slice($options ?? [], 0, self::OPTIONS_MAX) as $option) { if (!is_array($option)) { continue; } $value = trim((string) ($option['value'] ?? '')); $label = trim((string) ($option['label'] ?? '')); if ('' === $value && '' === $label) { continue; } $clean[] = ['value' => $value, 'label' => '' !== $label ? $label : $value]; } $this->options = $clean; return $this; } public function getPosition(): int { return $this->position; } public function setPosition(?int $position): static { $this->position = $position ?? 0; return $this; } }