1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Traits; |
4
|
|
|
|
5
|
|
|
use Bavix\Wallet\Models\Wallet as WalletModel; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use function array_key_exists; |
9
|
|
|
use function config; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait HasWallets |
13
|
|
|
* To use a trait, you must add HasWallet trait. |
14
|
|
|
* |
15
|
|
|
* @package Bavix\Wallet\Traits |
16
|
|
|
* |
17
|
|
|
* @property-read Collection|WalletModel[] $wallets |
18
|
|
|
*/ |
19
|
|
|
trait HasWallets |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The variable is used for the cache, so as not to request wallets many times. |
24
|
|
|
* WalletProxy keeps the money wallets in the memory to avoid errors when you |
25
|
|
|
* purchase/transfer, etc. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $_wallets = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $_loadedWallets; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get wallet by slug |
38
|
|
|
* |
39
|
|
|
* $user->wallet->balance // 200 |
40
|
|
|
* or short recording $user->balance; // 200 |
41
|
|
|
* |
42
|
|
|
* $defaultSlug = config('wallet.wallet.default.slug'); |
43
|
|
|
* $user->getWallet($defaultSlug)->balance; // 200 |
44
|
|
|
* |
45
|
|
|
* $user->getWallet('usd')->balance; // 50 |
46
|
|
|
* $user->getWallet('rub')->balance; // 100 |
47
|
|
|
* |
48
|
|
|
* @param string $slug |
49
|
|
|
* @return WalletModel|null |
50
|
|
|
*/ |
51
|
|
|
public function getWallet(string $slug): ?WalletModel |
52
|
|
|
{ |
53
|
|
|
if (!$this->_loadedWallets && $this->relationLoaded('wallets')) { |
|
|
|
|
54
|
|
|
$this->_loadedWallets = true; |
55
|
|
|
$wallets = $this->getRelation('wallets'); |
|
|
|
|
56
|
|
|
foreach ($wallets as $wallet) { |
57
|
|
|
$this->_wallets[$wallet->slug] = $wallet; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!array_key_exists($slug, $this->_wallets)) { |
62
|
|
|
$this->_wallets[$slug] = $this->wallets() |
63
|
|
|
->where('slug', $slug) |
64
|
|
|
->first(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->_wallets[$slug]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* method of obtaining all wallets |
72
|
|
|
* |
73
|
|
|
* @return MorphMany |
74
|
|
|
*/ |
75
|
|
|
public function wallets(): MorphMany |
76
|
|
|
{ |
77
|
|
|
return $this->morphMany(config('wallet.wallet.model'), 'holder'); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $data |
82
|
|
|
* @return WalletModel |
83
|
|
|
*/ |
84
|
|
|
public function createWallet(array $data): WalletModel |
85
|
|
|
{ |
86
|
|
|
/** |
87
|
|
|
* Create a default wallet |
88
|
|
|
*/ |
89
|
|
|
$this->getBalanceAttribute(); |
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var WalletModel $wallet |
93
|
|
|
*/ |
94
|
|
|
$wallet = $this->wallets()->create($data); |
95
|
|
|
$this->_wallets[$wallet->slug] = $wallet; |
96
|
|
|
|
97
|
|
|
return $wallet; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|