Additional properties from Pro plugin */ private array $additionalProperties = []; /** * Field constructor. * @SuppressWarnings(PHPMD) * * @param int $id * @param int $formId * @param int $fieldIndex * @param string $type * @param int $position * @param int|null $parentId * @param FieldGeneralSettings $generalSettings * @param FieldOptions $fieldOptions * @param FieldAdvancedSettings $advancedSettings * @param int $rows * @param bool $confirmFieldEnabled * @param string $confirmFieldLabel * @param string $confirmFieldPlaceholder * @param bool $confirmFieldHideLabel * * @throws ValidationException */ public function __construct( int $id, int $formId, int $fieldIndex, string $type, int $position, FieldGeneralSettings $generalSettings, FieldOptions $fieldOptions, FieldAdvancedSettings $advancedSettings, ?int $parentId = null, int $rows = 0, bool $confirmFieldEnabled = false, string $confirmFieldLabel = '', string $confirmFieldPlaceholder = '', bool $confirmFieldHideLabel = false ) { $this->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); /** * Allows initialization of additional properties for the Field value object. * @since 0.1.0 * * @param mixed[] $additionalProperties The current list of additional properties. * @param Field $field The Field value object instance. * @return mixed[] The modified list of additional properties. */ $this->additionalProperties = apply_filters('ivyforms/field/value_object/init_additional_properties', [], $this); } /** * Set an additional property * * @param string $key * @param mixed $value * * @return void */ public function setAdditionalProperty(string $key, $value): void { foreach ($this->additionalProperties as &$property) { if (array_key_exists($key, $property)) { $property[$key] = $value; // update existing return; } } // if not found, append new key-value pair $this->additionalProperties[] = [$key => $value]; } /** * Get an additional property * * @param string $key * @return mixed|null The value of the additional property, or null if not found */ public function getAdditionalProperty(string $key) { foreach ($this->additionalProperties as $property) { if (array_key_exists($key, $property)) { return $property[$key]; } } return null; } /** * Get all additional properties * * @return array The additional properties */ public function getAllAdditionalProperties(): array { return $this->additionalProperties; } /** * 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 { $data = [ '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() ]; /** * Allows modification of the array representation of the Field value object. * @since 0.1.0 * * @param mixed[] $data The current array representation of the Field value object. * @param Field $field The Field value object instance. * @return mixed[] The modified array representation of the Field value object. */ return apply_filters('ivyforms/field/value_object/add_to_array_additional_properties', $data, $this); } }