1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ipag\Classes; |
4
|
|
|
|
5
|
|
|
use Ipag\Classes\Contracts\Emptiable; |
6
|
|
|
use Ipag\Classes\Contracts\ObjectSerializable; |
7
|
|
|
use Ipag\Classes\Traits\EmptiableTrait; |
8
|
|
|
|
9
|
|
|
final class Subscription extends BaseResource implements Emptiable, ObjectSerializable |
10
|
|
|
{ |
11
|
|
|
use EmptiableTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $profileId; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
private $frequency; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $interval; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $start; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $cycle; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var float |
40
|
|
|
*/ |
41
|
|
|
private $amount; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
private $trial; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
private $trialCycle; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
private $trialFrequency; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var float |
60
|
|
|
*/ |
61
|
|
|
private $trialAmount; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var Validators\SubscriptionValidator |
65
|
|
|
*/ |
66
|
|
|
private $validator; |
67
|
|
|
|
68
|
|
|
private function validator() |
69
|
|
|
{ |
70
|
|
|
if (is_null($this->validator)) { |
71
|
|
|
$this->validator = new Validators\SubscriptionValidator(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->validator; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return int |
79
|
|
|
*/ |
80
|
|
|
public function getFrequency() |
81
|
|
|
{ |
82
|
|
|
return $this->frequency; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getInterval() |
89
|
|
|
{ |
90
|
|
|
return $this->interval; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getStart() |
97
|
|
|
{ |
98
|
|
|
return $this->start; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return int |
103
|
|
|
*/ |
104
|
|
|
public function getCycle() |
105
|
|
|
{ |
106
|
|
|
return $this->cycle; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return float |
111
|
|
|
*/ |
112
|
|
|
public function getAmount() |
113
|
|
|
{ |
114
|
|
|
return $this->amount; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function isTrial() |
121
|
|
|
{ |
122
|
|
|
return $this->trial; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return int |
127
|
|
|
*/ |
128
|
|
|
public function getTrialCycle() |
129
|
|
|
{ |
130
|
|
|
return $this->trialCycle; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
|
|
public function getTrialFrequency() |
137
|
|
|
{ |
138
|
|
|
return $this->trialFrequency; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return float |
143
|
|
|
*/ |
144
|
|
|
public function getTrialAmount() |
145
|
|
|
{ |
146
|
|
|
return $this->trialAmount; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param int $frequency the frequency |
151
|
|
|
*/ |
152
|
|
|
public function setFrequency($frequency) |
153
|
|
|
{ |
154
|
|
|
if (!$this->validator()->isValidFrequency($frequency)) { |
155
|
|
|
throw new \UnexpectedValueException( |
156
|
|
|
'A frequencia não é válida ou não tem entre 1 e 2 caracteres' |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
$this->frequency = $frequency; |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $interval |
166
|
|
|
*/ |
167
|
|
|
public function setInterval($interval) |
168
|
|
|
{ |
169
|
|
|
if (!$this->validator()->isValidInterval($interval)) { |
170
|
|
|
throw new \UnexpectedValueException( |
171
|
|
|
'O intervalo (interval) não é válido' |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$this->interval = $interval; |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $start |
182
|
|
|
*/ |
183
|
|
|
public function setStart($start) |
184
|
|
|
{ |
185
|
|
|
if (!$this->getDateUtil()->isValid($start)) { |
186
|
|
|
throw new \UnexpectedValueException( |
187
|
|
|
'A data de início não é valida ou está em formato incorreto, deve ser informada utilizando o formato dd/mm/aaaa' |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->start = $start; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param int $cycle |
198
|
|
|
*/ |
199
|
|
|
public function setCycle($cycle) |
200
|
|
|
{ |
201
|
|
|
if (!$this->validator()->isValidCycle($cycle)) { |
202
|
|
|
throw new \UnexpectedValueException( |
203
|
|
|
'O ciclo deve ser númerico e ter entre 1 e 3 caracteres.' |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
$this->cycle = $cycle; |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param float $amount |
213
|
|
|
*/ |
214
|
|
|
public function setAmount($amount) |
215
|
|
|
{ |
216
|
|
|
$this->amount = $this->getNumberUtil()->convertToDouble($amount); |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param bool $trial |
223
|
|
|
*/ |
224
|
|
|
public function setTrial($trial) |
225
|
|
|
{ |
226
|
|
|
$this->trial = (bool) $trial; |
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param int $trialCycle |
233
|
|
|
*/ |
234
|
|
|
public function setTrialCycle($trialCycle) |
235
|
|
|
{ |
236
|
|
|
if (!$this->validator()->isValidCycle($trialCycle)) { |
237
|
|
|
throw new \UnexpectedValueException( |
238
|
|
|
'O ciclo trial deve ser númerico e ter entre 1 e 3 caracteres.' |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
$this->trialCycle = $trialCycle; |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param int $trialFrequency |
248
|
|
|
*/ |
249
|
|
|
public function setTrialFrequency($trialFrequency) |
250
|
|
|
{ |
251
|
|
|
if (!$this->validator()->isValidFrequency($trialFrequency)) { |
252
|
|
|
throw new \UnexpectedValueException( |
253
|
|
|
'A frequencia trial não é válida ou não tem entre 1 e 2 caracteres' |
254
|
|
|
); |
255
|
|
|
} |
256
|
|
|
$this->trialFrequency = $trialFrequency; |
257
|
|
|
|
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param float $trialAmount |
263
|
|
|
*/ |
264
|
|
|
public function setTrialAmount($trialAmount) |
265
|
|
|
{ |
266
|
|
|
$this->trialAmount = $this->getNumberUtil()->convertToDouble($trialAmount); |
267
|
|
|
|
268
|
|
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @return string |
273
|
|
|
*/ |
274
|
|
|
public function getProfileId() |
275
|
|
|
{ |
276
|
|
|
return $this->profileId; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param string $profileId |
281
|
|
|
*/ |
282
|
|
|
public function setProfileId($profileId) |
283
|
|
|
{ |
284
|
|
|
if (!$this->validator()->isValidProfileId($profileId)) { |
285
|
|
|
throw new \UnexpectedValueException( |
286
|
|
|
'Profile ID deve ser somente númerico e ter no máximo 32 caracteres.' |
287
|
|
|
); |
288
|
|
|
} |
289
|
|
|
$this->profileId = $profileId; |
290
|
|
|
|
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function serialize() |
295
|
|
|
{ |
296
|
|
|
if ($this->isEmpty()) { |
297
|
|
|
return []; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return [ |
301
|
|
|
'profile_id' => urlencode($this->getProfileId()), |
302
|
|
|
'frequencia' => urlencode($this->getFrequency()), |
303
|
|
|
'intervalo' => urlencode($this->getInterval()), |
304
|
|
|
'inicio' => urlencode($this->getStart()), |
305
|
|
|
'ciclos' => urlencode($this->getCycle()), |
306
|
|
|
'valor_rec' => urlencode($this->getAmount()), |
307
|
|
|
'trial' => urlencode($this->isTrial()), |
308
|
|
|
'trial_ciclos' => urlencode($this->getTrialCycle()), |
309
|
|
|
'trial_frequencia' => urlencode($this->getTrialFrequency()), |
310
|
|
|
'trial_valor' => urlencode($this->getTrialAmount()), |
311
|
|
|
]; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|