PluginProbe
The Innovative Form Builder – IvyForms / 1.1
The Innovative Form Builder – IvyForms v1.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 / Entity / Field / Field.php

Field.php in The Innovative Form Builder – IvyForms 1.1, at backend/src/Entity/Field/Field.php

365 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Entity\Field;
4
5 use IvyForms\ValueObjects\Field\Field as FieldValueObject;
6 use IvyForms\Entity\Field\FieldGeneralSettings;
7 use IvyForms\Entity\Field\FieldAdvancedSettings;
8
9 /**
10 * Class Field
11 *
12 * @package IvyForms\Entity\Field
13 */
14 class Field
15 {
16 /**
17 * @var FieldGeneralSettings
18 */
19 private FieldGeneralSettings $fieldGeneralSettings;
20 /**
21 * @var FieldAdvancedSettings
22 */
23 private FieldAdvancedSettings $fieldAdvancedSettings;
24 /**
25 * @var FieldValueObject
26 */
27 private FieldValueObject $formField;
28
29 /**
30 * @var array<string, mixed> Additional properties
31 */
32 private array $additionalProperties = [];
33
34 /**
35 * Field constructor.
36 *
37 * @param FieldValueObject $formField The form field object.
38 */
39 public function __construct(FieldValueObject $formField)
40 {
41 $this->formField = $formField;
42 $this->fieldGeneralSettings = new FieldGeneralSettings($formField->getGeneralSettings());
43 $this->fieldAdvancedSettings = new FieldAdvancedSettings($formField->getAdvancedSettings());
44
45 /**
46 * Allows initialization of additional properties for the Field entity.
47 * @since 0.1.0
48 *
49 * @param mixed[] $additionalProperties The current list of additional properties.
50 * @param Field $field The Field entity instance.
51 * @return mixed[] The modified list of additional properties.
52 */
53 $this->additionalProperties = apply_filters('ivyforms/field/entity/init_additional_properties', [], $this);
54 }
55
56 /**
57 * Get the ID of the form to which this field belongs
58 *
59 * @return int The form ID.
60 */
61 public function getFormId(): int
62 {
63 return $this->formField->getFormId();
64 }
65
66 /**
67 * Set the ID of the form to which this field belongs.
68 *
69 * @param int $formId The form ID to set.
70 */
71 public function setFormId(int $formId): void
72 {
73 $this->formField->formId = $formId;
74 }
75
76 /**
77 * Get ID of the form field.
78 *
79 * @return int The form field id.
80 */
81 public function getId(): int
82 {
83 return $this->formField->getId();
84 }
85
86 /**
87 * Set the ID of the form field.
88 *
89 * @param int $id The ID to set.
90 */
91 public function setId(int $id): void
92 {
93 $this->formField->id = $id;
94 }
95
96 /**
97 * Get the index of the form field.
98 *
99 * @return int The index of the form field.
100 */
101 public function getIndex(): int
102 {
103 return $this->formField->getIndex();
104 }
105
106 /**
107 * Set the index of the form field.
108 *
109 * @param int $index The index to set.
110 */
111 public function setIndex(int $index): void
112 {
113 $this->formField->fieldIndex = $index;
114 }
115
116 /**
117 * Get the type of the form field.
118 *
119 * @return string The type of the form field.
120 */
121 public function getType(): string
122 {
123 return $this->formField->getType();
124 }
125
126 /**
127 * Set the type of the form field.
128 *
129 * @param string $type The type to set.
130 */
131 public function setType(string $type): void
132 {
133 $this->formField->type = $type;
134 }
135
136 /**
137 * Get the position of the form field.
138 *
139 * @return int The position of the form field.
140 */
141 public function getPosition(): int
142 {
143 return $this->formField->getPosition();
144 }
145
146 /**
147 * Set the position of the form field.
148 *
149 * @param int $position The position to set.
150 */
151 public function setPosition(int $position): void
152 {
153 $this->formField->position = $position;
154 }
155
156 /**
157 * Get the row index of the form field.
158 *
159 * @return int The row index of the form field.
160 */
161 public function getRowIndex(): int
162 {
163 return $this->formField->getRowIndex();
164 }
165
166 /**
167 * Set the row index of the form field.
168 *
169 * @param int $rowIndex The row index to set.
170 */
171 public function setRowIndex(int $rowIndex): void
172 {
173 $this->formField->rowIndex = $rowIndex;
174 }
175
176 /**
177 * Get the column index of the form field.
178 *
179 * @return int The column index of the form field.
180 */
181 public function getColumnIndex(): int
182 {
183 return $this->formField->getColumnIndex();
184 }
185
186 /**
187 * Set the column index of the form field.
188 *
189 * @param int $columnIndex The column index to set.
190 */
191 public function setColumnIndex(int $columnIndex): void
192 {
193 $this->formField->columnIndex = $columnIndex;
194 }
195
196 /**
197 * Get the width percentage of the form field.
198 *
199 * @return int The width percentage of the form field.
200 */
201 public function getWidth(): int
202 {
203 return $this->formField->getWidth();
204 }
205
206 /**
207 * Set the width percentage of the form field.
208 *
209 * @param int $width The width percentage to set (20-100).
210 */
211 public function setWidth(int $width): void
212 {
213 $this->formField->width = $width;
214 }
215
216 /**
217 * Get the parent field ID.
218 *
219 * @return int|null The parent field ID.
220 */
221 public function getParentId(): ?int
222 {
223 return $this->formField->getParentId();
224 }
225
226 /**
227 * Set the parent field ID.
228 *
229 * @param int|null $parentId The parent field ID to set.
230 */
231 public function setParentId(?int $parentId): void
232 {
233 $this->formField->parentId = $parentId;
234 }
235
236 /**
237 * Get the page ID for multi-page forms.
238 *
239 * @return string|null The page ID.
240 */
241 public function getPageId(): ?string
242 {
243 return $this->formField->pageId;
244 }
245
246 /**
247 * Set the page ID for multi-page forms.
248 *
249 * @param string|null $pageId The page ID to set.
250 */
251 public function setPageId(?string $pageId): void
252 {
253 $this->formField->pageId = $pageId;
254 }
255
256 /**
257 * Set the number of rows (for textarea fields).
258 *
259 * @param int $rows
260 */
261 public function setRows(int $rows): void
262 {
263 $this->formField->setRows($rows);
264 }
265
266 /**
267 * Get the FieldGeneralSettings entity.
268 */
269 public function getFieldGeneralSettings(): FieldGeneralSettings
270 {
271 return $this->fieldGeneralSettings;
272 }
273
274 /**
275 * Get the FieldAdvancedSettings entity.
276 */
277 public function getFieldAdvancedSettings(): FieldAdvancedSettings
278 {
279 return $this->fieldAdvancedSettings;
280 }
281
282 /**
283 * Set an additional property
284 *
285 * @param string $key
286 * @param mixed $value
287 *
288 * @return void
289 */
290 public function setAdditionalProperty(string $key, $value): void
291 {
292 foreach ($this->additionalProperties as &$property) {
293 if (array_key_exists($key, $property)) {
294 $property[$key] = $value; // update existing
295 return;
296 }
297 }
298
299 // if not found, append new key-value pair
300 $this->additionalProperties[] = [$key => $value];
301 }
302
303 /**
304 * Get an additional property
305 *
306 * @param string $key
307 * @return mixed|null The value of the additional property, or null if not found
308 */
309 public function getAdditionalProperty(string $key)
310 {
311 foreach ($this->additionalProperties as $property) {
312 if (array_key_exists($key, $property)) {
313 return $property[$key];
314 }
315 }
316 return null;
317 }
318
319 /**
320 * Get all additional properties
321 *
322 * @return array<string, mixed> The additional properties
323 */
324 public function getAllAdditionalProperties(): array
325 {
326 return $this->additionalProperties;
327 }
328
329 /**
330 * Convert the form field entity to an array.
331 *
332 * @return array<string, mixed> The form field entity as an array.
333 */
334 public function toArray(): array
335 {
336 $data = array_merge(
337 [
338 'id' => $this->getId(),
339 'formId' => $this->getFormId(),
340 'fieldIndex' => $this->getIndex(),
341 'type' => $this->getType(),
342 'position' => $this->getPosition(),
343 'rowIndex' => $this->getRowIndex(),
344 'columnIndex' => $this->getColumnIndex(),
345 'width' => $this->getWidth(),
346 'parentId' => $this->getParentId(),
347 'pageId' => $this->getPageId(),
348 'rows' => $this->formField->getRows()
349 ],
350 $this->getFieldGeneralSettings()->toArray(),
351 $this->getFieldAdvancedSettings()->toArray()
352 );
353
354 /**
355 * Allows modification of the array representation of the Field entity.
356 * @since 0.1.0
357 *
358 * @param mixed[] $data The current array representation of the Field entity.
359 * @param Field $field The Field entity instance.
360 * @return mixed[] The modified array representation of the Field entity.
361 */
362 return apply_filters('ivyforms/field/entity/add_to_array_additional_properties', $data, $this);
363 }
364 }
365