PluginProbe
The Innovative Form Builder – IvyForms / 1.4
The Innovative Form Builder – IvyForms v1.4
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 / Common / Helpers / Import / FluentFormsOptionsExtractor.php

FluentFormsOptionsExtractor.php in The Innovative Form Builder – IvyForms 1.4, at backend/src/Common/Helpers/Import/FluentFormsOptionsExtractor.php

151 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Common\Helpers\Import;
4
5 /**
6 * Extract IvyForms field options from Fluent Forms elements.
7 *
8 * Splitting option extraction out of FluentFormsFieldMapper keeps each class
9 * focused and within complexity limits.
10 */
11 class FluentFormsOptionsExtractor
12 {
13 /**
14 * Extract field options for an option-based Fluent Forms element.
15 *
16 * @param array<string, mixed> $settings
17 * @param array<string, mixed> $attributes
18 * @return array<int, array<string, mixed>>
19 */
20 public static function extract(array $settings, array $attributes): array
21 {
22 $rawOptions = self::resolveRawOptions($settings, $attributes);
23 $selectedValues = self::resolveSelectedValues($attributes);
24
25 $options = [];
26 foreach ($rawOptions as $rawOption) {
27 $option = self::buildOption($rawOption, count($options) + 1, $selectedValues);
28 if ($option !== null) {
29 $options[] = $option;
30 }
31 }
32
33 return $options;
34 }
35
36 /**
37 * Resolve the raw options list from settings or attributes.
38 *
39 * @param array<string, mixed> $settings
40 * @param array<string, mixed> $attributes
41 * @return array<int, mixed>
42 */
43 private static function resolveRawOptions(array $settings, array $attributes): array
44 {
45 $advanced = $settings['advanced_options'] ?? null;
46 if (is_array($advanced) && !empty($advanced)) {
47 return array_values($advanced);
48 }
49
50 $attributeOptions = $attributes['options'] ?? null;
51 if (is_array($attributeOptions) && !empty($attributeOptions)) {
52 return self::normalizeAttributeOptions($attributeOptions);
53 }
54
55 return [];
56 }
57
58 /**
59 * Resolve the pre-selected option values of a Fluent Forms element.
60 *
61 * Fluent Forms keeps the selection on the field itself rather than on the
62 * options: a list for multi-value elements (checkbox, multi-select) and a
63 * single scalar for radio/select.
64 *
65 * @param array<string, mixed> $attributes
66 * @return array<int, string>
67 */
68 private static function resolveSelectedValues(array $attributes): array
69 {
70 $rawValues = $attributes['value'] ?? null;
71 if (!is_array($rawValues)) {
72 $rawValues = [$rawValues];
73 }
74
75 $selectedValues = [];
76 foreach ($rawValues as $rawValue) {
77 $value = self::scalarText($rawValue);
78 if ($value !== '') {
79 $selectedValues[] = $value;
80 }
81 }
82
83 return $selectedValues;
84 }
85
86 /**
87 * Build a single option array from a raw Fluent Forms option.
88 *
89 * @param mixed $rawOption
90 * @param int $position
91 * @param array<int, string> $selectedValues
92 * @return array<string, mixed>|null
93 */
94 private static function buildOption($rawOption, int $position, array $selectedValues): ?array
95 {
96 if (!is_array($rawOption)) {
97 return null;
98 }
99
100 $label = self::scalarText($rawOption['label'] ?? '');
101 $value = array_key_exists('value', $rawOption) ? self::scalarText($rawOption['value']) : $label;
102 if ($label === '' && $value === '') {
103 return null;
104 }
105
106 $resolvedValue = $value !== '' ? $value : $label;
107
108 return [
109 'id' => $position,
110 'label' => $label !== '' ? $label : $value,
111 'value' => $resolvedValue,
112 'isDefault' => !empty($rawOption['default']) || in_array($resolvedValue, $selectedValues, true),
113 'position' => $position,
114 ];
115 }
116
117 /**
118 * Normalize a value=>label options map into a list of option arrays.
119 *
120 * @param array<string|int, mixed> $options
121 * @return array<int, array<string, mixed>>
122 */
123 private static function normalizeAttributeOptions(array $options): array
124 {
125 $normalized = [];
126 foreach ($options as $value => $label) {
127 $normalized[] = ['label' => $label, 'value' => $value];
128 }
129
130 return $normalized;
131 }
132
133 /**
134 * Sanitize a value to string, discarding non-scalars.
135 *
136 * Fluent Forms stores multi-value data (such as checkbox defaults) as arrays,
137 * so casting those to string would emit a PHP warning.
138 *
139 * @param mixed $value
140 * @return string
141 */
142 private static function scalarText($value): string
143 {
144 if (!is_scalar($value)) {
145 return '';
146 }
147
148 return sanitize_text_field((string) $value);
149 }
150 }
151