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