| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Common\Helpers\Import; |
| 4 |
|
| 5 |
/** |
| 6 |
* Build an IvyForms composite address field from third-party address parts. |
| 7 |
*/ |
| 8 |
class ThirdPartyAddressFieldMapper |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @param string $label |
| 12 |
* @param string $description |
| 13 |
* @param bool $required |
| 14 |
* @param array<int, array<string, mixed>> $parts |
| 15 |
* @param array<int, array<string, mixed>> $fields |
| 16 |
* @param array<string, int> $context |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public static function append( |
| 20 |
string $label, |
| 21 |
string $description, |
| 22 |
bool $required, |
| 23 |
array $parts, |
| 24 |
array &$fields, |
| 25 |
array &$context |
| 26 |
): void { |
| 27 |
$parentIndex = $context['fieldIndex']++; |
| 28 |
$fields[] = [ |
| 29 |
'id' => $context['id']++, |
| 30 |
'fieldIndex' => $parentIndex, |
| 31 |
'type' => 'address', |
| 32 |
'label' => sanitize_text_field($label), |
| 33 |
'placeholder' => '', |
| 34 |
'description' => sanitize_text_field($description), |
| 35 |
'required' => $required, |
| 36 |
'defaultValue' => '', |
| 37 |
'parentId' => null, |
| 38 |
'position' => $context['position']++, |
| 39 |
'rowIndex' => $context['rowIndex']++, |
| 40 |
'columnIndex' => 0, |
| 41 |
'width' => 100, |
| 42 |
]; |
| 43 |
|
| 44 |
$position = 1; |
| 45 |
foreach ($parts as $part) { |
| 46 |
if (($part['visible'] ?? true) === false) { |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
$addressType = (string) ($part['type'] ?? ''); |
| 51 |
$fields[] = [ |
| 52 |
'id' => $context['id']++, |
| 53 |
'fieldIndex' => $parentIndex, |
| 54 |
'type' => 'text', |
| 55 |
'label' => sanitize_text_field((string) ($part['label'] ?? '')), |
| 56 |
'placeholder' => sanitize_text_field((string) ($part['placeholder'] ?? '')), |
| 57 |
'required' => $addressType === 'addressLine2' ? false : $required, |
| 58 |
'defaultValue' => sanitize_text_field((string) ($part['defaultValue'] ?? '')), |
| 59 |
'parentId' => $parentIndex, |
| 60 |
'position' => $position++, |
| 61 |
'visible' => true, |
| 62 |
'settings' => wp_json_encode(['addressFieldType' => $addressType]) ?: '', |
| 63 |
]; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|