# ivyforms/0.1.2/backend/src/Services/Form/FormService.php

The Innovative Form Builder – IvyForms, version 0.1.2. 144 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.1.2/code/backend/src/Services/Form/FormService.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.1.2/raw/backend/src/Services/Form/FormService.php
- Modified: 2025-06-12T10:29: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.1.2/code/backend/src/Services/Form/FormService.php#L10-L20`.

```php
<?php

namespace IvyForms\Services\Form;

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

use IvyForms\Common\Exceptions\NotFoundException;
use IvyForms\Entity\Form\Form;
use IvyForms\Repository\Form\FormRepositoryInterface;

class FormService
{
    private FormRepositoryInterface $formRepository;

    // Constructor injection for the FormRepository via PHP-DI
    public function __construct(FormRepositoryInterface $formRepository)
    {
        $this->formRepository = $formRepository;
    }

    /**
     * Get all forms from the repository.
     *
     * @return mixed[]|null
     */
    public function getAllForms(): ?array
    {
        return $this->formRepository->getAll();
    }

    /**
     * Create a new form.
     *
     * @param Form $formData
     *
     * @return int Form ID
     */
    public function createForm(Form $formData): int
    {
        // Create form via repository and return new form ID
        return $this->formRepository->add($formData);
    }

    /**
     * Get a specific form by its ID.
     *
     * @param int $formId
     *
     * @return object FormEntity
     *
     * @throws NotFoundException If the form is not found
     */
    public function getFormById(int $formId): object
    {
        $form = $this->formRepository->getById($formId);

        if (!$form) {
            throw new NotFoundException('Form not found.');
        }

        return $form;
    }

    /**
     * Update an existing form.
     *
     * @param int $formId
     * @param Form $formData
     *
     * @return bool
     *
     */
    public function updateForm(int $formId, Form $formData): bool
    {
        // Validate form data before updating

        return $this->formRepository->update($formId, $formData);
    }

    /**
     * Update the starred field of a form.
     *
     * @param int $formId
     * @param mixed $value
     *
     * @return bool
     */
    public function updateFormStarred(int $formId, $value): bool
    {
        return $this->formRepository->updateFormStarred($formId, $value);
    }

    /**
     * Update the published status of a form.
     *
     * @param int $formId
     * @param mixed $value
     *
     * @return bool
     */
    public function updateFormStatus(int $formId, $value): bool
    {
            return $this->formRepository->updateFormStatus($formId, $value);
    }

    /**
     * Delete a form by its ID.
     *
     * @param int $formId
     *
     * @return int
     */
    public function deleteForm(int $formId): int
    {
        return $this->formRepository->delete($formId);
    }

    /**
     * Check if a form exists by its ID.
     *
     * @param int $formId
     *
     * @return bool
     */
    public function formExists(int $formId): bool
    {
        return $this->formRepository->exists($formId);
    }

    /**
     * Search for forms based on parameters.
     *
     * @param array<string, mixed> $params
     * @return array<mixed>
     */
    public function searchForms(array $params): array
    {
        return $this->formRepository->search($params);
    }
}

```
