| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\Payment; |
| 4 |
|
| 5 |
use IvyForms\Common\Helpers\EntryHelper; |
| 6 |
|
| 7 |
/** |
| 8 |
* Resolves mapped form field values for Square/Stripe/PayPal buyer info. |
| 9 |
*/ |
| 10 |
class SquareBuyerSubmissionValues |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @param array<string, mixed> $submissionByTypeIndex |
| 14 |
* @param array<int, object> $formFields |
| 15 |
* @param array<int|string, mixed> $submissionData |
| 16 |
* @return mixed |
| 17 |
*/ |
| 18 |
public static function mappedValue( |
| 19 |
string $key, |
| 20 |
array $submissionByTypeIndex, |
| 21 |
array $formFields, |
| 22 |
array $submissionData |
| 23 |
) { |
| 24 |
if ($key === '') { |
| 25 |
return null; |
| 26 |
} |
| 27 |
|
| 28 |
if (array_key_exists($key, $submissionByTypeIndex)) { |
| 29 |
return self::decodeValue($submissionByTypeIndex[$key]); |
| 30 |
} |
| 31 |
|
| 32 |
return self::valueFromFieldId($key, $formFields, $submissionData); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @param mixed $nameValue |
| 37 |
* @param array<int, object> $formFields |
| 38 |
* @param array<int|string, mixed> $submissionData |
| 39 |
* @return array{first_name: string, last_name: string}|null |
| 40 |
*/ |
| 41 |
public static function nameParts($nameValue, string $nameKey, array $formFields, array $submissionData): ?array |
| 42 |
{ |
| 43 |
$fromValue = self::namePartsFromSubmissionValue($nameValue); |
| 44 |
if ($fromValue !== null) { |
| 45 |
return $fromValue; |
| 46 |
} |
| 47 |
|
| 48 |
return self::namePartsFromCompoundField($nameKey, $formFields, $submissionData); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @param mixed $nameValue |
| 53 |
* @return array{first_name: string, last_name: string}|null |
| 54 |
*/ |
| 55 |
private static function namePartsFromSubmissionValue($nameValue): ?array |
| 56 |
{ |
| 57 |
$nameValue = self::decodeValue($nameValue); |
| 58 |
if (is_string($nameValue) || is_numeric($nameValue)) { |
| 59 |
return self::namePartsFromString((string) $nameValue); |
| 60 |
} |
| 61 |
|
| 62 |
if (!is_array($nameValue)) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
$first = self::firstPresent($nameValue, ['first', 'first_name', 'given_name', 'nameField1']); |
| 67 |
$last = self::firstPresent($nameValue, ['last', 'last_name', 'surname', 'nameField2']); |
| 68 |
if ($first === '' && $last === '') { |
| 69 |
$parts = self::flattenedNameParts($nameValue); |
| 70 |
$first = $parts[0] ?? ''; |
| 71 |
$last = implode(' ', array_slice($parts, 1)); |
| 72 |
} |
| 73 |
|
| 74 |
if ($first === '' && $last === '') { |
| 75 |
return null; |
| 76 |
} |
| 77 |
|
| 78 |
return [ |
| 79 |
'first_name' => $first, |
| 80 |
'last_name' => $last, |
| 81 |
]; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @return array{first_name: string, last_name: string}|null |
| 86 |
*/ |
| 87 |
private static function namePartsFromString(string $nameValue): ?array |
| 88 |
{ |
| 89 |
$full = sanitize_text_field(trim($nameValue)); |
| 90 |
|
| 91 |
return $full !== '' ? ['first_name' => $full, 'last_name' => ''] : null; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @param array<int, object> $formFields |
| 96 |
* @param array<int|string, mixed> $submissionData |
| 97 |
* @return array{first_name: string, last_name: string}|null |
| 98 |
*/ |
| 99 |
private static function namePartsFromCompoundField( |
| 100 |
string $nameKey, |
| 101 |
array $formFields, |
| 102 |
array $submissionData |
| 103 |
): ?array { |
| 104 |
$field = self::fieldForTypeIndexKey($nameKey, $formFields); |
| 105 |
if ($field === null || !method_exists($field, 'getId')) { |
| 106 |
return null; |
| 107 |
} |
| 108 |
|
| 109 |
$parentChildrenMap = EntryHelper::buildParentChildrenMap($formFields); |
| 110 |
$name = trim(EntryHelper::getCompoundFieldValue( |
| 111 |
$field->getId(), |
| 112 |
$parentChildrenMap, |
| 113 |
EntryHelper::buildParentSubfieldKeys($parentChildrenMap, $submissionData, $formFields), |
| 114 |
$submissionData |
| 115 |
)); |
| 116 |
|
| 117 |
return $name !== '' ? ['first_name' => $name, 'last_name' => ''] : null; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @param array<int, object> $formFields |
| 122 |
* @param array<int|string, mixed> $submissionData |
| 123 |
* @return mixed |
| 124 |
*/ |
| 125 |
private static function valueFromFieldId(string $key, array $formFields, array $submissionData) |
| 126 |
{ |
| 127 |
$field = self::fieldForTypeIndexKey($key, $formFields); |
| 128 |
$fieldId = $field !== null && method_exists($field, 'getId') ? $field->getId() : null; |
| 129 |
if ($fieldId === null) { |
| 130 |
return array_key_exists($key, $submissionData) ? self::decodeValue($submissionData[$key]) : null; |
| 131 |
} |
| 132 |
|
| 133 |
if (array_key_exists($fieldId, $submissionData)) { |
| 134 |
return self::decodeValue($submissionData[$fieldId]); |
| 135 |
} |
| 136 |
|
| 137 |
if (array_key_exists((string) $fieldId, $submissionData)) { |
| 138 |
return self::decodeValue($submissionData[(string) $fieldId]); |
| 139 |
} |
| 140 |
|
| 141 |
return null; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* @param array<string, mixed> $nameValue |
| 146 |
* @param list<string> $keys |
| 147 |
*/ |
| 148 |
private static function firstPresent(array $nameValue, array $keys): string |
| 149 |
{ |
| 150 |
foreach ($keys as $key) { |
| 151 |
if (!isset($nameValue[$key])) { |
| 152 |
continue; |
| 153 |
} |
| 154 |
|
| 155 |
$value = sanitize_text_field((string) $nameValue[$key]); |
| 156 |
if ($value !== '') { |
| 157 |
return $value; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
return ''; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* @param array<int|string, mixed> $nameValue |
| 166 |
* @return list<string> |
| 167 |
*/ |
| 168 |
private static function flattenedNameParts(array $nameValue): array |
| 169 |
{ |
| 170 |
$parts = []; |
| 171 |
foreach ($nameValue as $part) { |
| 172 |
if (!is_string($part) && !is_numeric($part)) { |
| 173 |
continue; |
| 174 |
} |
| 175 |
|
| 176 |
$normalized = sanitize_text_field(trim((string) $part)); |
| 177 |
if ($normalized !== '') { |
| 178 |
$parts[] = $normalized; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return $parts; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @param array<int, object> $formFields |
| 187 |
*/ |
| 188 |
private static function fieldForTypeIndexKey(string $key, array $formFields): ?object |
| 189 |
{ |
| 190 |
foreach ($formFields as $field) { |
| 191 |
if (!method_exists($field, 'getType') || !method_exists($field, 'getIndex')) { |
| 192 |
continue; |
| 193 |
} |
| 194 |
|
| 195 |
if ((string) $field->getType() . '_' . (int) $field->getIndex() === $key) { |
| 196 |
return $field; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return null; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* @param mixed $value |
| 205 |
* @return mixed |
| 206 |
*/ |
| 207 |
private static function decodeValue($value) |
| 208 |
{ |
| 209 |
if (is_object($value)) { |
| 210 |
return get_object_vars($value); |
| 211 |
} |
| 212 |
|
| 213 |
if (!is_string($value)) { |
| 214 |
return $value; |
| 215 |
} |
| 216 |
|
| 217 |
$trimmed = trim($value); |
| 218 |
if ($trimmed === '' || ($trimmed[0] !== '{' && $trimmed[0] !== '[')) { |
| 219 |
return $value; |
| 220 |
} |
| 221 |
|
| 222 |
$decoded = json_decode($trimmed, true); |
| 223 |
|
| 224 |
return is_array($decoded) ? $decoded : $value; |
| 225 |
} |
| 226 |
} |
| 227 |
|