| @@ -1,0 +1,438 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace IvyForms\Factory\Field; | |
| 4 | + | |
| 5 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 6 | +use IvyForms\Common\Helpers\NameFieldKeyHelper; | |
| 7 | +use IvyForms\Entity\Field\Field as FieldEntity; | |
| 8 | +use IvyForms\ValueObjects\Field\Field; | |
| 9 | +use IvyForms\ValueObjects\Field\FieldGeneralSettings; | |
| 10 | +use IvyForms\ValueObjects\Field\FieldOptions; | |
| 11 | +use IvyForms\ValueObjects\Field\FieldAdvancedSettings; | |
| 12 | +use IvyForms\ValueObjects\Field\FileUploadFieldAdvancedSettings; | |
| 13 | +use IvyForms\ValueObjects\Field\ConfirmationGeneralSettings; | |
| 14 | +use IvyForms\ValueObjects\Field\TimeFieldGeneralSettings; | |
| 15 | +use IvyForms\ValueObjects\Field\DateFieldGeneralSettings; | |
| 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 = self::parseSettings($data['settings'] ?? null); | |
| 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['limitRange'] = $settings['limitRange'] ?? false; | |
| 43 | + $data['maxLength'] = $settings['maxLength'] ?? 255; | |
| 44 | + $data['labelPosition'] = $settings['labelPosition'] ?? 'default'; | |
| 45 | + $data['noDuplicates'] = $settings['noDuplicates'] ?? false; | |
| 46 | + $data['shuffleOptions'] = $settings['shuffleOptions'] ?? false; | |
| 47 | + $data['showValues'] = $settings['showValues'] ?? false; | |
| 48 | + $data['enableSearch'] = $settings['enableSearch'] ?? false; | |
| 49 | + $data['rows'] = $settings['rows'] ?? 0; | |
| 50 | + $data['confirmFieldEnabled'] = $settings['confirmFieldEnabled'] ?? false; | |
| 51 | + $data['confirmFieldLabel'] = $settings['confirmFieldLabel'] ?? ''; | |
| 52 | + $data['confirmFieldPlaceholder'] = $settings['confirmFieldPlaceholder'] ?? ''; | |
| 53 | + $data['confirmFieldHideLabel'] = $settings['confirmFieldHideLabel'] ?? false; | |
| 54 | + $data['phoneFormat'] = $settings['phoneFormat'] ?? ''; | |
| 55 | + $data['phoneAutoDetect'] = $settings['phoneAutoDetect'] ?? false; | |
| 56 | + $data['minValue'] = $settings['minValue'] ?? null; | |
| 57 | + $data['maxValue'] = $settings['maxValue'] ?? null; | |
| 58 | + $data['step'] = $settings['step'] ?? 1; | |
| 59 | + $data['showGrid'] = $settings['showGrid'] ?? false; | |
| 60 | + $data['numberFormat'] = $settings['numberFormat'] ?? ''; | |
| 61 | + $data['inputPrefix'] = $settings['inputPrefix'] ?? ''; | |
| 62 | + $data['inputSuffix'] = $settings['inputSuffix'] ?? ''; | |
| 63 | + $data['timeFieldType'] = $settings['timeFieldType'] ?? ''; | |
| 64 | + $data['timeFormat'] = $settings['timeFormat'] ?? ''; | |
| 65 | + $data['minTimeValue'] = $settings['minTimeValue'] ?? ''; | |
| 66 | + $data['maxTimeValue'] = $settings['maxTimeValue'] ?? ''; | |
| 67 | + $data['dateFieldType'] = $settings['dateFieldType'] ?? ''; | |
| 68 | + $data['dateFormat'] = $settings['dateFormat'] ?? ''; | |
| 69 | + $data['minDateValue'] = $settings['minDateValue'] ?? ''; | |
| 70 | + $data['maxDateValue'] = $settings['maxDateValue'] ?? ''; | |
| 71 | + $data['showRatingText'] = $settings['showRatingText'] ?? false; | |
| 72 | + $data['ratingIcon'] = $settings['ratingIcon'] ?? 'star'; | |
| 73 | + $data['htmlContent'] = $settings['htmlContent'] ?? ''; | |
| 74 | + $data['agreementText'] = $settings['agreementText'] ?? ''; | |
| 75 | + $data['limitDateRange'] = $settings['limitDateRange'] ?? false; | |
| 76 | + $data['limitTimeRange'] = $settings['limitTimeRange'] ?? false; | |
| 77 | + $data['maxFileSizeMb'] = isset($settings['maxFileSizeMb']) ? (int) $settings['maxFileSizeMb'] : 0; | |
| 78 | + $data['saveUploadsTo'] = isset($settings['saveUploadsTo']) | |
| 79 | + ? (string) $settings['saveUploadsTo'] | |
| 80 | + : 'ivyforms'; | |
| 81 | + if (!array_key_exists('allowedFileExtensions', $data) && isset($settings['allowedFileExtensions'])) { | |
| 82 | + $data['allowedFileExtensions'] = $settings['allowedFileExtensions']; | |
| 83 | + } | |
| 84 | + // Grid layout: prioritize direct database columns over settings JSON | |
| 85 | + // This ensures backward compatibility while preferring the dedicated columns | |
| 86 | + $data['rowIndex'] = $data['rowIndex'] ?? ($settings['rowIndex'] ?? 0); | |
| 87 | + $data['columnIndex'] = $data['columnIndex'] ?? ($settings['columnIndex'] ?? 0); | |
| 88 | + $data['width'] = $data['width'] ?? ($settings['width'] ?? 100); | |
| 89 | + $data['visible'] = array_key_exists('visible', $settings) | |
| 90 | + ? filter_var($settings['visible'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) | |
| 91 | + : true; | |
| 92 | + if ($data['visible'] === null) { | |
| 93 | + $data['visible'] = true; | |
| 94 | + } | |
| 95 | + if (isset($settings['nameFieldType'])) { | |
| 96 | + $data['nameFieldType'] = (string)$settings['nameFieldType']; | |
| 97 | + } | |
| 98 | + if (array_key_exists('subFieldIndex', $settings)) { | |
| 99 | + $data['subFieldIndex'] = is_numeric($settings['subFieldIndex']) | |
| 100 | + ? (int)$settings['subFieldIndex'] | |
| 101 | + : null; | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + if (isset($data['nameFieldType'])) { | |
| 106 | + $data['nameFieldType'] = (string)$data['nameFieldType']; | |
| 107 | + } | |
| 108 | + if (array_key_exists('subFieldIndex', $data)) { | |
| 109 | + $data['subFieldIndex'] = is_numeric($data['subFieldIndex']) | |
| 110 | + ? (int)$data['subFieldIndex'] | |
| 111 | + : null; | |
| 112 | + } | |
| 113 | + | |
| 114 | + if (empty($data['nameFieldType'])) { | |
| 115 | + $inferredNameFieldType = NameFieldKeyHelper::inferNameFieldKeyFromLabel($data['label'] ?? ''); | |
| 116 | + if ($inferredNameFieldType !== null) { | |
| 117 | + $data['nameFieldType'] = $inferredNameFieldType; | |
| 118 | + } | |
| 119 | + } | |
| 120 | + | |
| 121 | + /** | |
| 122 | + * Allows modification of the settings data before creating the Field entity. | |
| 123 | + * This filter runs for both: | |
| 124 | + * - Fields loaded from database (settings from JSON string) | |
| 125 | + * - Fields from API (settings from direct properties) | |
| 126 | + * @since 0.1.0 | |
| 127 | + * | |
| 128 | + * @param mixed[] $data The current field data. | |
| 129 | + * @param mixed[] $settings The settings array (from database or empty for API). | |
| 130 | + * @return mixed[] The modified field data. | |
| 131 | + */ | |
| 132 | + $data = apply_filters('ivyforms/field/factory/extract_settings', $data, $settings); | |
| 133 | + | |
| 134 | + $generalSettings = new FieldGeneralSettings( | |
| 135 | + self::validateString($data['label'] ?? '', 255, 'label'), | |
| 136 | + self::validateBool($data['required'] ?? false), | |
| 137 | + self::validateString($data['placeholder'] ?? '', 255, 'placeholder'), | |
| 138 | + self::validateBool($data['hideLabel'] ?? false), | |
| 139 | + self::validateBool($data['readOnly'] ?? false), | |
| 140 | + self::validateString($data['description'] ?? '', 1000, 'description'), | |
| 141 | + self::validateString($data['requiredMessage'] ?? '', 255, 'requiredMessage'), | |
| 142 | + self::validateString($data['cssClasses'] ?? '', 255, 'cssClasses'), | |
| 143 | + self::validateBool($data['shuffleOptions'] ?? false), | |
| 144 | + self::validateBool($data['showValues'] ?? false), | |
| 145 | + self::validateBool($data['enableSearch'] ?? false), | |
| 146 | + self::validatePhoneFormat($data['phoneFormat'] ?? ''), | |
| 147 | + self::validateBool($data['phoneAutoDetect'] ?? false), | |
| 148 | + self::validateNumberValue($data['minValue'] ?? null, 'minValue'), | |
| 149 | + self::validateNumberValue($data['maxValue'] ?? null, 'maxValue'), | |
| 150 | + self::validateStep($data['step'] ?? 1), | |
| 151 | + self::validateBool($data['showGrid'] ?? false), | |
| 152 | + self::validateNumberFormat($data['numberFormat'] ?? ''), | |
| 153 | + new ConfirmationGeneralSettings( | |
| 154 | + self::validateBool($data['confirmFieldEnabled'] ?? false), | |
| 155 | + self::validateString( | |
| 156 | + $data['confirmFieldLabel'] ?? '', | |
| 157 | + 255, | |
| 158 | + 'confirmFieldLabel' | |
| 159 | + ), | |
| 160 | + self::validateString( | |
| 161 | + $data['confirmFieldPlaceholder'] ?? '', | |
| 162 | + 255, | |
| 163 | + 'confirmFieldPlaceholder' | |
| 164 | + ), | |
| 165 | + self::validateBool($data['confirmFieldHideLabel'] ?? false) | |
| 166 | + ), | |
| 167 | + new TimeFieldGeneralSettings( | |
| 168 | + self::validateBool($data['limitTimeRange'] ?? false), | |
| 169 | + self::validateString($data['timeFieldType'] ?? '', 50, 'timeFieldType'), | |
| 170 | + self::validateString($data['timeFormat'] ?? '', 50, 'timeFormat'), | |
| 171 | + isset($data['minTimeValue']) && $data['minTimeValue'] !== '' ? | |
| 172 | + self::validateString($data['minTimeValue'], 255, 'minTimeValue') : null, | |
| 173 | + isset($data['maxTimeValue']) && $data['maxTimeValue'] !== '' ? | |
| 174 | + self::validateString($data['maxTimeValue'], 255, 'maxTimeValue') : null, | |
| 175 | + ), | |
| 176 | + new DateFieldGeneralSettings( | |
| 177 | + self::validateBool($data['limitDateRange'] ?? false), | |
| 178 | + self::validateString($data['dateFieldType'] ?? '', 50, 'dateFieldType'), | |
| 179 | + self::validateString($data['dateFormat'] ?? '', 50, 'dateFormat'), | |
| 180 | + isset($data['minDateValue']) && $data['minDateValue'] !== '' ? | |
| 181 | + self::validateString($data['minDateValue'], 255, 'minDateValue') : null, | |
| 182 | + isset($data['maxDateValue']) && $data['maxDateValue'] !== '' ? | |
| 183 | + self::validateString($data['maxDateValue'], 255, 'maxDateValue') : null, | |
| 184 | + ), | |
| 185 | + self::validateBool($data['showRatingText'] ?? false), | |
| 186 | + self::validateBool($data['visible'] ?? true), | |
| 187 | + self::validateString($data['htmlContent'] ?? '', 4294967295, 'htmlContent'), | |
| 188 | + self::validateString($data['agreementText'] ?? '', 1000, 'agreementText'), | |
| 189 | + self::validateBool($data['limitRange'] ?? false), | |
| 190 | + ); | |
| 191 | + $advancedSettings = new FieldAdvancedSettings( | |
| 192 | + self::resolveDefaultValue($data), | |
| 193 | + self::validateBool($data['limitMaxLength'] ?? false), | |
| 194 | + (int)($data['maxLength'] ?? 255), | |
| 195 | + self::validateString($data['labelPosition'] ?? '', 255, 'labelPosition'), | |
| 196 | + self::validateBool($data['noDuplicates'] ?? false), | |
| 197 | + self::validateString($data['inputPrefix'] ?? '', 255, 'inputPrefix'), | |
| 198 | + self::validateString($data['inputSuffix'] ?? '', 255, 'inputSuffix'), | |
| 199 | + self::validateString($data['ratingIcon'] ?? 'star', 50, 'ratingIcon'), | |
| 200 | + new FileUploadFieldAdvancedSettings( | |
| 201 | + (int) ($data['maxFileSizeMb'] ?? 0), | |
| 202 | + (string) ($data['saveUploadsTo'] ?? 'ivyforms'), | |
| 203 | + is_array($data['allowedFileExtensions'] ?? null) | |
| 204 | + ? $data['allowedFileExtensions'] | |
| 205 | + : [] | |
| 206 | + ) | |
| 207 | + ); | |
| 208 | + $fieldOptions = new FieldOptions($data['options'] ?? []); | |
| 209 | + | |
| 210 | + $fieldObject = new Field( | |
| 211 | + $data['id'] ?? 0, | |
| 212 | + $data['formId'] ?? 0, | |
| 213 | + $data['fieldIndex'] ?? 0, | |
| 214 | + $data['type'] ?? '', | |
| 215 | + $data['position'] ?? 0, | |
| 216 | + $generalSettings, | |
| 217 | + $fieldOptions, | |
| 218 | + $advancedSettings, | |
| 219 | + $data['parentId'] ?? null, | |
| 220 | + $data['rows'] ?? 0, | |
| 221 | + $data['rowIndex'] ?? 0, | |
| 222 | + $data['columnIndex'] ?? 0, | |
| 223 | + $data['width'] ?? 100, | |
| 224 | + $data['pageId'] ?? null | |
| 225 | + ); | |
| 226 | + | |
| 227 | + /** | |
| 228 | + * Allows setting of additional properties for the Field value object. | |
| 229 | + * @since 0.1.0 | |
| 230 | + * | |
| 231 | + * @param Field $fieldObject The current field data. | |
| 232 | + * @param mixed[] $data The settings array from the database. | |
| 233 | + * @return mixed[] The modified field data. | |
| 234 | + */ | |
| 235 | + $fieldObject = apply_filters('ivyforms/field/value_object/set_properties', $fieldObject, $data); | |
| 236 | + | |
| 237 | + $field = new FieldEntity($fieldObject); | |
| 238 | + | |
| 239 | + if (isset($data['id'])) { | |
| 240 | + $field->setId($data['id']); | |
| 241 | + } | |
| 242 | + | |
| 243 | + if (isset($data['formId'])) { | |
| 244 | + $field->setFormId($data['formId']); | |
| 245 | + } | |
| 246 | + | |
| 247 | + if (isset($data['fieldIndex'])) { | |
| 248 | + $field->setIndex($data['fieldIndex']); | |
| 249 | + } | |
| 250 | + | |
| 251 | + if (isset($data['type'])) { | |
| 252 | + $field->setType($data['type']); | |
| 253 | + } | |
| 254 | + | |
| 255 | + if (isset($data['position'])) { | |
| 256 | + $field->setPosition($data['position']); | |
| 257 | + } | |
| 258 | + | |
| 259 | + if (isset($data['parentId'])) { | |
| 260 | + $field->setParentId($data['parentId']); | |
| 261 | + } | |
| 262 | + | |
| 263 | + if (isset($data['pageId'])) { | |
| 264 | + $field->setPageId($data['pageId']); | |
| 265 | + } | |
| 266 | + | |
| 267 | + if (isset($data['rows'])) { | |
| 268 | + $field->setRows((int)$data['rows']); | |
| 269 | + } | |
| 270 | + | |
| 271 | + /** | |
| 272 | + * Allows setting of additional properties for the Field entity. | |
| 273 | + * @since 0.1.0 | |
| 274 | + * | |
| 275 | + * @param FieldEntity $field The current field data. | |
| 276 | + * @param mixed[] $data The settings array from the database. | |
| 277 | + * @return mixed[] The modified field data. | |
| 278 | + */ | |
| 279 | + $field = apply_filters('ivyforms/field/entity/set_properties', $field, $data); | |
| 280 | + | |
| 281 | + if (!empty($data['nameFieldType'])) { | |
| 282 | + $field->setAdditionalProperty('nameFieldType', (string)$data['nameFieldType']); | |
| 283 | + } | |
| 284 | + if (array_key_exists('subFieldIndex', $data) && $data['subFieldIndex'] !== null) { | |
| 285 | + $field->setAdditionalProperty('subFieldIndex', (int)$data['subFieldIndex']); | |
| 286 | + } | |
| 287 | + | |
| 288 | + return $field; | |
| 289 | + } | |
| 290 | + | |
| 291 | + /** | |
| 292 | + * @param mixed $rawSettings | |
| 293 | + * @return array<string, mixed> | |
| 294 | + */ | |
| 295 | + private static function parseSettings($rawSettings): array | |
| 296 | + { | |
| 297 | + if (is_array($rawSettings)) { | |
| 298 | + return $rawSettings; | |
| 299 | + } | |
| 300 | + | |
| 301 | + if (!is_string($rawSettings) || $rawSettings === '') { | |
| 302 | + return []; | |
| 303 | + } | |
| 304 | + | |
| 305 | + $decoded = json_decode($rawSettings, true); | |
| 306 | + | |
| 307 | + return is_array($decoded) ? $decoded : []; | |
| 308 | + } | |
| 309 | + | |
| 310 | + /** | |
| 311 | + * Validate and convert a value to boolean. | |
| 312 | + * | |
| 313 | + * @param mixed $value The value to validate. | |
| 314 | + * | |
| 315 | + * @return bool The validated boolean value. | |
| 316 | + */ | |
| 317 | + private static function validateBool($value): bool | |
| 318 | + { | |
| 319 | + return (bool)$value; | |
| 320 | + } | |
| 321 | + | |
| 322 | + /** | |
| 323 | + * Resolve and validate field default value (Pro may override for rich_text, etc.). | |
| 324 | + * | |
| 325 | + * @param array<string, mixed> $data | |
| 326 | + * | |
| 327 | + * @return string | |
| 328 | + * | |
| 329 | + * @throws ValidationException | |
| 330 | + */ | |
| 331 | + private static function resolveDefaultValue(array $data): string | |
| 332 | + { | |
| 333 | + $filtered = apply_filters( | |
| 334 | + 'ivyforms/field/validate_default_value', | |
| 335 | + null, | |
| 336 | + $data['defaultValue'] ?? '', | |
| 337 | + $data | |
| 338 | + ); | |
| 339 | + | |
| 340 | + if (is_string($filtered)) { | |
| 341 | + return $filtered; | |
| 342 | + } | |
| 343 | + | |
| 344 | + return self::validateString($data['defaultValue'] ?? '', 255, 'defaultValue'); | |
| 345 | + } | |
| 346 | + | |
| 347 | + /** | |
| 348 | + * Validate a string's length. | |
| 349 | + * | |
| 350 | + * @param mixed $value The value to validate. | |
| 351 | + * @param int $maxLength The maximum allowed length. | |
| 352 | + * @param string $fieldName The name of the field (for error messages). | |
| 353 | + * | |
| 354 | + * @return string The validated string. | |
| 355 | + * | |
| 356 | + * @throws ValidationException If the string exceeds the maximum length. | |
| 357 | + */ | |
| 358 | + private static function validateString($value, int $maxLength, string $fieldName): string | |
| 359 | + { | |
| 360 | + $value = (string)$value; | |
| 361 | + if (strlen($value) > $maxLength) { | |
| 362 | + throw new ValidationException( | |
| 363 | + sprintf( | |
| 364 | + esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'), | |
| 365 | + esc_html($fieldName), | |
| 366 | + $maxLength | |
| 367 | + ) | |
| 368 | + ); | |
| 369 | + } | |
| 370 | + return $value; | |
| 371 | + } | |
| 372 | + /** | |
| 373 | + * Validate and coerce numeric range values (minValue / maxValue). | |
| 374 | + * If value is null/empty/non-numeric => returns 0.0. | |
| 375 | + * Performs cross-check when the counterpart is already set. | |
| 376 | + * | |
| 377 | + * @param mixed $value | |
| 378 | + * @param string $fieldName 'minValue' | 'maxValue' | |
| 379 | + * @return float | null | |
| 380 | + * @throws ValidationException | |
| 381 | + */ | |
| 382 | + private static function validateNumberValue($value, string $fieldName): ?float | |
| 383 | + { | |
| 384 | + if ($value === '' || $value === null) { | |
| 385 | + return null; | |
| 386 | + } | |
| 387 | + if (!is_numeric($value)) { | |
| 388 | + throw new ValidationException( | |
| 389 | + sprintf( | |
| 390 | + esc_html__('%1$s must be a number.', 'ivyforms'), | |
| 391 | + esc_html($fieldName) | |
| 392 | + ) | |
| 393 | + ); | |
| 394 | + } | |
| 395 | + return (float)$value; | |
| 396 | + } | |
| 397 | + | |
| 398 | + /** | |
| 399 | + * Validate step (positive number >0) | |
| 400 | + * @param float $value | |
| 401 | + * @return float | |
| 402 | + * @throws ValidationException | |
| 403 | + */ | |
| 404 | + private static function validateStep(float $value): float | |
| 405 | + { | |
| 406 | + if ($value <= 0) { | |
| 407 | + throw new ValidationException(esc_html__('step must be a positive number (>0).', 'ivyforms')); | |
| 408 | + } | |
| 409 | + return (float)$value; | |
| 410 | + } | |
| 411 | + | |
| 412 | + /** | |
| 413 | + * Validates the number format. | |
| 414 | + * | |
| 415 | + * @param string $value | |
| 416 | + * | |
| 417 | + * @return string | |
| 418 | + * | |
| 419 | + */ | |
| 420 | + private static function validateNumberFormat(string $value): string | |
| 421 | + { | |
| 422 | + $allowed = ['', 'us_decimal', 'us_int', 'eu_decimal', 'eu_int']; | |
| 423 | + if (!in_array($value, $allowed, true)) { | |
| 424 | + return ''; | |
| 425 | + } | |
| 426 | + return $value; | |
| 427 | + } | |
| 428 | + | |
| 429 | + private static function validatePhoneFormat(string $format): string | |
| 430 | + { | |
| 431 | + $allowed = ['international','national','e164']; | |
| 432 | + $lower = strtolower($format); | |
| 433 | + if (!in_array($lower, $allowed, true)) { | |
| 434 | + return 'international'; | |
| 435 | + } | |
| 436 | + return $lower; | |
| 437 | + } | |
| 438 | +} | |