# ivyforms/0.3.1/backend/src/ValueObjects/Form/Form.php

The Innovative Form Builder – IvyForms, version 0.3.1. 297 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.3.1/code/backend/src/ValueObjects/Form/Form.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.3.1/raw/backend/src/ValueObjects/Form/Form.php
- Modified: 2025-09-04T07:37:14+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.3.1/code/backend/src/ValueObjects/Form/Form.php#L10-L20`.

```php
<?php

namespace IvyForms\ValueObjects\Form;

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

use IvyForms\Common\Exceptions\ValidationException;

/**
 * Class Form
 *
 * @package IvyForms\ValueObjects\Form
 */
final class Form
{
    /**
     * @var int
     */
    public int $id;
    /**
     * @var string
     */
    public string $name;
    /**
     * @var string
     */
    public string $author;
    /**
     * @var bool
     */
    public bool $published;
    /**
     * @var bool
     */
    public bool $starred;
    /**
     * @var string
     */
    public string $dateCreated;
    /**
     * @var string
     */
    public string $dateEdited;
    /**
     * @var mixed[]|null
     */
    public ?array $fields;
    /**
     * @var string
     */
    public string $description;
    /**
     * @var bool
     */
    public bool $showTitle;
    /**
     * @var bool
     */
    public bool $showDescription;
    /**
     * @var bool
     */
    public bool $storeEntries;

    /**
     * Form constructor.
     *
     * @param int          $id
     * @param string       $name
     * @param string       $author
     * @param bool         $starred
     * @param bool         $published
     * @param string       $description
     * @param bool         $showTitle
     * @param bool         $showDescription
     * @param bool         $storeEntries
     * @param mixed[]|null $fields
     *
     * @throws ValidationException
     */
    public function __construct(
        int $id,
        string $name,
        string $author,
        bool $starred,
        bool $published,
        string $description,
        bool $showTitle,
        bool $showDescription,
        bool $storeEntries,
        array $fields = null
    ) {
        $this->id              = $this->validateId($id);
        $this->name            = $this->validateString($name, 255, 'name');
        $this->author          = $this->validateString($author, 1000, 'author');
        $this->starred         = $this->validateBool($starred);
        $this->published       = $this->validateBool($published);
        $this->dateCreated     = gmdate('Y-m-d H:i:s');
        $this->dateEdited      = gmdate('Y-m-d H:i:s');
        $this->fields          = $fields ?? [];
        $this->description     = $this->validateString($description, 1000, 'description');
        $this->showTitle       = $this->validateBool($showTitle);
        $this->showDescription = $this->validateBool($showDescription);
        $this->storeEntries    = $this->validateBool($storeEntries);
    }

    /**
     * Validates the ID.
     *
     * @param int $id
     *
     * @return int
     * @throws ValidationException
     */
    private 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 a boolean field.
     *
     * @param bool $value
     *
     * @return bool
     */
    private function validateBool(bool $value): bool
    {
        return $value;
    }

    /**
     * Get the form ID.
     *
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * Get the form name.
     *
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * Get the form author.
     *
     * @return string
     */
    public function getAuthor(): string
    {
        return $this->author;
    }

    /**
     * Get the form starred status.
     *
     * @return bool
     */
    public function isStarred(): bool
    {
        return $this->starred;
    }
    /**
     * Get the form published status.
     *
     * @return bool
     */
    public function isPublished(): bool
    {
        return $this->published;
    }
    /**
     * Get the date of form creation.
     *
     * @return string
     */
    public function getDateCreated(): string
    {
        return $this->dateCreated;
    }
    /**
     * Get the form date edited.
     *
     * @return string
     */
    public function getDateEdited(): string
    {
        return $this->dateEdited;
    }
    /**
     * Get the form fields.
     *
     * @return mixed[]|null
     */
    public function getFields(): ?array
    {
        return $this->fields;
    }
    /**
     * Get the form description.
     *
     * @return string
     */
    public function getDescription(): string
    {
        return $this->description;
    }
    /**
     * Get the show description of the form.
     *
     * @return bool The form show description status.
     */
    public function isDescriptionVisible(): bool
    {
        return $this->showDescription;
    }
    /**
     * Get the form store entries' status.
     * @return bool
     */
    public function isStoreEntries(): bool
    {
        return $this->storeEntries;
    }

    /**
     * Get the show title of the form.
     *
     * @return bool The form show title status.
     */
    public function isTitleVisible(): bool
    {
        return $this->showTitle;
    }

    /**
     * Convert the form to an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'id'              => $this->getId(),
            'name'            => $this->getName(),
            'author'          => $this->getAuthor(),
            'starred'         => $this->isStarred(),
            'published'       => $this->isPublished(),
            'fields'          => $this->getFields(),
            'description'     => $this->getDescription(),
            'showTitle'       => $this->isTitleVisible(),
            'showDescription' => $this->isDescriptionVisible(),
            'storeEntries'    => $this->isStoreEntries(),
        ];
    }
}

```
