id = $this->validateId($id); $this->formId = $this->validateId($formId); $this->fieldIndex = $this->validateId($fieldIndex); $this->type = $this->validateString($type, 50, 'type'); $this->position = $this->validatePosition($position); $this->parentId = $this->validateParentId($parentId); $this->generalSettings = $generalSettings; $this->fieldOptions = $fieldOptions; $this->advancedSettings = $advancedSettings; $this->rows = $this->validateRows($rows); $this->confirmFieldEnabled = $this->validateBool($confirmFieldEnabled); $this->confirmFieldLabel = $this->validateString($confirmFieldLabel, 255, 'confirmFieldLabel'); $this->confirmFieldPlaceholder = $this->validateString( $confirmFieldPlaceholder, 255, 'confirmFieldPlaceholder' ); $this->confirmFieldHideLabel = $this->validateBool($confirmFieldHideLabel); } /** * Validates the ID. * * @param int $id * * @return int * @throws ValidationException */ public function validateId(int $id): int { if ($id < 0) { throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']); } return $id; } /** * Validates parent id which can be null or non-negative int. * * @param int|null $parentId * * @return int|null * @throws ValidationException */ private function validateParentId(?int $parentId): ?int { if ($parentId === null) { return null; } if ($parentId < 0) { throw new ValidationException(BackendStrings::getExceptionStrings()['parentId_positive_integer']); } return $parentId; } /** * Validates a string value. * * @param string $value * @param int $maxLength * @param string $fieldName * * @return string * * @throws ValidationException */ private function validateString(string $value, int $maxLength, string $fieldName): string { if (strlen($value) > $maxLength) { throw new ValidationException( sprintf( /* translators: 1: String value, 2: String max length. */ esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'), esc_html($fieldName), $maxLength ) ); } return $value; } /** * Validates the position. * * @param int $value * * @return int * * @throws ValidationException */ private function validatePosition(int $value): int { if ($value < 0) { throw new ValidationException( esc_html__('position must be a non-negative integer.', 'ivyforms') ); } return $value; } /** * Validates a boolean field. * * @param bool $value * * @return bool */ private function validateBool(bool $value): bool { return $value; } /** * Validate rows. * * @param int $rows * @return int * @throws ValidationException */ private function validateRows(int $rows): int { if ($rows < 0) { throw new ValidationException( esc_html__('rows must be at least 1.', 'ivyforms') ); } return $rows; } /** * Get the form ID. * * @return int */ public function getFormId(): int { return $this->formId; } /** * Get the field ID. * * @return int */ public function getId(): int { return $this->id; } /** * Get the field index. * * @return int */ public function getIndex(): int { return $this->fieldIndex; } /** * Get the field type. * * @return string */ public function getType(): string { return $this->type; } /** * Get the field position. * * @return int */ public function getPosition(): int { return $this->position; } /** * Get the parent field id. * * @return int|null */ public function getParentId(): ?int { return $this->parentId; } /** * Get the general settings object. * * @return FieldGeneralSettings */ public function getGeneralSettings(): FieldGeneralSettings { return $this->generalSettings; } /** * Get the field options. * * @return FieldOptions */ public function getFieldOptions(): FieldOptions { return $this->fieldOptions; } /** * Get number of rows. * @return int */ public function getRows(): int { return $this->rows; } /** * Set number of rows. * @param int $rows */ public function setRows(int $rows): void { $this->rows = $rows; } /** * Check if the confirm field feature is enabled. * * @return bool */ public function isConfirmFieldEnabled(): bool { return $this->confirmFieldEnabled; } /** * Get the confirm field label. * * @return string */ public function getConfirmFieldLabel(): string { return $this->confirmFieldLabel; } /** * Get the confirm field placeholder. * * @return string */ public function getConfirmFieldPlaceholder(): string { return $this->confirmFieldPlaceholder; } /** * Check if the confirm field label should be hidden. * * @return bool */ public function isConfirmFieldHideLabel(): bool { return $this->confirmFieldHideLabel; } /** * Set the confirm field feature enabled/disabled. * * @param bool $enabled */ public function setConfirmFieldEnabled(bool $enabled): void { $this->confirmFieldEnabled = $enabled; } /** * Set the confirm field label. * * @param string $label */ public function setConfirmFieldLabel(string $label): void { $this->confirmFieldLabel = $label; } /** * Set the confirm field placeholder. * * @param string $ph */ public function setConfirmFieldPlaceholder(string $ph): void { $this->confirmFieldPlaceholder = $ph; } /** * Set the confirm field label visibility. * * @param bool $hide */ public function setConfirmFieldHideLabel(bool $hide): void { $this->confirmFieldHideLabel = $hide; } /** * Get the general settings object. * * @return FieldAdvancedSettings */ public function getAdvancedSettings(): FieldAdvancedSettings { return $this->advancedSettings; } /** * Convert the field to an array. * * @return array */ public function toArray(): array { return [ 'id' => $this->id, 'formId' => $this->formId, 'fieldIndex' => $this->fieldIndex, 'type' => $this->type, 'position' => $this->position, 'parentId' => $this->parentId, 'generalSettings' => $this->generalSettings->toArray(), 'fieldOptions' => $this->fieldOptions->toArray(), 'rows' => $this->rows, 'confirmFieldEnabled' => $this->confirmFieldEnabled, 'confirmFieldLabel' => $this->confirmFieldLabel, 'confirmFieldPlaceholder' => $this->confirmFieldPlaceholder, 'confirmFieldHideLabel' => $this->confirmFieldHideLabel, // Advanced options 'advancedSettings' => $this->advancedSettings->toArray() ]; } }