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); } /** * 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; } /** * 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; } /** * 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(), ]; } }