SubscriptionQueryData.php
122 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Subscriptions\DataTransferObjects; |
| 4 | |
| 5 | use DateTime; |
| 6 | use Give\Framework\Support\Facades\DateTime\Temporal; |
| 7 | use Give\Framework\Support\ValueObjects\Money; |
| 8 | use Give\Subscriptions\Models\Subscription; |
| 9 | use Give\Subscriptions\ValueObjects\SubscriptionMode; |
| 10 | use Give\Subscriptions\ValueObjects\SubscriptionPeriod; |
| 11 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 12 | |
| 13 | /** |
| 14 | * Class SubscriptionObjectData |
| 15 | * |
| 16 | * @since 2.19.6 |
| 17 | */ |
| 18 | final class SubscriptionQueryData |
| 19 | { |
| 20 | /** |
| 21 | * @var int |
| 22 | */ |
| 23 | public $id; |
| 24 | /** |
| 25 | * @var DateTime |
| 26 | */ |
| 27 | public $createdAt; |
| 28 | /** |
| 29 | * @var DateTime |
| 30 | */ |
| 31 | public $renewsAt; |
| 32 | /** |
| 33 | * @var string |
| 34 | */ |
| 35 | public $status; |
| 36 | /** |
| 37 | * @var int |
| 38 | */ |
| 39 | public $donorId; |
| 40 | /** |
| 41 | * @var SubscriptionPeriod |
| 42 | */ |
| 43 | public $period; |
| 44 | /** |
| 45 | * @var string |
| 46 | */ |
| 47 | public $frequency; |
| 48 | /** |
| 49 | * @var int |
| 50 | */ |
| 51 | public $installments; |
| 52 | /** |
| 53 | * @var string |
| 54 | */ |
| 55 | public $transactionId; |
| 56 | /** |
| 57 | * @var SubscriptionMode |
| 58 | */ |
| 59 | public $mode; |
| 60 | /** |
| 61 | * @var Money |
| 62 | */ |
| 63 | public $amount; |
| 64 | /** |
| 65 | * @var Money |
| 66 | */ |
| 67 | public $feeAmountRecovered; |
| 68 | /** |
| 69 | * @var string |
| 70 | */ |
| 71 | public $gatewayId; |
| 72 | /** |
| 73 | * @var string |
| 74 | */ |
| 75 | public $gatewaySubscriptionId; |
| 76 | /** |
| 77 | * @var int |
| 78 | */ |
| 79 | public $donationFormId; |
| 80 | |
| 81 | /** |
| 82 | * Convert data from Subscription Object to Subscription Model |
| 83 | * |
| 84 | * @since 2.19.6 |
| 85 | */ |
| 86 | public static function fromObject($subscriptionQueryObject): self |
| 87 | { |
| 88 | $self = new static(); |
| 89 | |
| 90 | $self->id = (int)$subscriptionQueryObject->id; |
| 91 | $self->createdAt = Temporal::toDateTime($subscriptionQueryObject->createdAt); |
| 92 | $self->renewsAt = isset($subscriptionQueryObject->renewsAt) ? Temporal::toDateTime( |
| 93 | $subscriptionQueryObject->renewsAt |
| 94 | ) : null; |
| 95 | $self->donorId = (int)$subscriptionQueryObject->donorId; |
| 96 | $self->period = new SubscriptionPeriod($subscriptionQueryObject->period); |
| 97 | $self->frequency = (int)$subscriptionQueryObject->frequency; |
| 98 | $self->installments = (int)$subscriptionQueryObject->installments; |
| 99 | $self->transactionId = $subscriptionQueryObject->transactionId; |
| 100 | $self->mode = new SubscriptionMode($subscriptionQueryObject->mode); |
| 101 | $self->amount = Money::fromDecimal($subscriptionQueryObject->amount, $subscriptionQueryObject->currency ?? give_get_currency()); |
| 102 | $self->feeAmountRecovered = Money::fromDecimal($subscriptionQueryObject->feeAmount, |
| 103 | $subscriptionQueryObject->currency ?? give_get_currency()); |
| 104 | $self->status = new SubscriptionStatus($subscriptionQueryObject->status); |
| 105 | $self->gatewayId = $subscriptionQueryObject->gatewayId; |
| 106 | $self->gatewaySubscriptionId = $subscriptionQueryObject->gatewaySubscriptionId; |
| 107 | $self->donationFormId = (int)$subscriptionQueryObject->donationFormId; |
| 108 | |
| 109 | return $self; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Convert DTO to Subscription |
| 114 | */ |
| 115 | public function toSubscription(): Subscription |
| 116 | { |
| 117 | $attributes = get_object_vars($this); |
| 118 | |
| 119 | return new Subscription($attributes); |
| 120 | } |
| 121 | } |
| 122 |