CouponMapper.php
59 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Api\Utils\Coupons; |
| 6 | |
| 7 | use Automattic\WooCommerce\Api\Enums\Coupons\CouponStatus; |
| 8 | use Automattic\WooCommerce\Api\Enums\Coupons\DiscountType; |
| 9 | use Automattic\WooCommerce\Api\Types\Coupons\Coupon; |
| 10 | |
| 11 | /** |
| 12 | * Maps a WC_Coupon to the Coupon DTO. |
| 13 | */ |
| 14 | class CouponMapper { |
| 15 | /** |
| 16 | * Map a WC_Coupon to the Coupon DTO. |
| 17 | * |
| 18 | * @param \WC_Coupon $wc_coupon The WooCommerce coupon object. |
| 19 | * @return Coupon |
| 20 | */ |
| 21 | public static function from_wc_coupon( \WC_Coupon $wc_coupon ): Coupon { |
| 22 | $coupon = new Coupon(); |
| 23 | |
| 24 | $raw_discount_type = (string) $wc_coupon->get_discount_type(); |
| 25 | $raw_status = (string) $wc_coupon->get_status(); |
| 26 | |
| 27 | $coupon->id = $wc_coupon->get_id(); |
| 28 | $coupon->code = $wc_coupon->get_code(); |
| 29 | $coupon->description = $wc_coupon->get_description(); |
| 30 | $coupon->discount_type = DiscountType::tryFrom( $raw_discount_type ) ?? DiscountType::Other; |
| 31 | $coupon->raw_discount_type = $raw_discount_type; |
| 32 | $coupon->amount = (float) $wc_coupon->get_amount(); |
| 33 | $coupon->status = '' === $raw_status |
| 34 | ? CouponStatus::Draft |
| 35 | : ( CouponStatus::tryFrom( $raw_status ) ?? CouponStatus::Other ); |
| 36 | $coupon->raw_status = $raw_status; |
| 37 | $coupon->date_created = $wc_coupon->get_date_created()?->format( \DateTimeInterface::ATOM ); |
| 38 | $coupon->date_modified = $wc_coupon->get_date_modified()?->format( \DateTimeInterface::ATOM ); |
| 39 | $coupon->date_expires = $wc_coupon->get_date_expires()?->format( \DateTimeInterface::ATOM ); |
| 40 | $coupon->usage_count = $wc_coupon->get_usage_count(); |
| 41 | $coupon->individual_use = $wc_coupon->get_individual_use(); |
| 42 | $coupon->product_ids = $wc_coupon->get_product_ids(); |
| 43 | $coupon->excluded_product_ids = $wc_coupon->get_excluded_product_ids(); |
| 44 | $coupon->usage_limit = $wc_coupon->get_usage_limit(); |
| 45 | $coupon->usage_limit_per_user = $wc_coupon->get_usage_limit_per_user(); |
| 46 | $coupon->limit_usage_to_x_items = $wc_coupon->get_limit_usage_to_x_items(); |
| 47 | $coupon->free_shipping = $wc_coupon->get_free_shipping(); |
| 48 | $coupon->product_categories = $wc_coupon->get_product_categories(); |
| 49 | $coupon->excluded_product_categories = $wc_coupon->get_excluded_product_categories(); |
| 50 | $coupon->exclude_sale_items = $wc_coupon->get_exclude_sale_items(); |
| 51 | $coupon->minimum_amount = (float) $wc_coupon->get_minimum_amount(); |
| 52 | $coupon->maximum_amount = (float) $wc_coupon->get_maximum_amount(); |
| 53 | $coupon->email_restrictions = $wc_coupon->get_email_restrictions(); |
| 54 | $coupon->used_by = $wc_coupon->get_used_by(); |
| 55 | |
| 56 | return $coupon; |
| 57 | } |
| 58 | } |
| 59 |