1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Models; |
4
|
|
|
|
5
|
|
|
use Bavix\Wallet\Interfaces\Wallet; |
|
|
|
|
6
|
|
|
use Bavix\Wallet\Models\Wallet as WalletModel; |
7
|
|
|
use Bavix\Wallet\Services\WalletService; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
11
|
|
|
use function config; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Transaction |
15
|
|
|
* @package Bavix\Wallet\Models |
16
|
|
|
* |
17
|
|
|
* @property string $payable_type |
18
|
|
|
* @property int $payable_id |
19
|
|
|
* @property string $uuid |
20
|
|
|
* @property string $type |
21
|
|
|
* @property int $amount |
22
|
|
|
* @property float $amountFloat |
23
|
|
|
* @property bool $confirmed |
24
|
|
|
* @property array $meta |
25
|
|
|
* @property Wallet $payable |
26
|
|
|
* @property WalletModel $wallet |
27
|
|
|
*/ |
28
|
|
|
class Transaction extends Model |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
public const TYPE_DEPOSIT = 'deposit'; |
32
|
|
|
public const TYPE_WITHDRAW = 'withdraw'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $fillable = [ |
38
|
|
|
'payable_type', |
39
|
|
|
'payable_id', |
40
|
|
|
'wallet_id', |
41
|
|
|
'uuid', |
42
|
|
|
'type', |
43
|
|
|
'amount', |
44
|
|
|
'confirmed', |
45
|
|
|
'meta', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $casts = [ |
52
|
|
|
'wallet_id' => 'int', |
53
|
|
|
'amount' => 'int', |
54
|
|
|
'confirmed' => 'bool', |
55
|
|
|
'meta' => 'json' |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
92 |
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
92 |
|
public function getCasts(): array |
62
|
92 |
|
{ |
63
|
92 |
|
$this->casts = array_merge( |
64
|
|
|
$this->casts, |
65
|
|
|
config('wallet.transaction.casts', []) |
66
|
92 |
|
); |
67
|
|
|
|
68
|
|
|
return parent::getCasts(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
132 |
|
* @return string |
73
|
|
|
*/ |
74
|
132 |
|
public function getTable(): string |
75
|
132 |
|
{ |
76
|
|
|
if (!$this->table) { |
77
|
|
|
$this->table = config('wallet.transaction.table', 'transactions'); |
78
|
132 |
|
} |
79
|
|
|
|
80
|
|
|
return parent::getTable(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
7 |
|
* @return MorphTo |
85
|
|
|
*/ |
86
|
7 |
|
public function payable(): MorphTo |
87
|
|
|
{ |
88
|
|
|
return $this->morphTo(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
27 |
|
* @return BelongsTo |
93
|
|
|
*/ |
94
|
27 |
|
public function wallet(): BelongsTo |
95
|
|
|
{ |
96
|
|
|
return $this->belongsTo(config('wallet.wallet.model', WalletModel::class)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return float |
101
|
|
|
*/ |
102
|
|
|
public function getAmountFloatAttribute(): float |
103
|
|
|
{ |
104
|
|
|
$decimalPlaces = app(WalletService::class) |
105
|
|
|
->decimalPlaces($this->wallet); |
106
|
|
|
|
107
|
|
|
return $this->amount / $decimalPlaces; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param float $amount |
112
|
|
|
* @return float |
113
|
|
|
*/ |
114
|
|
|
public function setAmountFloatAttribute(float $amount): void |
115
|
|
|
{ |
116
|
|
|
$decimalPlaces = app(WalletService::class) |
117
|
|
|
->decimalPlaces($this->wallet); |
118
|
|
|
|
119
|
|
|
$this->amount = $amount * $decimalPlaces; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: