| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\Payment; |
| 4 |
|
| 5 |
/** |
| 6 |
* Handles product submission snapshot parsing and normalization. |
| 7 |
* |
| 8 |
* Snapshot line priceAmount values are for entry display only — never use them |
| 9 |
* to compute charge amounts (see ProductSelectionSubtotalCalculator). |
| 10 |
*/ |
| 11 |
class ProductSubmissionSnapshotHelper |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @param mixed $rawValue |
| 15 |
* @return mixed |
| 16 |
*/ |
| 17 |
public function normalizeProductSubmissionValue($rawValue) |
| 18 |
{ |
| 19 |
if (!is_string($rawValue)) { |
| 20 |
return $rawValue; |
| 21 |
} |
| 22 |
|
| 23 |
$trimmed = trim($rawValue); |
| 24 |
if ($trimmed === '' || ($trimmed[0] ?? '') !== '{') { |
| 25 |
return $rawValue; |
| 26 |
} |
| 27 |
|
| 28 |
$decoded = json_decode($trimmed, true); |
| 29 |
if (!is_array($decoded) || (int) ($decoded['v'] ?? 0) !== 1) { |
| 30 |
return $rawValue; |
| 31 |
} |
| 32 |
|
| 33 |
$productType = (string) ($decoded['productType'] ?? ''); |
| 34 |
$raw = (string) ($decoded['raw'] ?? ''); |
| 35 |
|
| 36 |
if ($productType === 'checkbox') { |
| 37 |
return array_values( |
| 38 |
array_filter( |
| 39 |
array_map('trim', explode(',', $raw)), |
| 40 |
static fn(string $part): bool => $part !== '' |
| 41 |
) |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
return $raw !== '' ? $raw : $rawValue; |
| 46 |
} |
| 47 |
} |
| 48 |
|