| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit; |
| 4 |
|
| 5 |
class WC_Vipps_Agreement_Pricing extends WC_Vipps_Model { |
| 6 |
public const TYPE_LEGACY = "LEGACY"; |
| 7 |
public const TYPE_VARIABLE = "VARIABLE"; |
| 8 |
|
| 9 |
protected array $valid_types = [ |
| 10 |
self::TYPE_LEGACY, |
| 11 |
self::TYPE_VARIABLE |
| 12 |
]; |
| 13 |
|
| 14 |
protected array $required_fields = [ |
| 15 |
"VARIABLE" => [ "type", "currency", "suggested_max_amount" ], |
| 16 |
"LEGACY" => [ "type", "currency", "amount" ], |
| 17 |
]; |
| 18 |
|
| 19 |
public ?string $type = null; |
| 20 |
public ?string $currency = null; |
| 21 |
public ?int $amount = null; |
| 22 |
public ?int $suggested_max_amount = null; |
| 23 |
public ?int $max_amount = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* @throws WC_Vipps_Recurring_Invalid_Value_Exception |
| 27 |
*/ |
| 28 |
public function set_type( string $type ): self { |
| 29 |
if ( ! in_array( $type, $this->valid_types, true ) ) { |
| 30 |
$class = get_class( $this ); |
| 31 |
throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$type is not a valid value for `type` in $class." ); |
| 32 |
} |
| 33 |
|
| 34 |
$this->type = $type; |
| 35 |
|
| 36 |
return $this; |
| 37 |
} |
| 38 |
|
| 39 |
public function set_currency( string $currency ): self { |
| 40 |
$this->currency = $currency; |
| 41 |
|
| 42 |
return $this; |
| 43 |
} |
| 44 |
|
| 45 |
public function set_amount( int $amount ): self { |
| 46 |
$this->amount = $amount; |
| 47 |
|
| 48 |
return $this; |
| 49 |
} |
| 50 |
|
| 51 |
public function set_suggested_max_amount( int $suggested_max_amount ): self { |
| 52 |
$this->suggested_max_amount = $suggested_max_amount; |
| 53 |
|
| 54 |
return $this; |
| 55 |
} |
| 56 |
|
| 57 |
public function set_max_amount( int $max_amount ): self { |
| 58 |
$this->max_amount = $max_amount; |
| 59 |
|
| 60 |
return $this; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @throws WC_Vipps_Recurring_Missing_Value_Exception |
| 65 |
*/ |
| 66 |
public function to_array( bool $check_required = false ): array { |
| 67 |
if ( $check_required ) { |
| 68 |
$this->check_required( $this->type ); |
| 69 |
} |
| 70 |
|
| 71 |
return array_merge( |
| 72 |
[ |
| 73 |
"type" => $this->type, |
| 74 |
"currency" => $this->currency, |
| 75 |
], |
| 76 |
$this->conditional( "amount", $this->amount ), |
| 77 |
$this->conditional( "suggestedMaxAmount", $this->suggested_max_amount ), |
| 78 |
$this->conditional( "maxAmount", $this->max_amount ) |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
|