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

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