PluginProbe
The Innovative Form Builder – IvyForms / 0.1.2
The Innovative Form Builder – IvyForms v0.1.2
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Field / FieldService.php

FieldService.php in The Innovative Form Builder – IvyForms 0.1.2, at backend/src/Services/Field/FieldService.php

110 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\Field;
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\Field\Field;
12 use IvyForms\Common\Exceptions\InvalidArgumentException;
13 use IvyForms\Repository\Field\FieldRepositoryInterface;
14
15 class FieldService
16 {
17 private FieldRepositoryInterface $fieldRepository;
18
19 // Constructor injection for the FieldRepository via PHP-DI
20 public function __construct(FieldRepositoryInterface $fieldRepository)
21 {
22 $this->fieldRepository = $fieldRepository;
23 }
24
25 /**
26 * Create a new field.
27 *
28 * @param Field $fieldData
29 *
30 * @return int Form ID
31 *
32 * @throws InvalidArgumentException If the field data is invalid
33 */
34 public function addField(Field $fieldData): int
35 {
36 // Validate form data
37 if (empty($fieldData->getFormId())) {
38 throw new InvalidArgumentException('Form id is required.');
39 }
40
41 // Create form via repository and return new form ID
42 return $this->fieldRepository->add($fieldData);
43 }
44
45
46 /**
47 * Update an existing field.
48 *
49 * @param int $fieldId
50 * @param Field $fieldData
51 *
52 * @return bool
53 *
54 * @throws InvalidArgumentException If the form data is invalid
55 */
56 public function updateField(int $fieldId, Field $fieldData): bool
57 {
58 // Validate form data before updating
59 if (empty($fieldData->getId())) {
60 throw new InvalidArgumentException('Filed id is required.');
61 }
62
63 return $this->fieldRepository->update($fieldId, $fieldData);
64 }
65
66 /**
67 * Delete a field by its ID.
68 *
69 * @param int $fieldId
70 * @return int
71 */
72 public function deleteField(int $fieldId): int
73 {
74 return $this->fieldRepository->delete($fieldId);
75 }
76
77 /**
78 * Get fields by form ID
79 *
80 * @param int $id
81 *
82 * @return mixed[]|null
83 */
84 public function getAllFields(int $id): ?array
85 {
86 return $this->fieldRepository->getFieldsById($id);
87 }
88
89 /**
90 * Delete fields by form ID
91 *
92 * @param int $id
93 *
94 * @throws NotFoundException
95 * @throws InvalidArgumentException
96 */
97 public function deleteFieldsByFormId(int $id): void
98 {
99 $fields = $this->getAllFields($id);
100
101 if (!$fields) {
102 return;
103 }
104
105 foreach ($fields as $field) {
106 $this->deleteField($field->getId());
107 }
108 }
109 }
110