| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\ValueObjects\Form; |
| 4 |
|
| 5 |
use IvyForms\Common\Constants\SupportedCurrencyCodes; |
| 6 |
use IvyForms\Services\Template\TemplateDefaults; |
| 7 |
|
| 8 |
/** |
| 9 |
* Form-level payment display settings (currency for product fields, totals, etc.). |
| 10 |
*/ |
| 11 |
final class PaymentSettings |
| 12 |
{ |
| 13 |
private string $currency; |
| 14 |
|
| 15 |
/** |
| 16 |
* @param array<string, mixed>|null $settings Raw settings (typically already sanitized). |
| 17 |
*/ |
| 18 |
public function __construct($settings = null) |
| 19 |
{ |
| 20 |
if (!is_array($settings) || empty($settings)) { |
| 21 |
$settings = TemplateDefaults::getDefaultPaymentSettings(); |
| 22 |
} |
| 23 |
|
| 24 |
$code = isset($settings['currency']) |
| 25 |
? strtoupper(trim((string) $settings['currency'])) |
| 26 |
: SupportedCurrencyCodes::DEFAULT_CODE; |
| 27 |
$this->currency = SupportedCurrencyCodes::isAllowed($code) ? $code : SupportedCurrencyCodes::DEFAULT_CODE; |
| 28 |
} |
| 29 |
|
| 30 |
public function getCurrency(): string |
| 31 |
{ |
| 32 |
return $this->currency; |
| 33 |
} |
| 34 |
|
| 35 |
public function setCurrency(string $code): void |
| 36 |
{ |
| 37 |
$normalized = strtoupper(trim($code)); |
| 38 |
$this->currency = SupportedCurrencyCodes::isAllowed($normalized) |
| 39 |
? $normalized |
| 40 |
: SupportedCurrencyCodes::DEFAULT_CODE; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @return mixed[] |
| 45 |
*/ |
| 46 |
public function toArray(): array |
| 47 |
{ |
| 48 |
return [ |
| 49 |
'paymentSettings' => [ |
| 50 |
'currency' => $this->currency, |
| 51 |
], |
| 52 |
]; |
| 53 |
} |
| 54 |
} |
| 55 |
|