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

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