id = $this->validateId($id); $this->formId = $this->validateId($formId) ; $this->fieldIndex = $this->validateId($fieldIndex); $this->type = $this->validateString($type, 50, 'type'); $this->label = $this->validateString($label, 255, 'label'); $this->defaultValue = $this->validateString($defaultValue, 255, 'defaultValue'); $this->required = $this->validateRequired($required); $this->placeholder = $this->validateString($placeholder, 255, 'placeholder'); $this->position = $this->validatePosition($position); $this->hideLabel = $this->validateBool($hideLabel); $this->readOnly = $this->validateBool($readOnly); $this->description = $this->validateString($description, 255, 'description'); $this->requiredMessage = $this->validateString($requiredMessage, 255, 'requiredMessage'); $this->cssClasses = $this->validateString($cssClasses, 255, 'cssClasses'); } /** * Validates the ID. * * @param int $id * * @return int * @throws ValidationException */ public function validateId(int $id): int { if ($id < 0) { throw new ValidationException("Id must be a positive integer."); } return $id; } /** * 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 required field. * * @param bool $value * * @return bool * */ private function validateRequired(bool $value): bool { return $value; } /** * Validates the position. * * @param int $value * * @return int * * @throws ValidationException */ private function validatePosition(int $value): int { if ($value < 0) { throw new ValidationException("position must be a non-negative integer."); } return $value; } /** * Validates a boolean field. * * @param bool $value * * @return bool */ private function validateBool(bool $value): bool { return $value; } /** * 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 label. * * @return string */ public function getLabel(): string { return $this->label; } /** * Get the default value. * * @return string */ public function getDefaultValue(): string { return $this->defaultValue; } /** * Check if the field is required. * * @return bool */ public function isRequired(): bool { return $this->required; } /** * Get the field placeholder. * * @return string */ public function getPlaceholder(): string { return $this->placeholder; } /** * Get the field position. * * @return int */ public function getPosition(): int { return $this->position; } /** * Check if the field label should be hidden. * * @return bool */ public function isLabelHidden(): bool { return $this->hideLabel; } /** * Set the hideLabel setting. * * @param bool $hideLabel */ public function setHideLabel(bool $hideLabel): void { $this->hideLabel = $hideLabel; } /** * Check if the field is read-only. * * @return bool */ public function isReadOnly(): bool { return $this->readOnly; } /** * Set the readOnly setting. * * @param bool $readOnly */ public function setReadOnly(bool $readOnly): void { $this->readOnly = $readOnly; } /** * Get the field description. * * @return string */ public function getDescription(): string { return $this->description; } /** * Set the description setting. * * @param string $description */ public function setDescription(string $description): void { $this->description = $description; } /** * Get the field required message. * * @return string */ public function getRequiredMessage(): string { return $this->requiredMessage; } /** * Set the required message setting. * * @param string $requiredMessage */ public function setRequiredMessage(string $requiredMessage): void { $this->requiredMessage = $requiredMessage; } /** * Get the field CSS classes. * * @return string */ public function getCssClasses(): string { return $this->cssClasses; } /** * Set the CSS classes setting. * * @param string $cssClasses */ public function setCssClasses(string $cssClasses): void { $this->cssClasses = $cssClasses; } /** * Convert the field to an array. * * @return array */ public function toArray(): array { return [ 'id' => $this->getId(), 'formId' => $this->getFormId(), 'fieldIndex' => $this->getIndex(), 'type' => $this->getType(), 'label' => $this->getLabel(), 'defaultValue' => $this->getDefaultValue(), 'required' => $this->isRequired(), 'placeholder' => $this->getPlaceholder(), 'position' => $this->getPosition(), 'hideLabel' => $this->isLabelHidden(), 'readOnly' => $this->isReadOnly(), 'description' => $this->getDescription(), 'requiredMessage' => $this->getRequiredMessage(), 'cssClasses' => $this->getCssClasses(), ]; } }