1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Objects; |
4
|
|
|
|
5
|
|
|
use Bavix\Wallet\Interfaces\Wallet; |
6
|
|
|
use Bavix\Wallet\Models\Transaction; |
7
|
|
|
use Ramsey\Uuid\Uuid; |
8
|
|
|
|
9
|
|
|
class Operation |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $type; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $uuid; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
protected $amount; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var null|array |
29
|
|
|
*/ |
30
|
|
|
protected $meta; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $confirmed; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Wallet |
39
|
|
|
*/ |
40
|
|
|
protected $wallet; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Transaction constructor. |
44
|
|
|
* @throws |
45
|
|
|
*/ |
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
$this->uuid = Uuid::uuid4()->toString(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getType(): string |
55
|
|
|
{ |
56
|
|
|
return $this->type; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getUuid(): string |
63
|
|
|
{ |
64
|
|
|
return $this->uuid; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
|
|
public function getAmount(): int |
71
|
|
|
{ |
72
|
|
|
return $this->amount; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array|null |
77
|
|
|
*/ |
78
|
|
|
public function getMeta(): ?array |
79
|
|
|
{ |
80
|
|
|
return $this->meta; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function isConfirmed(): bool |
87
|
|
|
{ |
88
|
|
|
return $this->confirmed; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $type |
93
|
|
|
* @return static |
94
|
|
|
*/ |
95
|
|
|
public function setType(string $type): self |
96
|
|
|
{ |
97
|
|
|
$this->type = $type; |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param int $amount |
103
|
|
|
* @return static |
104
|
|
|
*/ |
105
|
|
|
public function setAmount(int $amount): self |
106
|
|
|
{ |
107
|
|
|
$this->amount = $amount; |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array|null $meta |
113
|
|
|
* @return static |
114
|
|
|
*/ |
115
|
|
|
public function setMeta(?array $meta): self |
116
|
|
|
{ |
117
|
|
|
$this->meta = $meta; |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param bool $confirmed |
123
|
|
|
* @return static |
124
|
|
|
*/ |
125
|
|
|
public function setConfirmed(bool $confirmed): self |
126
|
|
|
{ |
127
|
|
|
$this->confirmed = $confirmed; |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return Wallet |
133
|
|
|
*/ |
134
|
|
|
public function getWallet(): Wallet |
135
|
|
|
{ |
136
|
|
|
return $this->wallet; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param Wallet $wallet |
141
|
|
|
* @return static |
142
|
|
|
*/ |
143
|
|
|
public function setWallet(Wallet $wallet): self |
144
|
|
|
{ |
145
|
|
|
$this->wallet = $wallet; |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return Transaction |
151
|
|
|
*/ |
152
|
|
|
public function create(): Transaction |
153
|
|
|
{ |
154
|
|
|
/** |
155
|
|
|
* @var Transaction $model |
156
|
|
|
*/ |
157
|
|
|
$model = $this->getWallet() |
158
|
|
|
->transactions() |
159
|
|
|
->create($this->toArray()); |
160
|
|
|
|
161
|
|
|
return $model; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return array |
166
|
|
|
* @throws |
167
|
|
|
*/ |
168
|
|
|
public function toArray(): array |
169
|
|
|
{ |
170
|
|
|
return [ |
171
|
|
|
'type' => $this->getType(), |
172
|
|
|
'wallet_id' => $this->getWallet()->getKey(), |
|
|
|
|
173
|
|
|
'uuid' => $this->getUuid(), |
174
|
|
|
'confirmed' => $this->isConfirmed(), |
175
|
|
|
'amount' => $this->getAmount(), |
176
|
|
|
'meta' => $this->getMeta(), |
177
|
|
|
]; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|