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