PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Factory / Field / FieldFactory.php

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

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