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

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