| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit; |
| 4 |
|
| 5 |
class WC_Vipps_Agreement_Interval extends WC_Vipps_Model { |
| 6 |
public const UNIT_YEAR = "YEAR"; |
| 7 |
public const UNIT_MONTH = "MONTH"; |
| 8 |
public const UNIT_WEEK = "WEEK"; |
| 9 |
public const UNIT_DAY = "DAY"; |
| 10 |
|
| 11 |
protected array $valid_units = [ |
| 12 |
self::UNIT_YEAR, |
| 13 |
self::UNIT_MONTH, |
| 14 |
self::UNIT_WEEK, |
| 15 |
self::UNIT_DAY |
| 16 |
]; |
| 17 |
|
| 18 |
protected array $required_fields = [ "unit", "count" ]; |
| 19 |
|
| 20 |
public ?string $unit = null; |
| 21 |
public ?int $count = null; |
| 22 |
|
| 23 |
/** |
| 24 |
* @throws WC_Vipps_Recurring_Invalid_Value_Exception |
| 25 |
*/ |
| 26 |
public function set_unit( string $unit ): self { |
| 27 |
if ( ! in_array( $unit, $this->valid_units ) ) { |
| 28 |
$class = get_class( $this ); |
| 29 |
throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$unit is not a valid value for `unit` in $class." ); |
| 30 |
} |
| 31 |
|
| 32 |
$this->unit = $unit; |
| 33 |
|
| 34 |
return $this; |
| 35 |
} |
| 36 |
|
| 37 |
public function set_count( int $count ): self { |
| 38 |
$this->count = $count; |
| 39 |
|
| 40 |
return $this; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @throws WC_Vipps_Recurring_Missing_Value_Exception |
| 45 |
*/ |
| 46 |
public function to_array( bool $check_required = false ): array { |
| 47 |
if ( $check_required ) { |
| 48 |
$this->check_required(); |
| 49 |
} |
| 50 |
|
| 51 |
return [ |
| 52 |
"unit" => $this->unit, |
| 53 |
"count" => $this->count |
| 54 |
]; |
| 55 |
} |
| 56 |
} |
| 57 |
|