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