Skip to content

Commit ac50440

Browse files
committed
shared multi wallet with wallet
1 parent f212db4 commit ac50440

File tree

4 files changed

+73
-47
lines changed

4 files changed

+73
-47
lines changed

src/Traits/CanBePaidFloat.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ trait CanBePaidFloat
1818
CanBePaid::change insteadof HasWalletFloat;
1919
CanBePaid::transactions insteadof HasWalletFloat;
2020
CanBePaid::transfers insteadof HasWalletFloat;
21-
CanBePaid::wallets insteadof HasWalletFloat;
22-
CanBePaid::getWallet insteadof HasWalletFloat;
2321
CanBePaid::wallet insteadof HasWalletFloat;
2422
CanBePaid::getBalanceAttribute insteadof HasWalletFloat;
2523
CanBePaid::addBalance insteadof HasWalletFloat;

src/Traits/HasWallet.php

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,6 @@
3030
trait HasWallet
3131
{
3232

33-
/**
34-
* The variable is used for the cache, so as not to request wallets many times.
35-
* WalletProxy keeps the money wallets in the memory to avoid errors when you
36-
* purchase/transfer, etc.
37-
*
38-
* @var array
39-
*/
40-
private $_wallets = [];
41-
4233
/**
4334
* The amount of checks for errors
4435
*
@@ -255,42 +246,6 @@ public function transfers(): MorphMany
255246
return $this->morphMany(config('wallet.transfer.model'), 'from');
256247
}
257248

258-
/**
259-
* method of obtaining all wallets
260-
*
261-
* @return MorphMany
262-
*/
263-
public function wallets(): MorphMany
264-
{
265-
return $this->morphMany(config('wallet.wallet.model'), 'holder');
266-
}
267-
268-
/**
269-
* Get wallet by slug
270-
*
271-
* $user->wallet->balance // 200
272-
* or short recording $user->balance; // 200
273-
*
274-
* $defaultSlug = config('wallet.wallet.default.slug');
275-
* $user->getWallet($defaultSlug)->balance; // 200
276-
*
277-
* $user->getWallet('usd')->balance; // 50
278-
* $user->getWallet('rub')->balance; // 100
279-
*
280-
* @param string $slug
281-
* @return WalletModel|null
282-
*/
283-
public function getWallet(string $slug): ?WalletModel
284-
{
285-
if (!\array_key_exists($slug, $this->_wallets)) {
286-
$this->_wallets[$slug] = $this->wallets()
287-
->where('slug', $slug)
288-
->first();
289-
}
290-
291-
return $this->_wallets[$slug];
292-
}
293-
294249
/**
295250
* Get default Wallet
296251
* this method is used for Eager Loading

src/Traits/HasWallets.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
9+
/**
10+
* Trait HasWallets
11+
* To use a trait, you must add HasWallet trait.
12+
*
13+
* @package Bavix\Wallet\Traits
14+
*
15+
* @property-read Collection|WalletModel[] $wallets
16+
*/
17+
trait HasWallets
18+
{
19+
20+
/**
21+
* The variable is used for the cache, so as not to request wallets many times.
22+
* WalletProxy keeps the money wallets in the memory to avoid errors when you
23+
* purchase/transfer, etc.
24+
*
25+
* @var array
26+
*/
27+
private $_wallets = [];
28+
29+
/**
30+
* method of obtaining all wallets
31+
*
32+
* @return MorphMany
33+
*/
34+
public function wallets(): MorphMany
35+
{
36+
return $this->morphMany(config('wallet.wallet.model'), 'holder');
37+
}
38+
39+
/**
40+
* Get wallet by slug
41+
*
42+
* $user->wallet->balance // 200
43+
* or short recording $user->balance; // 200
44+
*
45+
* $defaultSlug = config('wallet.wallet.default.slug');
46+
* $user->getWallet($defaultSlug)->balance; // 200
47+
*
48+
* $user->getWallet('usd')->balance; // 50
49+
* $user->getWallet('rub')->balance; // 100
50+
*
51+
* @param string $slug
52+
* @return WalletModel|null
53+
*/
54+
public function getWallet(string $slug): ?WalletModel
55+
{
56+
if (!\array_key_exists($slug, $this->_wallets)) {
57+
$this->_wallets[$slug] = $this->wallets()
58+
->where('slug', $slug)
59+
->first();
60+
}
61+
62+
return $this->_wallets[$slug];
63+
}
64+
65+
}

tests/MultiWalletTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Test;
4+
5+
class MultiWalletTest extends TestCase
6+
{
7+
8+
}

0 commit comments

Comments
 (0)