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 / Factory / Field / FieldFactory.php

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

262 lines 9.6 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 use IvyForms\ValueObjects\Field\FieldGeneralSettings;
14 use IvyForms\ValueObjects\Field\FieldOptions;
15 use IvyForms\ValueObjects\Field\FieldAdvancedSettings;
16
17 /**
18 * Class FieldFactory
19 *
20 * @package IvyForms\Factory\Field
21 */
22 class FieldFactory
23 {
24 /**
25 * @param array<string, mixed> $data
26 *
27 * @return FieldEntity
28 *
29 * @throws ValidationException
30 */
31 public static function create(array $data): FieldEntity
32 {
33 $settings = isset($data['settings']) ? json_decode($data['settings'], true) : [];
34
35 if (!empty($settings)) {
36 $data['hideLabel'] = $settings['hideLabel'] ?? false;
37 $data['readOnly'] = $settings['readOnly'] ?? false;
38 $data['description'] = $settings['description'] ?? '';
39 $data['requiredMessage'] = $settings['requiredMessage'] ?? '';
40 $data['cssClasses'] = $settings['cssClasses'] ?? '';
41 $data['limitMaxLength'] = $settings['limitMaxLength'] ?? false;
42 $data['maxLength'] = $settings['maxLength'] ?? 255;
43 $data['labelPosition'] = $settings['labelPosition'] ?? 'default';
44 $data['noDuplicates'] = $settings['noDuplicates'] ?? false;
45 $data['shuffleOptions'] = $settings['shuffleOptions'] ?? false;
46 $data['showValues'] = $settings['showValues'] ?? false;
47 $data['enableSearch'] = $settings['enableSearch'] ?? false;
48 $data['rows'] = $settings['rows'] ?? 0;
49 $data['confirmFieldEnabled'] = $settings['confirmFieldEnabled'] ?? false;
50 $data['confirmFieldLabel'] = $settings['confirmFieldLabel'] ?? '';
51 $data['confirmFieldPlaceholder'] = $settings['confirmFieldPlaceholder'] ?? '';
52 $data['confirmFieldHideLabel'] = $settings['confirmFieldHideLabel'] ?? false;
53 $data['phoneFormat'] = $settings['phoneFormat'] ?? '';
54 $data['phoneAutoDetect'] = $settings['phoneAutoDetect'] ?? false;
55 $data['minValue'] = $settings['minValue'] ?? 0;
56 $data['maxValue'] = $settings['maxValue'] ?? 0;
57 $data['step'] = $settings['step'] ?? 1;
58 $data['numberFormat'] = $settings['numberFormat'] ?? '';
59 // Robust boolean conversion for visible (matches hideLabel logic)
60 $data['visible'] = array_key_exists('visible', $settings)
61 ? filter_var($settings['visible'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
62 : true;
63 if ($data['visible'] === null) {
64 $data['visible'] = true;
65 }
66 }
67 $generalSettings = new FieldGeneralSettings(
68 self::validateString($data['label'] ?? '', 255, 'label'),
69 self::validateBool($data['required'] ?? false),
70 self::validateString($data['placeholder'] ?? '', 255, 'placeholder'),
71 self::validateBool($data['hideLabel'] ?? false),
72 self::validateBool($data['readOnly'] ?? false),
73 self::validateString($data['description'] ?? '', 255, 'description'),
74 self::validateString($data['requiredMessage'] ?? '', 255, 'requiredMessage'),
75 self::validateString($data['cssClasses'] ?? '', 255, 'cssClasses'),
76 self::validateBool($data['shuffleOptions'] ?? false),
77 self::validateBool($data['showValues'] ?? false),
78 self::validateBool($data['enableSearch'] ?? false),
79 self::validatePhoneFormat($data['phoneFormat'] ?? ''),
80 self::validateBool($data['phoneAutoDetect'] ?? false),
81 self::validateNumberValue($data['minValue'] ?? 0.0, 'minValue'),
82 self::validateNumberValue($data['maxValue'] ?? 0.0, 'maxValue'),
83 self::validateStep($data['step'] ?? 1),
84 self::validateNumberFormat($data['numberFormat'] ?? ''),
85 self::validateBool($data['visible'] ?? true)
86 );
87 $advancedSettings = new FieldAdvancedSettings(
88 self::validateString($data['defaultValue'] ?? '', 255, 'defaultValue'),
89 self::validateBool($data['limitMaxLength'] ?? false),
90 (int)($data['maxLength'] ?? 255),
91 self::validateString($data['labelPosition'] ?? '', 255, 'labelPosition'),
92 self::validateBool($data['noDuplicates'] ?? false),
93 );
94 $fieldOptions = new FieldOptions($data['options'] ?? []);
95
96 $fieldObject = new Field(
97 $data['id'] ?? 0,
98 $data['formId'] ?? 0,
99 $data['fieldIndex'] ?? 0,
100 $data['type'] ?? '',
101 $data['position'] ?? 0,
102 $data['parentId'] ?? null,
103 $generalSettings,
104 $fieldOptions,
105 $advancedSettings,
106 $data['rows'] ?? 0,
107 $data['confirmFieldEnabled'] ?? false,
108 $data['confirmFieldLabel'] ?? '',
109 $data['confirmFieldPlaceholder'] ?? '',
110 $data['confirmFieldHideLabel'] ?? false
111 );
112
113 $field = new FieldEntity($fieldObject);
114
115 if (isset($data['id'])) {
116 $field->setId($data['id']);
117 }
118
119 if (isset($data['formId'])) {
120 $field->setFormId($data['formId']);
121 }
122
123 if (isset($data['fieldIndex'])) {
124 $field->setIndex($data['fieldIndex']);
125 }
126
127 if (isset($data['type'])) {
128 $field->setType($data['type']);
129 }
130
131 if (isset($data['position'])) {
132 $field->setPosition($data['position']);
133 }
134
135 if (isset($data['parentId'])) {
136 $field->setParentId($data['parentId']);
137 }
138
139 if (isset($data['rows'])) {
140 $field->setRows((int)$data['rows']);
141 }
142 if (isset($data['confirmFieldEnabled'])) {
143 $field->setConfirmFieldEnabled((bool)$data['confirmFieldEnabled']);
144 }
145 if (isset($data['confirmFieldLabel'])) {
146 $field->setConfirmFieldLabel($data['confirmFieldLabel']);
147 }
148 if (isset($data['confirmFieldPlaceholder'])) {
149 $field->setConfirmFieldPlaceholder($data['confirmFieldPlaceholder']);
150 }
151 if (isset($data['confirmFieldHideLabel'])) {
152 $field->setConfirmFieldHideLabel((bool)$data['confirmFieldHideLabel']);
153 }
154
155 return $field;
156 }
157
158 /**
159 * Validate and convert a value to boolean.
160 *
161 * @param mixed $value The value to validate.
162 *
163 * @return bool The validated boolean value.
164 */
165 private static function validateBool($value): bool
166 {
167 return (bool)$value;
168 }
169
170 /**
171 * Validate a string's length.
172 *
173 * @param mixed $value The value to validate.
174 * @param int $maxLength The maximum allowed length.
175 * @param string $fieldName The name of the field (for error messages).
176 *
177 * @return string The validated string.
178 *
179 * @throws ValidationException If the string exceeds the maximum length.
180 */
181 private static function validateString($value, int $maxLength, string $fieldName): string
182 {
183 $value = (string)$value;
184 if (strlen($value) > $maxLength) {
185 throw new ValidationException(
186 sprintf(
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 * Validate and coerce numeric range values (minValue / maxValue).
197 * If value is null/empty/non-numeric => returns 0.0.
198 * Performs cross-check when the counterpart is already set.
199 *
200 * @param mixed $value
201 * @param string $fieldName 'minValue' | 'maxValue'
202 * @return float
203 * @throws ValidationException
204 */
205 private static function validateNumberValue($value, string $fieldName): float
206 {
207 if ($value === '' || $value === null) {
208 return 0.0;
209 }
210 if (!is_numeric($value)) {
211 throw new ValidationException(
212 sprintf(
213 esc_html__('%1$s must be a number.', 'ivyforms'),
214 esc_html($fieldName)
215 )
216 );
217 }
218 return (float)$value;
219 }
220
221 /**
222 * Validate step (positive number >0)
223 * @param float $value
224 * @return float
225 * @throws ValidationException
226 */
227 private static function validateStep(float $value): float
228 {
229 if ($value <= 0) {
230 throw new ValidationException(esc_html__('step must be a positive number (>0).', 'ivyforms'));
231 }
232 return (float)$value;
233 }
234
235 /**
236 * Validates the number format.
237 *
238 * @param string $value
239 *
240 * @return string
241 *
242 */
243 private static function validateNumberFormat(string $value): string
244 {
245 $allowed = ['', 'us_decimal', 'us_int', 'eu_decimal', 'eu_int'];
246 if (!in_array($value, $allowed, true)) {
247 return '';
248 }
249 return $value;
250 }
251
252 private static function validatePhoneFormat(string $format): string
253 {
254 $allowed = ['international','national','e164'];
255 $lower = strtolower($format);
256 if (!in_array($lower, $allowed, true)) {
257 return 'international';
258 }
259 return $lower;
260 }
261 }
262