| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\Form; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\NotFoundException; |
| 11 |
use IvyForms\Entity\Form\Form; |
| 12 |
use IvyForms\Repository\Form\FormRepositoryInterface; |
| 13 |
|
| 14 |
class FormService |
| 15 |
{ |
| 16 |
private FormRepositoryInterface $formRepository; |
| 17 |
|
| 18 |
// Constructor injection for the FormRepository via PHP-DI |
| 19 |
public function __construct(FormRepositoryInterface $formRepository) |
| 20 |
{ |
| 21 |
$this->formRepository = $formRepository; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get all forms from the repository. |
| 26 |
* |
| 27 |
* @return mixed[]|null |
| 28 |
*/ |
| 29 |
public function getAllForms(): ?array |
| 30 |
{ |
| 31 |
return $this->formRepository->getAll(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Create a new form. |
| 36 |
* |
| 37 |
* @param Form $formData |
| 38 |
* |
| 39 |
* @return int Form ID |
| 40 |
*/ |
| 41 |
public function createForm(Form $formData): int |
| 42 |
{ |
| 43 |
// Create form via repository and return new form ID |
| 44 |
return $this->formRepository->add($formData); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get a specific form by its ID. |
| 49 |
* |
| 50 |
* @param int $formId |
| 51 |
* |
| 52 |
* @return object FormEntity |
| 53 |
* |
| 54 |
* @throws NotFoundException If the form is not found |
| 55 |
*/ |
| 56 |
public function getFormById(int $formId): object |
| 57 |
{ |
| 58 |
$form = $this->formRepository->getById($formId); |
| 59 |
|
| 60 |
if (!$form) { |
| 61 |
throw new NotFoundException('Form not found.'); |
| 62 |
} |
| 63 |
|
| 64 |
return $form; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Update an existing form. |
| 69 |
* |
| 70 |
* @param int $formId |
| 71 |
* @param Form $formData |
| 72 |
* |
| 73 |
* @return bool |
| 74 |
* |
| 75 |
*/ |
| 76 |
public function updateForm(int $formId, Form $formData): bool |
| 77 |
{ |
| 78 |
// Validate form data before updating |
| 79 |
|
| 80 |
return $this->formRepository->update($formId, $formData); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Update the starred field of a form. |
| 85 |
* |
| 86 |
* @param int $formId |
| 87 |
* @param mixed $value |
| 88 |
* |
| 89 |
* @return bool |
| 90 |
*/ |
| 91 |
public function updateFormStarred(int $formId, $value): bool |
| 92 |
{ |
| 93 |
return $this->formRepository->updateFormStarred($formId, $value); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Update the published status of a form. |
| 98 |
* |
| 99 |
* @param int $formId |
| 100 |
* @param mixed $value |
| 101 |
* |
| 102 |
* @return bool |
| 103 |
*/ |
| 104 |
public function updateFormStatus(int $formId, $value): bool |
| 105 |
{ |
| 106 |
return $this->formRepository->updateFormStatus($formId, $value); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Delete a form by its ID. |
| 111 |
* |
| 112 |
* @param int $formId |
| 113 |
* |
| 114 |
* @return int |
| 115 |
*/ |
| 116 |
public function deleteForm(int $formId): int |
| 117 |
{ |
| 118 |
return $this->formRepository->delete($formId); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Check if a form exists by its ID. |
| 123 |
* |
| 124 |
* @param int $formId |
| 125 |
* |
| 126 |
* @return bool |
| 127 |
*/ |
| 128 |
public function formExists(int $formId): bool |
| 129 |
{ |
| 130 |
return $this->formRepository->exists($formId); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Search for forms based on parameters. |
| 135 |
* |
| 136 |
* @param array<string, mixed> $params |
| 137 |
* @return array<mixed> |
| 138 |
*/ |
| 139 |
public function searchForms(array $params): array |
| 140 |
{ |
| 141 |
return $this->formRepository->search($params); |
| 142 |
} |
| 143 |
} |
| 144 |
|