1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Shapin\Stripe\Model\Coupon; |
11
|
|
|
|
12
|
|
|
use Money\Currency; |
13
|
|
|
use Money\Money; |
14
|
|
|
use Shapin\Stripe\Model\ContainsMetadata; |
15
|
|
|
use Shapin\Stripe\Model\CreatableFromArray; |
16
|
|
|
use Shapin\Stripe\Model\LivemodeTrait; |
17
|
|
|
use Shapin\Stripe\Model\MetadataCollection; |
18
|
|
|
use Shapin\Stripe\Model\MetadataTrait; |
19
|
|
|
|
20
|
|
|
final class Coupon implements CreatableFromArray, ContainsMetadata |
21
|
|
|
{ |
22
|
|
|
use LivemodeTrait; |
23
|
|
|
use MetadataTrait; |
24
|
|
|
|
25
|
|
|
const DURATION_FOREVER = 'forever'; |
26
|
|
|
const DURATION_ONCE = 'once'; |
27
|
|
|
const DURATION_REPEATING = 'repeating'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $id; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ?Money |
36
|
|
|
*/ |
37
|
|
|
private $amountOff; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \DateTimeImmutable |
41
|
|
|
*/ |
42
|
|
|
private $createdAt; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ?Currency |
46
|
|
|
*/ |
47
|
|
|
private $currency; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $duration; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var int |
56
|
|
|
*/ |
57
|
|
|
private $durationInMonths; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var ?int |
61
|
|
|
*/ |
62
|
|
|
private $maxRedemptions; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
private $name; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var float |
71
|
|
|
*/ |
72
|
|
|
private $percentOff; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var ?\DateTimeImmutable |
76
|
|
|
*/ |
77
|
|
|
private $redeemBy; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var int |
81
|
|
|
*/ |
82
|
|
|
private $timesRedeemed; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var bool |
86
|
|
|
*/ |
87
|
|
|
private $valid; |
88
|
|
|
|
89
|
9 |
|
public static function createFromArray(array $data): self |
90
|
|
|
{ |
91
|
9 |
|
$model = new self(); |
92
|
|
|
|
93
|
9 |
|
if (isset($data['currency'])) { |
94
|
|
|
$currency = new Currency(strtoupper($data['currency'])); |
95
|
|
|
|
96
|
|
|
$model->amountOff = isset($data['amount_off']) ? new Money($data['amount_off'], $currency) : null; |
97
|
|
|
$model->currency = $currency; |
98
|
|
|
} |
99
|
|
|
|
100
|
9 |
|
$model->id = $data['id']; |
101
|
9 |
|
$model->createdAt = new \DateTimeImmutable('@'.$data['created']); |
102
|
9 |
|
$model->duration = $data['duration']; |
103
|
9 |
|
$model->durationInMonths = (int) $data['duration_in_months']; |
104
|
9 |
|
$model->live = $data['livemode']; |
105
|
9 |
|
$model->maxRedemptions = $data['max_redemptions'] ?? null; |
106
|
9 |
|
$model->metadata = MetadataCollection::createFromArray($data['metadata']); |
|
|
|
|
107
|
9 |
|
$model->name = $data['name']; |
108
|
9 |
|
$model->percentOff = (float) $data['percent_off']; |
109
|
9 |
|
$model->redeemBy = isset($data['redeem_by']) ? new \DateTimeImmutable('@'.$data['redeem_by']) : null; |
110
|
9 |
|
$model->timesRedeemed = (int) $data['times_redeemed']; |
111
|
9 |
|
$model->valid = (bool) $data['valid']; |
112
|
|
|
|
113
|
9 |
|
return $model; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function isRepeating(): bool |
117
|
|
|
{ |
118
|
|
|
return self::DURATION_REPEATING === $this->duration; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function isOnce(): bool |
122
|
|
|
{ |
123
|
|
|
return self::DURATION_ONCE === $this->duration; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function isForever(): bool |
127
|
|
|
{ |
128
|
|
|
return self::DURATION_FOREVER === $this->duration; |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
public function getId(): string |
132
|
|
|
{ |
133
|
1 |
|
return $this->id; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function getAmountOff(): ?Money |
137
|
|
|
{ |
138
|
1 |
|
return $this->amountOff; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
public function getCreatedAt(): \DateTimeImmutable |
142
|
|
|
{ |
143
|
1 |
|
return $this->createdAt; |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
public function getCurrency(): ?Currency |
147
|
|
|
{ |
148
|
1 |
|
return $this->currency; |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
public function getDuration(): string |
152
|
|
|
{ |
153
|
1 |
|
return $this->duration; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
public function getDurationInMonths(): int |
157
|
|
|
{ |
158
|
1 |
|
return $this->durationInMonths; |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
public function getMaxRedemptions(): ?int |
162
|
|
|
{ |
163
|
1 |
|
return $this->maxRedemptions; |
164
|
|
|
} |
165
|
|
|
|
166
|
1 |
|
public function getName(): ?string |
167
|
|
|
{ |
168
|
1 |
|
return $this->name; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getPercentOff(): float |
172
|
|
|
{ |
173
|
|
|
return $this->percentOff; |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
public function getRedeemBy(): ?\DateTimeImmutable |
177
|
|
|
{ |
178
|
1 |
|
return $this->redeemBy; |
179
|
|
|
} |
180
|
|
|
|
181
|
1 |
|
public function getTimesRedeemed(): int |
182
|
|
|
{ |
183
|
1 |
|
return $this->timesRedeemed; |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
public function isValid(): bool |
187
|
|
|
{ |
188
|
1 |
|
return $this->valid; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..