← All changes
|
backend/src/Services/Submission/SubmissionDataFormatterService.php
+22
-100
1.0
→
1.5
View file →
| @@ -25,19 +25,27 @@ | ||
| 25 | 25 | */ |
| 26 | 26 | private SubmissionFieldTypeFormatter $typeFormatter; |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | + * @var SubmissionIntegrationFieldValueResolver | |
| 30 | + */ | |
| 31 | + private SubmissionIntegrationFieldValueResolver $fieldValueResolver; | |
| 32 | + | |
| 33 | + /** | |
| 29 | 34 | * Constructor. |
| 30 | 35 | */ |
| 31 | 36 | public function __construct() |
| 32 | 37 | { |
| 33 | - $this->formatter = new SubmissionFieldFormatter(); | |
| 34 | - $this->typeFormatter = new SubmissionFieldTypeFormatter(); | |
| 38 | + $this->formatter = new SubmissionFieldFormatter(); | |
| 39 | + $this->typeFormatter = new SubmissionFieldTypeFormatter(); | |
| 40 | + $this->fieldValueResolver = new SubmissionIntegrationFieldValueResolver(); | |
| 35 | 41 | } |
| 42 | + | |
| 36 | 43 | /** |
| 37 | 44 | * Format submission data into a standardized structure for integrations. |
| 38 | 45 | * |
| 39 | - * @param array<int|string, mixed> $submissionData Raw submission data (indexed by fieldIndex) | |
| 46 | + * @param array<int|string, mixed> $submissionData Raw submission data (keys are field database | |
| 47 | + * ids from sanitization; legacy 0-based indices still supported) | |
| 40 | 48 | * @param array<int, object> $formFields Array of Field objects |
| 41 | 49 | * @return array{ |
| 42 | 50 | * fields: array<string, mixed>, |
| 43 | 51 | * user_inputs: array<string, string>, |
| @@ -47,9 +55,13 @@ | ||
| 47 | 55 | public function formatForIntegrations(array $submissionData, array $formFields): array |
| 48 | 56 | { |
| 49 | 57 | // Build parent-children map and subfield keys (same as entry system) |
| 50 | 58 | $parentChildrenMap = EntryHelper::buildParentChildrenMap($formFields); |
| 51 | - $parentSubfieldKeys = EntryHelper::buildParentSubfieldKeys($parentChildrenMap, $submissionData); | |
| 59 | + $parentSubfieldKeys = EntryHelper::buildParentSubfieldKeys( | |
| 60 | + $parentChildrenMap, | |
| 61 | + $submissionData, | |
| 62 | + $formFields | |
| 63 | + ); | |
| 52 | 64 | $fieldIdToIndex = $this->buildFieldIdToIndexMap($formFields); |
| 53 | 65 | |
| 54 | 66 | // Build formatted data |
| 55 | 67 | $fields = []; // Original values (arrays/objects for complex fields) |
| @@ -139,9 +151,14 @@ | ||
| 139 | 151 | if (FieldHelper::isSecurityField($fieldType) || ($parentId !== null && $parentId > 0)) { |
| 140 | 152 | return null; |
| 141 | 153 | } |
| 142 | 154 | |
| 143 | - $value = $this->resolveFieldValue($field, $submissionData, $parentSubfieldKeys, $fieldIdToIndex); | |
| 155 | + $value = $this->fieldValueResolver->resolveFieldValue( | |
| 156 | + $field, | |
| 157 | + $submissionData, | |
| 158 | + $parentSubfieldKeys, | |
| 159 | + $fieldIdToIndex | |
| 160 | + ); | |
| 144 | 161 | |
| 145 | 162 | // Skip empty values |
| 146 | 163 | if ($value === null || $value === '' || (is_array($value) && empty($value))) { |
| 147 | 164 | return null; |
| @@ -210,101 +227,6 @@ | ||
| 210 | 227 | */ |
| 211 | 228 | public function formatFieldValueByType($value, string $type, ?object $field = null) |
| 212 | 229 | { |
| 213 | 230 | return $this->typeFormatter->format($value, $type, $field); |
| 214 | - } | |
| 215 | - | |
| 216 | - /** | |
| 217 | - * Resolve field value (mirrors entry system logic). | |
| 218 | - * | |
| 219 | - * @param object $field The field object | |
| 220 | - * @param array<int|string, mixed> $submissionData Submission data keyed by fieldIndex | |
| 221 | - * @param array<int, string|null> $parentSubfieldKeys Map of child field IDs to subfield keys | |
| 222 | - * @param array<int, int> $fieldIdToIndex Map of field IDs to field indices | |
| 223 | - * @return mixed The resolved field value | |
| 224 | - */ | |
| 225 | - private function resolveFieldValue( | |
| 226 | - object $field, | |
| 227 | - array $submissionData, | |
| 228 | - array $parentSubfieldKeys, | |
| 229 | - array $fieldIdToIndex | |
| 230 | - ) { | |
| 231 | - $parentId = method_exists($field, 'getParentId') ? $field->getParentId() : null; | |
| 232 | - | |
| 233 | - $subfieldValue = $this->resolveSubfieldValue( | |
| 234 | - $parentId, | |
| 235 | - $field->getId(), | |
| 236 | - $parentSubfieldKeys, | |
| 237 | - $fieldIdToIndex, | |
| 238 | - $submissionData | |
| 239 | - ); | |
| 240 | - | |
| 241 | - if ($subfieldValue !== false) { | |
| 242 | - return $subfieldValue; | |
| 243 | - } | |
| 244 | - | |
| 245 | - $fieldIndex = $fieldIdToIndex[$field->getId()] ?? null; | |
| 246 | - $compoundValue = $this->resolveCompoundFieldValue( | |
| 247 | - $field->getType(), | |
| 248 | - $parentId, | |
| 249 | - $fieldIndex, | |
| 250 | - $submissionData | |
| 251 | - ); | |
| 252 | - | |
| 253 | - return $compoundValue ?? ($fieldIndex !== null ? ($submissionData[$fieldIndex] ?? '') : ''); | |
| 254 | - } | |
| 255 | - | |
| 256 | - /** | |
| 257 | - * Resolve subfield value from parent field. | |
| 258 | - * | |
| 259 | - * @param int|null $parentId | |
| 260 | - * @param int $fieldId | |
| 261 | - * @param array<int, string|null> $parentSubfieldKeys | |
| 262 | - * @param array<int, int> $fieldIdToIndex | |
| 263 | - * @param array<int|string, mixed> $submissionData | |
| 264 | - * @return mixed|false False if not a subfield, value otherwise | |
| 265 | - */ | |
| 266 | - private function resolveSubfieldValue( | |
| 267 | - ?int $parentId, | |
| 268 | - int $fieldId, | |
| 269 | - array $parentSubfieldKeys, | |
| 270 | - array $fieldIdToIndex, | |
| 271 | - array $submissionData | |
| 272 | - ) { | |
| 273 | - if ($parentId === null || $parentId <= 0 || !isset($parentSubfieldKeys[$fieldId])) { | |
| 274 | - return false; | |
| 275 | - } | |
| 276 | - | |
| 277 | - $parentFieldIndex = $fieldIdToIndex[$parentId] ?? null; | |
| 278 | - $subKey = $parentSubfieldKeys[$fieldId]; | |
| 279 | - | |
| 280 | - if ($parentFieldIndex !== null && isset($submissionData[$parentFieldIndex][$subKey])) { | |
| 281 | - return $submissionData[$parentFieldIndex][$subKey]; | |
| 282 | - } | |
| 283 | - | |
| 284 | - return null; | |
| 285 | - } | |
| 286 | - | |
| 287 | - /** | |
| 288 | - * Resolve compound field value (name, address). | |
| 289 | - * | |
| 290 | - * @param string $fieldType | |
| 291 | - * @param int|null $parentId | |
| 292 | - * @param int|null $fieldIndex | |
| 293 | - * @param array<int|string, mixed> $submissionData | |
| 294 | - * @return mixed|null | |
| 295 | - */ | |
| 296 | - private function resolveCompoundFieldValue( | |
| 297 | - string $fieldType, | |
| 298 | - ?int $parentId, | |
| 299 | - ?int $fieldIndex, | |
| 300 | - array $submissionData | |
| 301 | - ) { | |
| 302 | - if (!in_array($fieldType, ['name', 'address'], true) || ($parentId !== null && $parentId !== 0)) { | |
| 303 | - return null; | |
| 304 | - } | |
| 305 | - | |
| 306 | - return ($fieldIndex !== null && isset($submissionData[$fieldIndex]) && is_array($submissionData[$fieldIndex])) | |
| 307 | - ? $submissionData[$fieldIndex] | |
| 308 | - : ''; | |
| 309 | 231 | } |
| 310 | 232 | } |