PaymentSubscription::installments()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Ipag\Sdk\Model;
4
5
use Ipag\Sdk\Model\Schema\Mutator;
6
use Ipag\Sdk\Model\Schema\Schema;
7
use Ipag\Sdk\Model\Schema\SchemaBuilder;
8
use Kubinyete\Assertation\Assert;
9
10
/**
11
 * PaymentSubscription Class
12
 *
13
 * Classe responsável por representar o recurso Payment Subscription.
14
 */
15
class PaymentSubscription extends Model
16
{
17
    /**
18
     *  @param array $data
19
     *  array de dados do Payment Subscription.
20
     *
21
     *  + [`'frequency'`] int (opcional).
22
     *  + [`'interval'`] string (opcional).
23
     *  + [`'start_date'`] string (opcional).
24
     *  + [`'amount'`] float (opcional).
25
     *  + [`'installments'`] int (opcional).
26
     *  + [`'cycles'`] int (opcional).
27
     *
28
     *  + [`'trial'`] array (opcional) dos dados do Trial.
29
     *  + &emsp; [`'amount'`] float (opcional).
30
     *  + &emsp; [`'cycles'`] float (opcional).
31
     *  + &emsp; [`'frequency'`] int (opcional).
32
     */
33
    public function __construct(?array $data = [])
34
    {
35
        parent::__construct($data);
36
    }
37
38
    protected function schema(SchemaBuilder $schema): Schema
39
    {
40
        $schema->int('frequency')->nullable(); // 1 ~ 12
41
        $schema->string('interval')->nullable(); // day, week, month
42
        $schema->string('start_date')->nullable(); //Y-m-d
43
        $schema->float('amount')->nullable();
44
        $schema->int('installments')->nullable();
45
        $schema->int('cycles')->nullable();
46
47
        $schema->has('trial', Trial::class)->nullable();
48
49
        return $schema->build();
50
    }
51
52
    protected function frequency(): Mutator
53
    {
54
        return new Mutator(
55
            null,
56
            fn($value, $ctx) =>
57
            is_null($value) ? $value :
58
            (
59
                Assert::value(intval($value))->gt(0)->get()
60
                ?? $ctx->raise('inválido (informe um valor de 1 à 12)')
61
            )
62
        );
63
    }
64
65
    protected function start_date(): Mutator
66
    {
67
        return new Mutator(
68
            null,
69
            function ($value, $ctx) {
70
                $d = \DateTime::createFromFormat('Y-m-d', $value);
71
72
                return is_null($value) ||
73
                    ($d && $d->format('Y-m-d') === $value) ?
74
                    $value : $ctx->raise('inválido');
75
            }
76
        );
77
    }
78
79
    protected function amount(): Mutator
80
    {
81
        return new Mutator(
82
            null,
83
            fn($value, $ctx) =>
84
            is_null($value) ? $value :
85
            (
86
                Assert::value(floatval($value))->gte(0)->get()
87
                ?? $ctx->raise('inválido')
88
            )
89
        );
90
    }
91
92
    protected function installments(): Mutator
93
    {
94
        return new Mutator(
95
            null,
96
            fn($value, $ctx) =>
97
            is_null($value) ? $value :
98
            (
99
                Assert::value(intval($value))->gt(0)->get()
100
                ?? $ctx->raise('inválido')
101
            )
102
        );
103
    }
104
105
    protected function cycles(): Mutator
106
    {
107
        return new Mutator(
108
            null,
109
            fn($value, $ctx) =>
110
            is_null($value) ? $value :
111
            (
112
                Assert::value(intval($value))->gt(0)->get()
113
                ?? $ctx->raise('inválido')
114
            )
115
        );
116
    }
117
118
    /**
119
     * Retorna o valor da propriedade `frequency`
120
     *
121
     * @return integer|null
122
     */
123
    public function getFrequency(): ?int
124
    {
125
        return $this->get('frequency');
126
    }
127
128
    /**
129
     * Seta o valor da propriedade `frequency`
130
     *
131
     * @param integer|null $frequency
132
     * @return self
133
     */
134
    public function setFrequency(?int $frequency = null): self
135
    {
136
        $this->set('frequency', $frequency);
137
        return $this;
138
    }
139
140
    /**
141
     * Retorna o valor da propriedade `interval`
142
     *
143
     * @return string|null
144
     */
145
    public function getInterval(): ?string
146
    {
147
        return $this->get('interval');
148
    }
149
150
    /**
151
     * Seta o valor da propriedade `interval`
152
     *
153
     * @param string|null $interval
154
     * @return self
155
     */
156
    public function setInterval(?string $interval = null): self
157
    {
158
        $this->set('interval', $interval);
159
        return $this;
160
    }
161
162
    /**
163
     * Retorna o valor da propriedade `trial`
164
     *
165
     * @return string|null
166
     */
167
    public function getStartDate(): ?string
168
    {
169
        return $this->get('start_date');
170
    }
171
172
    /**
173
     * Seta o valor da propriedade `trial`
174
     *
175
     * @param string|null $startDate
176
     * @return self
177
     */
178
    public function setStartDate(?string $startDate = null): self
179
    {
180
        $this->set('start_date', $startDate);
181
        return $this;
182
    }
183
184
    /**
185
     * Retorna o valor da propriedade `amount`
186
     *
187
     * @return float|null
188
     */
189
    public function getAmount(): ?float
190
    {
191
        return $this->get('amount');
192
    }
193
194
    /**
195
     * Seta o valor da propriedade `amount`
196
     *
197
     * @param float|null $amount
198
     * @return self
199
     */
200
    public function setAmount(?float $amount = null): self
201
    {
202
        $this->set('amount', $amount);
203
        return $this;
204
    }
205
206
    /**
207
     * Retorna o valor da propriedade `installments`
208
     *
209
     * @return integer|null
210
     */
211
    public function getInstallments(): ?int
212
    {
213
        return $this->get('installments');
214
    }
215
216
    /**
217
     * Seta o valor da propriedade `installments`
218
     *
219
     * @param integer|null $installments
220
     * @return self
221
     */
222
    public function setInstallments(?int $installments = null): self
223
    {
224
        $this->set('installments', $installments);
225
        return $this;
226
    }
227
228
    /**
229
     * Retorna o valor da propriedade `cycles`
230
     *
231
     * @return integer|null
232
     */
233
    public function getCycles(): ?int
234
    {
235
        return $this->get('cycles');
236
    }
237
238
    /**
239
     * Seta o valor da propriedade `cycles`
240
     *
241
     * @param integer|null $cycles
242
     * @return self
243
     */
244
    public function setCycles(?int $cycles = null): self
245
    {
246
        $this->set('cycles', $cycles);
247
        return $this;
248
    }
249
250
    /**
251
     * Retorna o objeto `Trial` do `Subscription`.
252
     *
253
     * @return Trial|null
254
     */
255
    public function getTrial(): ?Trial
256
    {
257
        return $this->get('trial');
258
    }
259
260
    /**
261
     * Seta o objeto `Trial` do `Subscription`.
262
     *
263
     * @param Trial|null $trial
264
     * @return self
265
     */
266
    public function setTrial(?Trial $trial = null): self
267
    {
268
        $this->set('trial', $trial);
269
        return $this;
270
    }
271
272
}