-
Notifications
You must be signed in to change notification settings - Fork 17
/
CartItem.php
180 lines (150 loc) · 4.21 KB
/
CartItem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace ValdeirPsr\PagSeguro\Domains;
use DOMDocument;
use ValdeirPsr\PagSeguro\Interfaces\Serializer\Xml;
use ValdeirPsr\PagSeguro\Interfaces\Serializer\IArray;
use ValdeirPsr\PagSeguro\Parser\Xml as XmlParser;
use ValdeirPsr\PagSeguro\Validation\Validator as v;
class CartItem implements Xml, IArray
{
/** @var string Identificador do produto. Deve ser único */
private $id;
/** @var string */
private $description;
/** @var float */
private $amount;
/** @var int */
private $quantity;
/**
* Identifica o item. Você pode escolher códigos que tenham significado para
* seu sistema e informá-los nestes parâmetros.
*
* @param string $value
*
* @return self
*/
public function setId(string $value): self
{
$this->id = $value;
return $this;
}
/**
* @return string Retorna o identificador do item
*/
public function getId(): string
{
return $this->id;
}
/**
* Descreve o item. A descrição é o texto que o PagSeguro mostra associado a
* cada item quando o comprador está finalizando o pagamento, portanto é
* importante que ela seja clara e explicativa.
*
* @param string $value
*
* @return self
*/
public function setDescription(string $value): self
{
$this->description = $value;
return $this;
}
/**
* @return string Retorna a descrição do item
*/
public function getDescription(): string
{
return $this->description;
}
/**
* Representa o preço unitário do item. Este método define o valor de uma
* unidade do item, que será multiplicado pela quantidade para obter o valor
* total dentro do pagamento.
*
* @param float $value
*
* @throws \InvalidArgumentException Caso o valor possua mais de duas casas decimais
*
* @return self
*/
public function setAmount(float $value): self
{
if (!v::Money(2)->validate($value)) {
throw new \InvalidArgumentException('Amount invalid. The value must have two decimal places. Was: ' . $value);
}
$this->amount = number_format($value, 2, '.', '');
return $this;
}
/**
* @return float Retorna o preço unitário do item
*/
public function getAmount(): string
{
return $this->amount;
}
/**
* Representa a quantidade comprada de determinado item. Esta função define
* a quantidade de um item, que será multiplicado pelo valor unitário para
* obter o valor total dentro do pagamento.
*
* @param int $value
*
* @return self
*/
public function setQuantity(int $value): self
{
$this->quantity = $value;
return $this;
}
/**
* @return int Retorna a quantidade comprada
*/
public function getQuantity(): int
{
return $this->quantity;
}
/**
* {@inheritDoc}
*/
public static function fromXml(string $value)
{
$dom = new DOMDocument();
$dom->loadXML($value);
$instance = new self();
$id = $dom->getElementsByTagName('id');
if ($id->count() > 0) {
$instance->id = $id->item(0)->textContent;
}
$description = $dom->getElementsByTagName('description');
if ($description->count() > 0) {
$instance->description = $description->item(0)->textContent;
}
$quantity = $dom->getElementsByTagName('quantity');
if ($quantity->count() > 0) {
$instance->quantity = $quantity->item(0)->textContent;
}
$amount = $dom->getElementsByTagName('amount');
if ($amount->count() > 0) {
$instance->amount = $amount->item(0)->textContent;
}
return $instance;
}
/**
* {@inheritDoc}
*/
public function toXml(): string
{
$parser = new XmlParser();
$result = $parser->parser([
'item' => $this->toArray()
]);
return $result->saveXML();
}
/**
* {@inheritDoc}
*/
public function toArray(): array
{
return array_filter(get_object_vars($this));
}
}