# ivyforms/0.2/backend/src/ValueObjects/Field/Field.php

The Innovative Form Builder – IvyForms, version 0.2. 281 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.2/code/backend/src/ValueObjects/Field/Field.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.2/raw/backend/src/ValueObjects/Field/Field.php
- Modified: 2025-07-24T13:31:58+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ivyforms/0.2/code/backend/src/ValueObjects/Field/Field.php#L10-L20`.

```php
<?php

/**
* @copyright © TMS-Plugins. All rights reserved.
* @licence   See LICENCE.md for license details.
*/

namespace IvyForms\ValueObjects\Field;

// phpcs:disable PSR1.Files.SideEffects
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

use IvyForms\Common\Exceptions\InvalidArgumentException;
use IvyForms\Common\Exceptions\ValidationException;

/**
* Class Field
*
* @package IvyForms\ValueObjects\Field
*/
final class Field
{
    /**
     * @var int
     */
    public int $id;
    /**
     * @var int
     */
    public int $formId;
    /**
     * @var int
     */
    public int $fieldIndex;
    /**
     * @var string
     */
    public string $type;
    /**
     * @var string
     */
    public string $label;
    /**
     * @var string
     */
    public string $defaultValue;
    /**
     * @var bool
     */
    public bool $required;
    /**
     * @var string
     */
    public string $placeholder;
    /**
     * @var int
     */
    public int $position;

    /**
     * Field constructor.
     *
     * @param int $id
     * @param int $formId
     * @param int $fieldIndex
     * @param string $type
     * @param string $label
     * @param string $defaultValue
     * @param bool $required
     * @param string $placeholder
     * @param int $position
     *
     * @throws ValidationException
     */
    public function __construct(
        int $id,
        int $formId,
        int $fieldIndex,
        string $type,
        string $label,
        string $defaultValue,
        bool $required,
        string $placeholder,
        int $position
    ) {
        $this->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<string, mixed>
     */
    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(),
        ];
    }
}

```
