PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
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 1.0, at backend/src/Factory/Field/FieldFactory.php

336 lines 14.4 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 use IvyForms\Common\Exceptions\ValidationException;
6 use IvyForms\Entity\Field\Field as FieldEntity;
7 use IvyForms\ValueObjects\Field\Field;
8 use IvyForms\ValueObjects\Field\FieldGeneralSettings;
9 use IvyForms\ValueObjects\Field\FieldOptions;
10 use IvyForms\ValueObjects\Field\FieldAdvancedSettings;
11 use IvyForms\ValueObjects\Field\ConfirmationGeneralSettings;
12 use IvyForms\ValueObjects\Field\TimeFieldGeneralSettings;
13 use IvyForms\ValueObjects\Field\DateFieldGeneralSettings;
14
15 /**
16 * Class FieldFactory
17 *
18 * @package IvyForms\Factory\Field
19 */
20 class FieldFactory
21 {
22 /**
23 * @param array<string, mixed> $data
24 *
25 * @return FieldEntity
26 *
27 * @throws ValidationException
28 */
29 public static function create(array $data): FieldEntity
30 {
31 $settings = isset($data['settings']) ? json_decode($data['settings'], true) : [];
32
33 if (!empty($settings)) {
34 $data['hideLabel'] = $settings['hideLabel'] ?? false;
35 $data['readOnly'] = $settings['readOnly'] ?? false;
36 $data['description'] = $settings['description'] ?? '';
37 $data['requiredMessage'] = $settings['requiredMessage'] ?? '';
38 $data['cssClasses'] = $settings['cssClasses'] ?? '';
39 $data['limitMaxLength'] = $settings['limitMaxLength'] ?? false;
40 $data['limitRange'] = $settings['limitRange'] ?? false;
41 $data['maxLength'] = $settings['maxLength'] ?? 255;
42 $data['labelPosition'] = $settings['labelPosition'] ?? 'default';
43 $data['noDuplicates'] = $settings['noDuplicates'] ?? false;
44 $data['shuffleOptions'] = $settings['shuffleOptions'] ?? false;
45 $data['showValues'] = $settings['showValues'] ?? false;
46 $data['enableSearch'] = $settings['enableSearch'] ?? false;
47 $data['rows'] = $settings['rows'] ?? 0;
48 $data['confirmFieldEnabled'] = $settings['confirmFieldEnabled'] ?? false;
49 $data['confirmFieldLabel'] = $settings['confirmFieldLabel'] ?? '';
50 $data['confirmFieldPlaceholder'] = $settings['confirmFieldPlaceholder'] ?? '';
51 $data['confirmFieldHideLabel'] = $settings['confirmFieldHideLabel'] ?? false;
52 $data['phoneFormat'] = $settings['phoneFormat'] ?? '';
53 $data['phoneAutoDetect'] = $settings['phoneAutoDetect'] ?? false;
54 $data['minValue'] = $settings['minValue'] ?? null;
55 $data['maxValue'] = $settings['maxValue'] ?? null;
56 $data['step'] = $settings['step'] ?? 1;
57 $data['numberFormat'] = $settings['numberFormat'] ?? '';
58 $data['inputPrefix'] = $settings['inputPrefix'] ?? '';
59 $data['inputSuffix'] = $settings['inputSuffix'] ?? '';
60 $data['timeFieldType'] = $settings['timeFieldType'] ?? '';
61 $data['timeFormat'] = $settings['timeFormat'] ?? '';
62 $data['minTimeValue'] = $settings['minTimeValue'] ?? '';
63 $data['maxTimeValue'] = $settings['maxTimeValue'] ?? '';
64 $data['dateFieldType'] = $settings['dateFieldType'] ?? '';
65 $data['dateFormat'] = $settings['dateFormat'] ?? '';
66 $data['minDateValue'] = $settings['minDateValue'] ?? '';
67 $data['maxDateValue'] = $settings['maxDateValue'] ?? '';
68 $data['showRatingText'] = $settings['showRatingText'] ?? false;
69 $data['ratingIcon'] = $settings['ratingIcon'] ?? 'star';
70 $data['htmlContent'] = $settings['htmlContent'] ?? '';
71 $data['limitDateRange'] = $settings['limitDateRange'] ?? false;
72 $data['limitTimeRange'] = $settings['limitTimeRange'] ?? false;
73 // Grid layout: prioritize direct database columns over settings JSON
74 // This ensures backward compatibility while preferring the dedicated columns
75 $data['rowIndex'] = $data['rowIndex'] ?? ($settings['rowIndex'] ?? 0);
76 $data['columnIndex'] = $data['columnIndex'] ?? ($settings['columnIndex'] ?? 0);
77 $data['width'] = $data['width'] ?? ($settings['width'] ?? 100);
78 $data['visible'] = array_key_exists('visible', $settings)
79 ? filter_var($settings['visible'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
80 : true;
81 if ($data['visible'] === null) {
82 $data['visible'] = true;
83 }
84 }
85
86 /**
87 * Allows modification of the settings data before creating the Field entity.
88 * This filter runs for both:
89 * - Fields loaded from database (settings from JSON string)
90 * - Fields from API (settings from direct properties)
91 * @since 0.1.0
92 *
93 * @param mixed[] $data The current field data.
94 * @param mixed[] $settings The settings array (from database or empty for API).
95 * @return mixed[] The modified field data.
96 */
97 $data = apply_filters('ivyforms/field/factory/extract_settings', $data, $settings);
98 $generalSettings = new FieldGeneralSettings(
99 self::validateString($data['label'] ?? '', 255, 'label'),
100 self::validateBool($data['required'] ?? false),
101 self::validateString($data['placeholder'] ?? '', 255, 'placeholder'),
102 self::validateBool($data['hideLabel'] ?? false),
103 self::validateBool($data['readOnly'] ?? false),
104 self::validateString($data['description'] ?? '', 1000, 'description'),
105 self::validateString($data['requiredMessage'] ?? '', 255, 'requiredMessage'),
106 self::validateString($data['cssClasses'] ?? '', 255, 'cssClasses'),
107 self::validateBool($data['shuffleOptions'] ?? false),
108 self::validateBool($data['showValues'] ?? false),
109 self::validateBool($data['enableSearch'] ?? false),
110 self::validatePhoneFormat($data['phoneFormat'] ?? ''),
111 self::validateBool($data['phoneAutoDetect'] ?? false),
112 self::validateNumberValue($data['minValue'] ?? null, 'minValue'),
113 self::validateNumberValue($data['maxValue'] ?? null, 'maxValue'),
114 self::validateStep($data['step'] ?? 1),
115 self::validateNumberFormat($data['numberFormat'] ?? ''),
116 new ConfirmationGeneralSettings(
117 self::validateBool($data['confirmFieldEnabled'] ?? false),
118 self::validateString(
119 $data['confirmFieldLabel'] ?? '',
120 255,
121 'confirmFieldLabel'
122 ),
123 self::validateString(
124 $data['confirmFieldPlaceholder'] ?? '',
125 255,
126 'confirmFieldPlaceholder'
127 ),
128 self::validateBool($data['confirmFieldHideLabel'] ?? false)
129 ),
130 new TimeFieldGeneralSettings(
131 self::validateBool($data['limitTimeRange'] ?? false),
132 self::validateString($data['timeFieldType'] ?? '', 50, 'timeFieldType'),
133 self::validateString($data['timeFormat'] ?? '', 50, 'timeFormat'),
134 isset($data['minTimeValue']) && $data['minTimeValue'] !== '' ?
135 self::validateString($data['minTimeValue'], 255, 'minTimeValue') : null,
136 isset($data['maxTimeValue']) && $data['maxTimeValue'] !== '' ?
137 self::validateString($data['maxTimeValue'], 255, 'maxTimeValue') : null,
138 ),
139 new DateFieldGeneralSettings(
140 self::validateBool($data['limitDateRange'] ?? false),
141 self::validateString($data['dateFieldType'] ?? '', 50, 'dateFieldType'),
142 self::validateString($data['dateFormat'] ?? '', 50, 'dateFormat'),
143 isset($data['minDateValue']) && $data['minDateValue'] !== '' ?
144 self::validateString($data['minDateValue'], 255, 'minDateValue') : null,
145 isset($data['maxDateValue']) && $data['maxDateValue'] !== '' ?
146 self::validateString($data['maxDateValue'], 255, 'maxDateValue') : null,
147 ),
148 self::validateBool($data['showRatingText'] ?? false),
149 self::validateBool($data['visible'] ?? true),
150 self::validateString($data['htmlContent'] ?? '', 4294967295, 'htmlContent'),
151 self::validateBool($data['limitRange'] ?? false),
152 );
153 $advancedSettings = new FieldAdvancedSettings(
154 self::validateString($data['defaultValue'] ?? '', 255, 'defaultValue'),
155 self::validateBool($data['limitMaxLength'] ?? false),
156 (int)($data['maxLength'] ?? 255),
157 self::validateString($data['labelPosition'] ?? '', 255, 'labelPosition'),
158 self::validateBool($data['noDuplicates'] ?? false),
159 self::validateString($data['inputPrefix'] ?? '', 255, 'inputPrefix'),
160 self::validateString($data['inputSuffix'] ?? '', 255, 'inputSuffix'),
161 self::validateString($data['ratingIcon'] ?? 'star', 50, 'ratingIcon')
162 );
163 $fieldOptions = new FieldOptions($data['options'] ?? []);
164
165 $fieldObject = new Field(
166 $data['id'] ?? 0,
167 $data['formId'] ?? 0,
168 $data['fieldIndex'] ?? 0,
169 $data['type'] ?? '',
170 $data['position'] ?? 0,
171 $generalSettings,
172 $fieldOptions,
173 $advancedSettings,
174 $data['parentId'] ?? null,
175 $data['rows'] ?? 0,
176 $data['rowIndex'] ?? 0,
177 $data['columnIndex'] ?? 0,
178 $data['width'] ?? 100
179 );
180
181 /**
182 * Allows setting of additional properties for the Field value object.
183 * @since 0.1.0
184 *
185 * @param Field $fieldObject The current field data.
186 * @param mixed[] $data The settings array from the database.
187 * @return mixed[] The modified field data.
188 */
189 $fieldObject = apply_filters('ivyforms/field/value_object/set_properties', $fieldObject, $data);
190
191 $field = new FieldEntity($fieldObject);
192
193 if (isset($data['id'])) {
194 $field->setId($data['id']);
195 }
196
197 if (isset($data['formId'])) {
198 $field->setFormId($data['formId']);
199 }
200
201 if (isset($data['fieldIndex'])) {
202 $field->setIndex($data['fieldIndex']);
203 }
204
205 if (isset($data['type'])) {
206 $field->setType($data['type']);
207 }
208
209 if (isset($data['position'])) {
210 $field->setPosition($data['position']);
211 }
212
213 if (isset($data['parentId'])) {
214 $field->setParentId($data['parentId']);
215 }
216
217 if (isset($data['rows'])) {
218 $field->setRows((int)$data['rows']);
219 }
220
221 /**
222 * Allows setting of additional properties for the Field entity.
223 * @since 0.1.0
224 *
225 * @param FieldEntity $field The current field data.
226 * @param mixed[] $data The settings array from the database.
227 * @return mixed[] The modified field data.
228 */
229 return apply_filters('ivyforms/field/entity/set_properties', $field, $data);
230 }
231
232 /**
233 * Validate and convert a value to boolean.
234 *
235 * @param mixed $value The value to validate.
236 *
237 * @return bool The validated boolean value.
238 */
239 private static function validateBool($value): bool
240 {
241 return (bool)$value;
242 }
243
244 /**
245 * Validate a string's length.
246 *
247 * @param mixed $value The value to validate.
248 * @param int $maxLength The maximum allowed length.
249 * @param string $fieldName The name of the field (for error messages).
250 *
251 * @return string The validated string.
252 *
253 * @throws ValidationException If the string exceeds the maximum length.
254 */
255 private static function validateString($value, int $maxLength, string $fieldName): string
256 {
257 $value = (string)$value;
258 if (strlen($value) > $maxLength) {
259 throw new ValidationException(
260 sprintf(
261 esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
262 esc_html($fieldName),
263 $maxLength
264 )
265 );
266 }
267 return $value;
268 }
269 /**
270 * Validate and coerce numeric range values (minValue / maxValue).
271 * If value is null/empty/non-numeric => returns 0.0.
272 * Performs cross-check when the counterpart is already set.
273 *
274 * @param mixed $value
275 * @param string $fieldName 'minValue' | 'maxValue'
276 * @return float | null
277 * @throws ValidationException
278 */
279 private static function validateNumberValue($value, string $fieldName): ?float
280 {
281 if ($value === '' || $value === null) {
282 return null;
283 }
284 if (!is_numeric($value)) {
285 throw new ValidationException(
286 sprintf(
287 esc_html__('%1$s must be a number.', 'ivyforms'),
288 esc_html($fieldName)
289 )
290 );
291 }
292 return (float)$value;
293 }
294
295 /**
296 * Validate step (positive number >0)
297 * @param float $value
298 * @return float
299 * @throws ValidationException
300 */
301 private static function validateStep(float $value): float
302 {
303 if ($value <= 0) {
304 throw new ValidationException(esc_html__('step must be a positive number (>0).', 'ivyforms'));
305 }
306 return (float)$value;
307 }
308
309 /**
310 * Validates the number format.
311 *
312 * @param string $value
313 *
314 * @return string
315 *
316 */
317 private static function validateNumberFormat(string $value): string
318 {
319 $allowed = ['', 'us_decimal', 'us_int', 'eu_decimal', 'eu_int'];
320 if (!in_array($value, $allowed, true)) {
321 return '';
322 }
323 return $value;
324 }
325
326 private static function validatePhoneFormat(string $format): string
327 {
328 $allowed = ['international','national','e164'];
329 $lower = strtolower($format);
330 if (!in_array($lower, $allowed, true)) {
331 return 'international';
332 }
333 return $lower;
334 }
335 }
336