PluginProbe
The Innovative Form Builder – IvyForms / 0.3.1
The Innovative Form Builder – IvyForms v0.3.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 / Factory / Field / FieldFactory.php

FieldFactory.php in The Innovative Form Builder – IvyForms 0.3.1, at backend/src/Factory/Field/FieldFactory.php

123 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\Factory\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\ValidationException;
11 use IvyForms\Entity\Field\Field as FieldEntity;
12 use IvyForms\ValueObjects\Field\Field;
13
14 /**
15 * Class FieldFactory
16 *
17 * @package IvyForms\Factory\Field
18 */
19 class FieldFactory
20 {
21 /**
22 * @param array<string, mixed> $data
23 *
24 * @return FieldEntity
25 *
26 * @throws ValidationException
27 */
28 public static function create(array $data): FieldEntity
29 {
30 $settings = isset($data['settings']) ? json_decode($data['settings'], true) : [];
31
32 if (!empty($settings)) {
33 $data['hideLabel'] = $settings['hideLabel'] ?? false;
34 $data['readOnly'] = $settings['readOnly'] ?? false;
35 $data['description'] = $settings['description'] ?? '';
36 $data['requiredMessage'] = $settings['requiredMessage'] ?? '';
37 $data['cssClasses'] = $settings['cssClasses'] ?? '';
38 }
39
40 $fieldObject = new Field(
41 $data['id'] ?? 0,
42 $data['formId'] ?? 0,
43 $data['fieldIndex'] ?? 0,
44 $data['type'] ?? '',
45 $data['label'] ?? '',
46 $data['defaultValue'] ?? '',
47 $data['required'] ?? false,
48 $data['placeholder'] ?? '',
49 $data['position'] ?? 0,
50 $data['hideLabel'] ?? false,
51 $data['readOnly'] ?? false,
52 $data['description'] ?? '',
53 $data['requiredMessage'] ?? '',
54 $data['cssClasses'] ?? ''
55 );
56
57 $field = new FieldEntity($fieldObject);
58
59 if (isset($data['id'])) {
60 $field->setId($data['id']);
61 }
62
63 if (isset($data['formId'])) {
64 $field->setFormId($data['formId']);
65 }
66
67 if (isset($data['fieldIndex'])) {
68 $field->setIndex($data['fieldIndex']);
69 }
70
71 if (isset($data['type'])) {
72 $field->setType($data['type']);
73 }
74
75 if (isset($data['label'])) {
76 $field->setLabel($data['label']);
77 }
78
79 if (isset($data['defaultValue'])) {
80 $field->setDefaultValue($data['defaultValue']);
81 }
82
83 if (isset($data['required'])) {
84 $field->setRequired($data['required']);
85 }
86
87 if (isset($data['placeholder'])) {
88 $field->setPlaceholder($data['placeholder']);
89 }
90
91 if (isset($data['position'])) {
92 $field->setPosition($data['position']);
93 }
94
95 // Set hideLabel if provided directly
96 if (isset($data['hideLabel'])) {
97 $field->setHideLabel($data['hideLabel']);
98 }
99
100 // Set readOnly if provided directly
101 if (isset($data['readOnly'])) {
102 $field->setReadOnly($data['readOnly']);
103 }
104
105 // Set description if provided directly
106 if (isset($data['description'])) {
107 $field->setDescription($data['description']);
108 }
109
110 // Set requiredMessage if provided directly
111 if (isset($data['requiredMessage'])) {
112 $field->setRequiredMessage($data['requiredMessage']);
113 }
114
115 // Set cssClasses if provided directly
116 if (isset($data['cssClasses'])) {
117 $field->setCssClasses($data['cssClasses']);
118 }
119
120 return $field;
121 }
122 }
123