1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Objects; |
4
|
|
|
|
5
|
|
|
use Bavix\Wallet\Interfaces\Mathable; |
6
|
|
|
use Bavix\Wallet\Interfaces\Wallet; |
7
|
|
|
use Bavix\Wallet\Models\Transaction; |
8
|
|
|
use Ramsey\Uuid\Uuid; |
9
|
|
|
|
10
|
|
|
class Operation |
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
|
111 |
|
public function __construct() |
47
|
|
|
{ |
48
|
111 |
|
$this->uuid = Uuid::uuid4()->toString(); |
49
|
111 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
110 |
|
public function getType(): string |
55
|
|
|
{ |
56
|
110 |
|
return $this->type; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
110 |
|
public function getUuid(): string |
63
|
|
|
{ |
64
|
110 |
|
return $this->uuid; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return float|int |
69
|
|
|
*/ |
70
|
110 |
|
public function getAmount() |
71
|
|
|
{ |
72
|
110 |
|
return $this->amount; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array|null |
77
|
|
|
*/ |
78
|
110 |
|
public function getMeta(): ?array |
79
|
|
|
{ |
80
|
110 |
|
return $this->meta; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
110 |
|
public function isConfirmed(): bool |
87
|
|
|
{ |
88
|
110 |
|
return $this->confirmed; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $type |
93
|
|
|
* @return static |
94
|
|
|
*/ |
95
|
110 |
|
public function setType(string $type): self |
96
|
|
|
{ |
97
|
110 |
|
$this->type = $type; |
98
|
|
|
|
99
|
110 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param int $amount |
104
|
|
|
* @return static |
105
|
|
|
*/ |
106
|
110 |
|
public function setAmount($amount): self |
107
|
|
|
{ |
108
|
110 |
|
$this->amount = app(Mathable::class)->round($amount); |
|
|
|
|
109
|
|
|
|
110
|
110 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param array|null $meta |
115
|
|
|
* @return static |
116
|
|
|
*/ |
117
|
110 |
|
public function setMeta(?array $meta): self |
118
|
|
|
{ |
119
|
110 |
|
$this->meta = $meta; |
120
|
|
|
|
121
|
110 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param bool $confirmed |
126
|
|
|
* @return static |
127
|
|
|
*/ |
128
|
110 |
|
public function setConfirmed(bool $confirmed): self |
129
|
|
|
{ |
130
|
110 |
|
$this->confirmed = $confirmed; |
131
|
|
|
|
132
|
110 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return Wallet |
137
|
|
|
*/ |
138
|
110 |
|
public function getWallet(): Wallet |
139
|
|
|
{ |
140
|
110 |
|
return $this->wallet; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Wallet $wallet |
145
|
|
|
* @return static |
146
|
|
|
*/ |
147
|
110 |
|
public function setWallet(Wallet $wallet): self |
148
|
|
|
{ |
149
|
110 |
|
$this->wallet = $wallet; |
150
|
|
|
|
151
|
110 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return Transaction |
156
|
|
|
*/ |
157
|
110 |
|
public function create(): Transaction |
158
|
|
|
{ |
159
|
|
|
/** |
160
|
|
|
* @var Transaction $model |
161
|
|
|
*/ |
162
|
110 |
|
$model = $this->getWallet() |
163
|
110 |
|
->transactions() |
164
|
110 |
|
->create($this->toArray()); |
165
|
|
|
|
166
|
110 |
|
return $model; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return array |
171
|
|
|
* @throws |
172
|
|
|
*/ |
173
|
110 |
|
public function toArray(): array |
174
|
|
|
{ |
175
|
|
|
return [ |
176
|
110 |
|
'type' => $this->getType(), |
177
|
110 |
|
'wallet_id' => $this->getWallet()->getKey(), |
|
|
|
|
178
|
110 |
|
'uuid' => $this->getUuid(), |
179
|
110 |
|
'confirmed' => $this->isConfirmed(), |
180
|
110 |
|
'amount' => $this->getAmount(), |
181
|
110 |
|
'meta' => $this->getMeta(), |
182
|
|
|
]; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.