PluginProbe
The Innovative Form Builder – IvyForms / 0.4
The Innovative Form Builder – IvyForms v0.4
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 / ValueObjects / Field / Field.php

Field.php in The Innovative Form Builder – IvyForms 0.4, at backend/src/ValueObjects/Field/Field.php

458 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © TMS-Plugins. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace IvyForms\ValueObjects\Field;
9
10 // phpcs:disable PSR1.Files.SideEffects
11 if (!defined('ABSPATH')) {
12 exit; // Exit if accessed directly
13 }
14
15 use IvyForms\Common\Exceptions\ValidationException;
16 use IvyForms\Services\Translations\BackendStrings;
17 use IvyForms\ValueObjects\Field\FieldGeneralSettings;
18 use IvyForms\ValueObjects\Field\FieldOptions;
19 use IvyForms\ValueObjects\Field\FieldAdvancedSettings;
20
21 /**
22 * Class Field
23 *
24 * @SuppressWarnings(PHPMD)
25 * @package IvyForms\ValueObjects\Field
26 */
27 final class Field
28 {
29 /**
30 * @var int
31 */
32 public int $id;
33 /**
34 * @var int
35 */
36 public int $formId;
37 /**
38 * @var int
39 */
40 public int $fieldIndex;
41 /**
42 * @var string
43 */
44 public string $type;
45 /**
46 * @var int
47 */
48 public int $position;
49 /**
50 * @var int|null
51 */
52 public ?int $parentId;
53 /**
54 * @var FieldGeneralSettings
55 */
56 public FieldGeneralSettings $generalSettings;
57 /**
58 * @var FieldOptions
59 */
60 public FieldOptions $fieldOptions;
61 /**
62 * @var int Number of rows (for textarea fields)
63 */
64 public int $rows;
65 /** @var bool */
66 public bool $confirmFieldEnabled;
67 /** @var string */
68 public string $confirmFieldLabel;
69 /** @var string */
70 public string $confirmFieldPlaceholder;
71 /** @var bool */
72 public bool $confirmFieldHideLabel;
73 /**
74 * @var FieldAdvancedSettings
75 */
76 public FieldAdvancedSettings $advancedSettings;
77
78 /**
79 * Field constructor.
80 * @SuppressWarnings(PHPMD)
81 *
82 * @param int $id
83 * @param int $formId
84 * @param int $fieldIndex
85 * @param string $type
86 * @param int $position
87 * @param int|null $parentId
88 * @param FieldGeneralSettings $generalSettings
89 * @param FieldOptions $fieldOptions
90 * @param int $rows
91 * @param bool $confirmFieldEnabled
92 * @param string $confirmFieldLabel
93 * @param string $confirmFieldPlaceholder
94 * @param bool $confirmFieldHideLabel
95 * @param FieldAdvancedSettings $advancedSettings
96 *
97 * @throws ValidationException
98 */
99 public function __construct(
100 int $id,
101 int $formId,
102 int $fieldIndex,
103 string $type,
104 int $position,
105 ?int $parentId = null,
106 FieldGeneralSettings $generalSettings,
107 FieldOptions $fieldOptions,
108 FieldAdvancedSettings $advancedSettings,
109 int $rows = 0,
110 bool $confirmFieldEnabled = false,
111 string $confirmFieldLabel = '',
112 string $confirmFieldPlaceholder = '',
113 bool $confirmFieldHideLabel = false
114 ) {
115 $this->id = $this->validateId($id);
116 $this->formId = $this->validateId($formId);
117 $this->fieldIndex = $this->validateId($fieldIndex);
118 $this->type = $this->validateString($type, 50, 'type');
119 $this->position = $this->validatePosition($position);
120 $this->parentId = $this->validateParentId($parentId);
121 $this->generalSettings = $generalSettings;
122 $this->fieldOptions = $fieldOptions;
123 $this->advancedSettings = $advancedSettings;
124 $this->rows = $this->validateRows($rows);
125 $this->confirmFieldEnabled = $this->validateBool($confirmFieldEnabled);
126 $this->confirmFieldLabel = $this->validateString($confirmFieldLabel, 255, 'confirmFieldLabel');
127 $this->confirmFieldPlaceholder = $this->validateString(
128 $confirmFieldPlaceholder,
129 255,
130 'confirmFieldPlaceholder'
131 );
132 $this->confirmFieldHideLabel = $this->validateBool($confirmFieldHideLabel);
133 }
134
135 /**
136 * Validates the ID.
137 *
138 * @param int $id
139 *
140 * @return int
141 * @throws ValidationException
142 */
143 public function validateId(int $id): int
144 {
145 if ($id < 0) {
146 throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']);
147 }
148 return $id;
149 }
150
151 /**
152 * Validates parent id which can be null or non-negative int.
153 *
154 * @param int|null $parentId
155 *
156 * @return int|null
157 * @throws ValidationException
158 */
159 private function validateParentId(?int $parentId): ?int
160 {
161 if ($parentId === null) {
162 return null;
163 }
164 if ($parentId < 0) {
165 throw new ValidationException(BackendStrings::getExceptionStrings()['parentId_positive_integer']);
166 }
167 return $parentId;
168 }
169
170 /**
171 * Validates a string value.
172 *
173 * @param string $value
174 * @param int $maxLength
175 * @param string $fieldName
176 *
177 * @return string
178 *
179 * @throws ValidationException
180 */
181 private function validateString(string $value, int $maxLength, string $fieldName): string
182 {
183 if (strlen($value) > $maxLength) {
184 throw new ValidationException(
185 sprintf(
186 /* translators: 1: String value, 2: String max length. */
187 esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
188 esc_html($fieldName),
189 $maxLength
190 )
191 );
192 }
193 return $value;
194 }
195
196 /**
197 * Validates the position.
198 *
199 * @param int $value
200 *
201 * @return int
202 *
203 * @throws ValidationException
204 */
205 private function validatePosition(int $value): int
206 {
207 if ($value < 0) {
208 throw new ValidationException(
209 esc_html__('position must be a non-negative integer.', 'ivyforms')
210 );
211 }
212 return $value;
213 }
214
215 /**
216 * Validates a boolean field.
217 *
218 * @param bool $value
219 *
220 * @return bool
221 */
222 private function validateBool(bool $value): bool
223 {
224 return $value;
225 }
226
227 /**
228 * Validate rows.
229 *
230 * @param int $rows
231 * @return int
232 * @throws ValidationException
233 */
234 private function validateRows(int $rows): int
235 {
236 if ($rows < 0) {
237 throw new ValidationException(
238 esc_html__('rows must be at least 1.', 'ivyforms')
239 );
240 }
241 return $rows;
242 }
243
244 /**
245 * Get the form ID.
246 *
247 * @return int
248 */
249 public function getFormId(): int
250 {
251 return $this->formId;
252 }
253
254 /**
255 * Get the field ID.
256 *
257 * @return int
258 */
259 public function getId(): int
260 {
261 return $this->id;
262 }
263
264 /**
265 * Get the field index.
266 *
267 * @return int
268 */
269 public function getIndex(): int
270 {
271 return $this->fieldIndex;
272 }
273
274 /**
275 * Get the field type.
276 *
277 * @return string
278 */
279 public function getType(): string
280 {
281 return $this->type;
282 }
283
284 /**
285 * Get the field position.
286 *
287 * @return int
288 */
289 public function getPosition(): int
290 {
291 return $this->position;
292 }
293
294 /**
295 * Get the parent field id.
296 *
297 * @return int|null
298 */
299 public function getParentId(): ?int
300 {
301 return $this->parentId;
302 }
303
304 /**
305 * Get the general settings object.
306 *
307 * @return FieldGeneralSettings
308 */
309 public function getGeneralSettings(): FieldGeneralSettings
310 {
311 return $this->generalSettings;
312 }
313
314 /**
315 * Get the field options.
316 *
317 * @return FieldOptions
318 */
319 public function getFieldOptions(): FieldOptions
320 {
321 return $this->fieldOptions;
322 }
323
324 /**
325 * Get number of rows.
326 * @return int
327 */
328 public function getRows(): int
329 {
330 return $this->rows;
331 }
332
333 /**
334 * Set number of rows.
335 * @param int $rows
336 */
337 public function setRows(int $rows): void
338 {
339 $this->rows = $rows;
340 }
341
342 /**
343 * Check if the confirm field feature is enabled.
344 *
345 * @return bool
346 */
347 public function isConfirmFieldEnabled(): bool
348 {
349 return $this->confirmFieldEnabled;
350 }
351
352 /**
353 * Get the confirm field label.
354 *
355 * @return string
356 */
357 public function getConfirmFieldLabel(): string
358 {
359 return $this->confirmFieldLabel;
360 }
361
362 /**
363 * Get the confirm field placeholder.
364 *
365 * @return string
366 */
367 public function getConfirmFieldPlaceholder(): string
368 {
369 return $this->confirmFieldPlaceholder;
370 }
371
372 /**
373 * Check if the confirm field label should be hidden.
374 *
375 * @return bool
376 */
377 public function isConfirmFieldHideLabel(): bool
378 {
379 return $this->confirmFieldHideLabel;
380 }
381
382 /**
383 * Set the confirm field feature enabled/disabled.
384 *
385 * @param bool $enabled
386 */
387 public function setConfirmFieldEnabled(bool $enabled): void
388 {
389 $this->confirmFieldEnabled = $enabled;
390 }
391
392 /**
393 * Set the confirm field label.
394 *
395 * @param string $label
396 */
397 public function setConfirmFieldLabel(string $label): void
398 {
399 $this->confirmFieldLabel = $label;
400 }
401
402 /**
403 * Set the confirm field placeholder.
404 *
405 * @param string $ph
406 */
407 public function setConfirmFieldPlaceholder(string $ph): void
408 {
409 $this->confirmFieldPlaceholder = $ph;
410 }
411
412 /**
413 * Set the confirm field label visibility.
414 *
415 * @param bool $hide
416 */
417 public function setConfirmFieldHideLabel(bool $hide): void
418 {
419 $this->confirmFieldHideLabel = $hide;
420 }
421
422 /**
423 * Get the general settings object.
424 *
425 * @return FieldAdvancedSettings
426 */
427 public function getAdvancedSettings(): FieldAdvancedSettings
428 {
429 return $this->advancedSettings;
430 }
431
432 /**
433 * Convert the field to an array.
434 *
435 * @return array<string, mixed>
436 */
437 public function toArray(): array
438 {
439 return [
440 'id' => $this->id,
441 'formId' => $this->formId,
442 'fieldIndex' => $this->fieldIndex,
443 'type' => $this->type,
444 'position' => $this->position,
445 'parentId' => $this->parentId,
446 'generalSettings' => $this->generalSettings->toArray(),
447 'fieldOptions' => $this->fieldOptions->toArray(),
448 'rows' => $this->rows,
449 'confirmFieldEnabled' => $this->confirmFieldEnabled,
450 'confirmFieldLabel' => $this->confirmFieldLabel,
451 'confirmFieldPlaceholder' => $this->confirmFieldPlaceholder,
452 'confirmFieldHideLabel' => $this->confirmFieldHideLabel,
453 // Advanced options
454 'advancedSettings' => $this->advancedSettings->toArray()
455 ];
456 }
457 }
458