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