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

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