PluginProbe
The Innovative Form Builder – IvyForms / 0.3
The Innovative Form Builder – IvyForms v0.3
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.3, at backend/src/Services/Field/FieldService.php

136 lines 3.3 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\Common\Exceptions\ValidationException;
12 use IvyForms\Entity\Field\Field;
13 use IvyForms\Common\Exceptions\InvalidArgumentException;
14 use IvyForms\Factory\Field\FieldFactory;
15 use IvyForms\Repository\Field\FieldRepositoryInterface;
16 use IvyForms\Services\Translations\BackendStrings;
17
18 class FieldService
19 {
20 private FieldRepositoryInterface $fieldRepository;
21
22 // Constructor injection for the FieldRepository via PHP-DI
23 public function __construct(FieldRepositoryInterface $fieldRepository)
24 {
25 $this->fieldRepository = $fieldRepository;
26 }
27
28 /**
29 * Create a new field.
30 *
31 * @param Field $fieldData
32 *
33 * @return int Form ID
34 *
35 * @throws InvalidArgumentException If the field data is invalid
36 */
37 public function addField(Field $fieldData): int
38 {
39 // Validate form data
40 if (empty($fieldData->getFormId())) {
41 throw new InvalidArgumentException(
42 BackendStrings::getExceptionStrings()['form_id_required']
43 );
44 }
45
46 // Create form via repository and return new form ID
47 return $this->fieldRepository->add($fieldData);
48 }
49
50
51 /**
52 * Update an existing field.
53 *
54 * @param int $fieldId
55 * @param Field $fieldData
56 *
57 * @return bool
58 *
59 * @throws InvalidArgumentException If the form data is invalid
60 */
61 public function updateField(int $fieldId, Field $fieldData): bool
62 {
63 // Validate form data before updating
64 if (empty($fieldData->getId())) {
65 throw new InvalidArgumentException(
66 BackendStrings::getExceptionStrings()['field_id_required']
67 );
68 }
69
70 return $this->fieldRepository->update($fieldId, $fieldData);
71 }
72
73 /**
74 * Delete a field by its ID.
75 *
76 * @param int $fieldId
77 * @return int
78 */
79 public function deleteField(int $fieldId): int
80 {
81 return $this->fieldRepository->delete($fieldId);
82 }
83
84 /**
85 * Get fields by form ID
86 *
87 * @param int $id
88 *
89 * @return mixed[]|null
90 */
91 public function getAllFields(int $id): ?array
92 {
93 return $this->fieldRepository->getFieldsById($id);
94 }
95
96 /**
97 * Delete fields by form ID
98 *
99 * @param int $id
100 *
101 * @throws NotFoundException
102 * @throws InvalidArgumentException
103 */
104 public function deleteFieldsByFormId(int $id): void
105 {
106 $fields = $this->getAllFields($id);
107
108 if (!$fields) {
109 return;
110 }
111
112 foreach ($fields as $field) {
113 $this->deleteField($field->getId());
114 }
115 }
116
117 /**
118 * @param int $originalFormId
119 * @param int $newFormId
120 *
121 * @return void
122 * @throws InvalidArgumentException|ValidationException
123 */
124 public function duplicateFields(int $originalFormId, int $newFormId): void
125 {
126 $fields = $this->getAllFields($originalFormId);
127 foreach ($fields as $field) {
128 $fieldData = $field->toArray();
129 unset($fieldData['id']);
130 $fieldData['formId'] = $newFormId;
131
132 $this->addField(FieldFactory::create($fieldData));
133 }
134 }
135 }
136