PluginProbe
The Innovative Form Builder – IvyForms / 0.6.1.1
The Innovative Form Builder – IvyForms v0.6.1.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 0.6.1.1, at backend/src/Entity/Field/Field.php

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