| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit; |
| 4 |
|
| 5 |
class WC_Vipps_Agreement_Campaign extends WC_Vipps_Model { |
| 6 |
public const TYPE_PRICE_CAMPAIGN = "PRICE_CAMPAIGN"; |
| 7 |
public const TYPE_PERIOD_CAMPAIGN = "PERIOD_CAMPAIGN"; |
| 8 |
public const TYPE_PERIOD_CAMPAIGN_V3 = "PeriodCampaignV3"; |
| 9 |
public const TYPE_EVENT_CAMPAIGN = "EVENT_CAMPAIGN"; |
| 10 |
public const TYPE_FULL_FLEX_CAMPAIGN = "FULL_FLEX_CAMPAIGN"; |
| 11 |
public const TYPE_LEGACY_CAMPAIGN = "LEGACY_CAMPAIGN"; |
| 12 |
|
| 13 |
protected array $valid_types = [ |
| 14 |
self::TYPE_PRICE_CAMPAIGN, |
| 15 |
self::TYPE_PERIOD_CAMPAIGN, |
| 16 |
self::TYPE_EVENT_CAMPAIGN, |
| 17 |
self::TYPE_FULL_FLEX_CAMPAIGN, |
| 18 |
self::TYPE_LEGACY_CAMPAIGN, |
| 19 |
]; |
| 20 |
|
| 21 |
protected array $required_fields = [ |
| 22 |
"PRICE_CAMPAIGN" => [ "type", "price", "end" ], |
| 23 |
"PERIOD_CAMPAIGN" => [ "type", "price", "end", "period" ], |
| 24 |
"EVENT_CAMPAIGN" => [ "type", "price", "event_date", "event_text" ], |
| 25 |
"FULL_FLEX_CAMPAIGN" => [ "type", "price", "end", "interval" ], |
| 26 |
"LEGACY_CAMPAIGN" => [ "type", "price", "end" ], |
| 27 |
]; |
| 28 |
|
| 29 |
public ?string $type = null; |
| 30 |
public ?int $price = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var DateTime|string $end |
| 34 |
*/ |
| 35 |
public $end; |
| 36 |
|
| 37 |
public ?string $explanation = null; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var DateTime|string $event_date |
| 41 |
*/ |
| 42 |
public $event_date; |
| 43 |
public ?string $event_text = null; |
| 44 |
public ?WC_Vipps_Agreement_Campaign_Period $period = null; |
| 45 |
public ?WC_Vipps_Agreement_Interval $interval = null; |
| 46 |
|
| 47 |
/** |
| 48 |
* @throws WC_Vipps_Recurring_Invalid_Value_Exception |
| 49 |
*/ |
| 50 |
public function set_type( string $type ): self { |
| 51 |
if ( ! in_array( $type, $this->valid_types, true ) ) { |
| 52 |
$class = get_class( $this ); |
| 53 |
throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$type is not a valid value for `type` in $class." ); |
| 54 |
} |
| 55 |
|
| 56 |
$this->type = $type; |
| 57 |
|
| 58 |
return $this; |
| 59 |
} |
| 60 |
|
| 61 |
public function set_price( int $price ): self { |
| 62 |
$this->price = $price; |
| 63 |
|
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param DateTime|string $end |
| 69 |
* |
| 70 |
* @throws Exception |
| 71 |
*/ |
| 72 |
public function set_end( $end ): self { |
| 73 |
if ( is_string( $end ) ) { |
| 74 |
$this->end = new DateTime( $end ); |
| 75 |
} else { |
| 76 |
$this->end = $end; |
| 77 |
} |
| 78 |
|
| 79 |
return $this; |
| 80 |
} |
| 81 |
|
| 82 |
public function set_explanation( string $explanation ): self { |
| 83 |
$this->explanation = $explanation; |
| 84 |
|
| 85 |
return $this; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @param DateTime|string $event_date |
| 90 |
* |
| 91 |
* @throws Exception |
| 92 |
*/ |
| 93 |
public function set_event_date( $event_date ): self { |
| 94 |
if ( is_string( $event_date ) ) { |
| 95 |
$this->event_date = new DateTime( $event_date ); |
| 96 |
} else { |
| 97 |
$this->event_date = $event_date; |
| 98 |
} |
| 99 |
|
| 100 |
return $this; |
| 101 |
} |
| 102 |
|
| 103 |
public function set_event_text( string $event_text ): self { |
| 104 |
$this->event_text = $event_text; |
| 105 |
|
| 106 |
return $this; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @param array|WC_Vipps_Agreement_Campaign_Period $period |
| 111 |
*/ |
| 112 |
public function set_period( $period ): self { |
| 113 |
if ( is_array( $period ) ) { |
| 114 |
$period = new WC_Vipps_Agreement_Campaign_Period( $period ); |
| 115 |
} |
| 116 |
|
| 117 |
$this->period = $period; |
| 118 |
|
| 119 |
return $this; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* @param array|WC_Vipps_Agreement_Interval $interval |
| 124 |
*/ |
| 125 |
public function set_interval( $interval ): self { |
| 126 |
if ( is_array( $interval ) ) { |
| 127 |
$interval = new WC_Vipps_Agreement_Interval( $interval ); |
| 128 |
} |
| 129 |
|
| 130 |
$this->interval = $interval; |
| 131 |
|
| 132 |
return $this; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @throws WC_Vipps_Recurring_Missing_Value_Exception |
| 137 |
*/ |
| 138 |
public function to_array( bool $check_required = false ): array { |
| 139 |
if ( $check_required ) { |
| 140 |
$this->check_required( $this->type ); |
| 141 |
} |
| 142 |
|
| 143 |
return array_merge( |
| 144 |
[ |
| 145 |
"type" => $this->type, |
| 146 |
"price" => $this->price, |
| 147 |
], |
| 148 |
$this->conditional( "end", $this->end ), |
| 149 |
$this->conditional( "explanation", $this->explanation ), |
| 150 |
$this->conditional( "eventDate", $this->event_date ), |
| 151 |
$this->conditional( "eventText", $this->event_text ), |
| 152 |
$this->conditional( "period", $this->period ), |
| 153 |
$this->conditional( "interval", $this->interval ), |
| 154 |
); |
| 155 |
} |
| 156 |
} |
| 157 |
|