Product::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
 * Product Class
12
 *
13
 * Classe responsável por representar o recurso Product.
14
 *
15
 */
16
final class Product extends Model
17
{
18
    /**
19
     *  @param array $data
20
     *  array de dados do Product.
21
     *
22
     *  + [`'name'`] string.
23
     *  + [`'unit_price'`] float.
24
     *  + [`'quantity'`] int.
25
     *  + [`'sku'`] string.
26
     *  + [`'description'`] string.
27
     */
28
    public function __construct(?array $data = [])
29
    {
30
        parent::__construct($data);
31
    }
32
33
    public function schema(SchemaBuilder $schema): Schema
34
    {
35
        $schema->string('name')->nullable();
36
        $schema->float('unit_price')->nullable();
37
        $schema->int('quantity')->nullable();
38
        $schema->string('sku')->nullable();
39
        $schema->string('description')->nullable();
40
41
        return $schema->build();
42
    }
43
44
    protected function unit_price(): Mutator
45
    {
46
        return new Mutator(
47
            null,
48
            fn($value, $ctx) =>
49
            is_null($value) ? $value :
50
            (
51
                Assert::value(floatval($value))->gte(0)->get()
52
                ?? $ctx->raise('inválido')
53
            )
54
        );
55
    }
56
57
    protected function quantity(): Mutator
58
    {
59
        return new Mutator(
60
            null,
61
            fn($value, $ctx) =>
62
            is_null($value) ? $value :
63
            (
64
                Assert::value(intval($value))->gt(0)->get()
65
                ?? $ctx->raise('inválido')
66
            )
67
        );
68
    }
69
70
    /**
71
     * Retorna o valor da propriedade `name`.
72
     *
73
     * @return string|null
74
     */
75
    public function getName(): ?string
76
    {
77
        return $this->get('name');
78
    }
79
80
    /**
81
     * Seta o valor da propriedade `name`.
82
     *
83
     * @param string|null $name
84
     * @return self
85
     */
86
    public function setName(?string $name = null): self
87
    {
88
        $this->set('name', $name);
89
        return $this;
90
    }
91
92
    /**
93
     * Retorna o valor da propriedade `unit_price`.
94
     *
95
     * @return float|null
96
     */
97
    public function getUnitPrice(): ?float
98
    {
99
        return $this->get('unit_price');
100
    }
101
102
    /**
103
     * Seta o valor da propriedade `unit_price`.
104
     *
105
     * @param float|null $unit_price
106
     * @return self
107
     */
108
    public function setUnitPrice(?float $unit_price = null): self
109
    {
110
        $this->set('unit_price', $unit_price);
111
        return $this;
112
    }
113
114
    /**
115
     * Retorna o valor da propriedade `quantity`.
116
     *
117
     * @return integer|null
118
     */
119
    public function getQuantity(): ?int
120
    {
121
        return $this->get('quantity');
122
    }
123
124
    /**
125
     * Seta o valor da propriedade `quantity`.
126
     *
127
     * @param integer|null $quantity
128
     * @return self
129
     */
130
    public function setQuantity(?int $quantity = null): self
131
    {
132
        $this->set('quantity', $quantity);
133
        return $this;
134
    }
135
136
    /**
137
     * Retorna o valor da propriedade `sku`.
138
     *
139
     * @return string|null
140
     */
141
    public function getSku(): ?string
142
    {
143
        return $this->get('sku');
144
    }
145
146
    /**
147
     * Seta o valor da propriedade `sku`.
148
     *
149
     * @param string|null $sku
150
     * @return self
151
     */
152
    public function setSku(?string $sku = null): self
153
    {
154
        $this->set('sku', $sku);
155
        return $this;
156
    }
157
158
    /**
159
     * Retorna o valor da propriedade `description`.
160
     *
161
     * @return string|null
162
     */
163
    public function getDescription(): ?string
164
    {
165
        return $this->get('description');
166
    }
167
168
    /**
169
     * Seta o valor da propriedade `description`.
170
     *
171
     * @param string|null $description
172
     * @return self
173
     */
174
    public function setDescription(?string $description = null): self
175
    {
176
        $this->set('description', $description);
177
        return $this;
178
    }
179
180
}