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
|
|
|
* Transaction constructor. |
39
|
|
|
* @throws |
40
|
|
|
*/ |
41
|
30 |
|
public function __construct() |
42
|
|
|
{ |
43
|
30 |
|
$this->uuid = Uuid::uuid4()->toString(); |
44
|
30 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
30 |
|
public function getType(): string |
50
|
|
|
{ |
51
|
30 |
|
return $this->type; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
30 |
|
public function getUuid(): string |
58
|
|
|
{ |
59
|
30 |
|
return $this->uuid; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
30 |
|
public function getAmount(): int |
66
|
|
|
{ |
67
|
30 |
|
return $this->amount; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array|null |
72
|
|
|
*/ |
73
|
30 |
|
public function getMeta(): ?array |
74
|
|
|
{ |
75
|
30 |
|
return $this->meta; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
30 |
|
public function isConfirmed(): bool |
82
|
|
|
{ |
83
|
30 |
|
return $this->confirmed; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $type |
88
|
|
|
* @return static |
89
|
|
|
*/ |
90
|
30 |
|
public function setType(string $type): self |
91
|
|
|
{ |
92
|
30 |
|
$this->type = $type; |
93
|
30 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param int $amount |
98
|
|
|
* @return static |
99
|
|
|
*/ |
100
|
30 |
|
public function setAmount(int $amount): self |
101
|
|
|
{ |
102
|
30 |
|
$this->amount = $amount; |
103
|
30 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array|null $meta |
108
|
|
|
* @return static |
109
|
|
|
*/ |
110
|
30 |
|
public function setMeta(?array $meta): self |
111
|
|
|
{ |
112
|
30 |
|
$this->meta = $meta; |
113
|
30 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param bool $confirmed |
118
|
|
|
* @return static |
119
|
|
|
*/ |
120
|
30 |
|
public function setConfirmed(bool $confirmed): self |
121
|
|
|
{ |
122
|
30 |
|
$this->confirmed = $confirmed; |
123
|
30 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param Wallet $wallet |
128
|
|
|
* @return Transaction |
129
|
|
|
*/ |
130
|
30 |
|
public function create(Wallet $wallet): Transaction |
131
|
|
|
{ |
132
|
|
|
/** |
133
|
|
|
* @var Transaction $model |
134
|
|
|
*/ |
135
|
30 |
|
$model = $wallet->transactions()->create([ |
136
|
30 |
|
'type' => $this->getType(), |
137
|
30 |
|
'wallet_id' => $wallet->getKey(), |
|
|
|
|
138
|
30 |
|
'uuid' => $this->getUuid(), |
139
|
30 |
|
'confirmed' => $this->isConfirmed(), |
140
|
30 |
|
'amount' => $this->getAmount(), |
141
|
30 |
|
'meta' => $this->getMeta(), |
142
|
|
|
]); |
143
|
|
|
|
144
|
30 |
|
return $model; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|