Holder::getCpfCnpj()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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
 * Holder Class
12
 *
13
 * Classe responsável por representar o recurso Holder.
14
 *
15
 */
16
class Holder extends Model
17
{
18
19
    /**
20
     *  @param array $data
21
     *  array de dados do Holder.
22
     *
23
     *  + [`'name'`] string.
24
     *  + [`'cpfCnpj'`] string.
25
     *  + [`'mobilePhone'`] string.
26
     *  + [`'birthdate'`] string.
27
     *  + [`'address'`] array (opcional) dos dados do Address.
28
     *  + &emsp; [`'street'`] string (opcional).
29
     *  + &emsp; [`'number'`] string (opcional).
30
     *  + &emsp; [`'district'`] string (opcional).
31
     *  + &emsp; [`'city'`] string (opcional).
32
     *  + &emsp; [`'state'`] string (opcional).
33
     *  + &emsp; [`'zipcode'`] string (opcional).
34
     */
35
    public function __construct(?array $data = [])
36
    {
37
        parent::__construct($data);
38
    }
39
40
    protected function schema(SchemaBuilder $schema): Schema
41
    {
42
        $schema->string('name')->nullable();
43
        $schema->string('cpfCnpj')->nullable();
44
        $schema->string('mobilePhone')->nullable();
45
        $schema->string('birthdate')->nullable();
46
47
        $schema->has('address', Address::class)->nullable();
48
49
        return $schema->build();
50
    }
51
52
    /**
53
     * Retorna o valor da propriedade `name`.
54
     *
55
     * @return string|null
56
     */
57
    public function getName(): ?string
58
    {
59
        return $this->get('name');
60
    }
61
62
    /**
63
     * Seta o valor da propriedade `name`.
64
     *
65
     * @param string|null $name
66
     * @return self
67
     */
68
    public function setName(?string $name = null): self
69
    {
70
        $this->set('name', $name);
71
        return $this;
72
    }
73
74
    protected function cpfCnpj(): Mutator
75
    {
76
        return new Mutator(
77
            null,
78
            fn($value, $ctx) =>
79
            is_null($value) ?
80
            $value :
81
            Assert::value($value)->asCpf(false)->or()->asCnpj(false)->get() ?? $ctx->raise('inválido')
82
        );
83
    }
84
85
    /**
86
     * Retorna o valor da propriedade `cpfCnpj`.
87
     *
88
     * @return string|null
89
     */
90
    public function getCpfCnpj(): ?string
91
    {
92
        return $this->get('cpfCnpj');
93
    }
94
95
    /**
96
     * Seta o valor da propriedade `cpfCnpj`.
97
     *
98
     * @param string|null $cpfCnpj
99
     * @return self
100
     */
101
    public function setCpfCnpj(?string $cpfCnpj = null): self
102
    {
103
        $this->set('cpfCnpj', $cpfCnpj);
104
        return $this;
105
    }
106
107
    protected function mobilePhone(): Mutator
108
    {
109
        return new Mutator(
110
            null,
111
            fn($value, $ctx) =>
112
            is_null($value) ?
113
            $value :
114
            Assert::value($value)->asDigits()->lbetween(10, 11)->get() ?? $ctx->raise('inválido')
115
        );
116
    }
117
118
    /**
119
     * Retorna o valor da propriedade `mobilePhone`.
120
     *
121
     * @return string|null
122
     */
123
    public function getMobilePhone(): ?string
124
    {
125
        return $this->get('mobilePhone');
126
    }
127
128
    /**
129
     * Seta o valor da propriedade `mobilePhone`.
130
     *
131
     * @param string|null $phone
132
     * @return self
133
     */
134
    public function setMobilePhone(?string $phone = null): self
135
    {
136
        $this->set('mobilePhone', $phone);
137
        return $this;
138
    }
139
140
    protected function birthdate(): Mutator
141
    {
142
        return new Mutator(
143
            null,
144
            function ($value, $ctx) {
145
                $d = \DateTime::createFromFormat('Y-m-d', $value);
146
147
                return is_null($value) ||
148
                    ($d && $d->format('Y-m-d') === $value) ?
149
                    $value : $ctx->raise('inválido');
150
            }
151
        );
152
    }
153
154
    /**
155
     * Retorna o valor da propriedade `birthdate`.
156
     *
157
     * @return string|null
158
     */
159
    public function getBirthdate(): ?string
160
    {
161
        return $this->get('birthdate');
162
    }
163
164
    /**
165
     * Seta o valor da propriedade `birthdate`.
166
     *
167
     * @param string|null $birthdate
168
     * @return self
169
     */
170
    public function setBirthdate(?string $birthdate = null): self
171
    {
172
        $this->set('birthdate', $birthdate);
173
        return $this;
174
    }
175
176
    /**
177
     * Retorna o objeto endereço do cliente.
178
     *
179
     * @return Address|null
180
     */
181
    public function getAddress(): ?Address
182
    {
183
        return $this->get('address');
184
    }
185
186
    /**
187
     * Seta o objeto endereço do cliente.
188
     *
189
     * @param Address|null $address
190
     * @return self
191
     */
192
    public function setAddress(?Address $address = null): self
193
    {
194
        $this->set('address', $address);
195
        return $this;
196
    }
197
198
}