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

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