Skip to content

Commit 0b7e9e8

Browse files
authored
Merge pull request #140 from bavix/develop
5.0.x - Arbitrary Precision Mathematics
2 parents 16f2ad8 + 29e53f2 commit 0b7e9e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1349
-360
lines changed

.phpstorm.meta.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPSTORM_META {
44

5+
use Bavix\Wallet\Interfaces\Mathable;
56
use Bavix\Wallet\Interfaces\Rateable;
67
use Bavix\Wallet\Interfaces\Storable;
78
use Bavix\Wallet\Models\Transaction;
@@ -13,7 +14,6 @@
1314
use Bavix\Wallet\Objects\Operation;
1415
use Bavix\Wallet\Services\CommonService;
1516
use Bavix\Wallet\Services\ExchangeService;
16-
use Bavix\Wallet\Services\ProxyService;
1717
use Bavix\Wallet\Services\WalletService;
1818

1919
override(\app(0), map([
@@ -23,11 +23,11 @@
2323
EmptyLock::class => EmptyLock::class,
2424
ExchangeService::class => ExchangeService::class,
2525
CommonService::class => CommonService::class,
26-
ProxyService::class => ProxyService::class,
2726
WalletService::class => WalletService::class,
2827
Wallet::class => Wallet::class,
2928
Transfer::class => Transfer::class,
3029
Transaction::class => Transaction::class,
30+
Mathable::class => Mathable::class,
3131
Rateable::class => Rateable::class,
3232
Storable::class => Storable::class,
3333
]));

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ laravel-wallet - Easy work with virtual wallet.
2929

3030
### Upgrade Guide
3131

32+
> Starting with version 5.x, support for Laravel 5 has been discontinued.
33+
> Update laravel or use version 4.x.
34+
3235
To perform the migration, you will be [helped by the instruction](https://bavix.github.io/laravel-wallet/#/upgrade-guide).
3336

3437
### Extensions
@@ -98,7 +101,7 @@ class Item extends Model implements Product
98101
return true;
99102
}
100103

101-
public function getAmountProduct(Customer $customer): int
104+
public function getAmountProduct(Customer $customer)
102105
{
103106
return 100;
104107
}

changelog.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
- add support "Arbitrary Precision Mathematics" (`ext-bcmath`) #139 #146
11+
- add `Mathable` service (helps switch quickly from bcmath to php computing)
12+
13+
### Changed
14+
- add unit cases
15+
- upgrade composer packages
16+
- Now all casts are in the config, not in the model. If you use bcmath, then all values are reduced to a string.
17+
18+
### Removed
19+
- Strong typing (models, interfaces, etc.)
20+
- all deprecated methods are removed
21+
- `nesbot/carbon` is no longer needed for the library to work
22+
923
## [4.2.0] - 2020-03-08
1024

1125
### Added
@@ -142,7 +156,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
142156
### Deprecated
143157
- class `Taxing`.
144158

145-
### Remove
159+
### Removed
146160
- The ability to change the ratio `coefficient`.
147161
- Removed private and protected methods, the traits turned out to be more clean.
148162

@@ -453,7 +467,7 @@ The operation is now executed in the transaction and updates the new `refund` fi
453467
- Exceptions: AmountInvalid, BalanceIsEmpty.
454468
- Models: Transfer, Transaction.
455469

456-
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/4.2.0...HEAD
470+
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/4.2.0...develop
457471
[4.2.0]: https://github.com/bavix/laravel-wallet/compare/4.1.2...4.2.0
458472
[4.1.2]: https://github.com/bavix/laravel-wallet/compare/4.1.1...4.1.2
459473
[4.1.1]: https://github.com/bavix/laravel-wallet/compare/4.1.0...4.1.1

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"php": "^7.2|^8.0",
2929
"ext-pdo": "*",
3030
"illuminate/database": "^6.0|^7.0",
31-
"nesbot/carbon": "^2.0",
3231
"doctrine/dbal": "^2.8",
3332
"ramsey/uuid": "^3.0"
3433
},
@@ -40,6 +39,7 @@
4039
"laravel/cashier": "^10.0"
4140
},
4241
"suggest": {
42+
"ext-bcmath": "Used for accurate calculations of large numbers",
4343
"bavix/laravel-wallet-swap": "Addition to the laravel-wallet library for quick setting of exchange rates",
4444
"bavix/laravel-wallet-vacuum": "Addition to the laravel-wallet library for quick fix race condition"
4545
},
@@ -54,6 +54,9 @@
5454
}
5555
},
5656
"extra": {
57+
"branch-alias": {
58+
"dev-develop": "5.0.x-dev"
59+
},
5760
"laravel": {
5861
"providers": [
5962
"Bavix\\Wallet\\WalletServiceProvider"

config/config.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,37 @@
66
use Bavix\Wallet\Objects\Operation;
77
use Bavix\Wallet\Services\ExchangeService;
88
use Bavix\Wallet\Services\CommonService;
9-
use Bavix\Wallet\Services\ProxyService;
109
use Bavix\Wallet\Services\WalletService;
1110
use Bavix\Wallet\Services\LockService;
1211
use Bavix\Wallet\Models\Transaction;
1312
use Bavix\Wallet\Models\Transfer;
1413
use Bavix\Wallet\Models\Wallet;
1514
use Bavix\Wallet\Simple\Rate;
1615
use Bavix\Wallet\Simple\Store;
16+
use Bavix\Wallet\Simple\BCMath;
17+
use Bavix\Wallet\Simple\Math;
18+
19+
$bcLoaded = extension_loaded('bcmath');
1720

1821
return [
22+
/**
23+
* This parameter is necessary for more accurate calculations.
24+
* PS, Arbitrary Precision Calculations
25+
*/
26+
'math' => [
27+
'scale' => 64,
28+
],
29+
1930
/**
2031
* The parameter is used for fast packet overload.
2132
* You do not need to search for the desired class by code, the library will do it itself.
2233
*/
2334
'package' => [
2435
'rateable' => Rate::class,
2536
'storable' => Store::class,
37+
'mathable' => $bcLoaded ?
38+
BCMath::class :
39+
Math::class,
2640
],
2741

2842
/**
@@ -60,7 +74,6 @@
6074
'services' => [
6175
'exchange' => ExchangeService::class,
6276
'common' => CommonService::class,
63-
'proxy' => ProxyService::class,
6477
'wallet' => WalletService::class,
6578
'lock' => LockService::class,
6679
],
@@ -78,7 +91,9 @@
7891
'transaction' => [
7992
'table' => 'transactions',
8093
'model' => Transaction::class,
81-
'casts' => [],
94+
'casts' => [
95+
'amount' => $bcLoaded ? 'string' : 'int',
96+
],
8297
],
8398

8499
/**
@@ -87,7 +102,9 @@
87102
'transfer' => [
88103
'table' => 'transfers',
89104
'model' => Transfer::class,
90-
'casts' => [],
105+
'casts' => [
106+
'fee' => $bcLoaded ? 'string' : 'int',
107+
],
91108
],
92109

93110
/**
@@ -96,7 +113,9 @@
96113
'wallet' => [
97114
'table' => 'wallets',
98115
'model' => Wallet::class,
99-
'casts' => [],
116+
'casts' => [
117+
'balance' => $bcLoaded ? 'string' : 'int',
118+
],
100119
'default' => [
101120
'name' => 'Default Wallet',
102121
'slug' => 'default',

database/migrations_v1/2018_11_06_222923_create_transactions_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CreateTransactionsTable extends Migration
1818
public function up(): void
1919
{
2020
Schema::create($this->table(), function (Blueprint $table) {
21-
$table->increments('id');
21+
$table->bigIncrements('id');
2222
$table->morphs('payable');
2323
$table->enum('type', ['deposit', 'withdraw'])->index();
2424
$table->bigInteger('amount');

database/migrations_v1/2018_11_07_192923_create_transfers_table.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class CreateTransfersTable extends Migration
1515
public function up(): void
1616
{
1717
Schema::create($this->table(), function (Blueprint $table) {
18-
$table->increments('id');
18+
$table->bigIncrements('id');
1919
$table->morphs('from');
2020
$table->morphs('to');
21-
$table->unsignedInteger('deposit_id');
22-
$table->unsignedInteger('withdraw_id');
21+
$table->unsignedBigInteger('deposit_id');
22+
$table->unsignedBigInteger('withdraw_id');
2323
$table->uuid('uuid')->unique();
2424
$table->timestamps();
2525

database/migrations_v2/2018_11_15_124230_create_wallets_table.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use Bavix\Wallet\Models\Transaction;
44
use Bavix\Wallet\Models\Wallet;
5-
use Carbon\Carbon;
65
use Illuminate\Database\Eloquent\Collection;
76
use Illuminate\Database\Migrations\Migration;
87
use Illuminate\Database\Schema\Blueprint;
@@ -26,7 +25,7 @@ protected function table(): string
2625
public function up(): void
2726
{
2827
Schema::create($this->table(), function (Blueprint $table) {
29-
$table->increments('id');
28+
$table->bigIncrements('id');
3029
$table->morphs('holder');
3130
$table->string('name');
3231
$table->string('slug')->index();
@@ -48,8 +47,8 @@ public function up(): void
4847
->selectRaw('? as name', [$default])
4948
->selectRaw('? as slug', [$slug])
5049
->selectRaw('sum(amount) as balance')
51-
->selectRaw('? as created_at', [Carbon::now()])
52-
->selectRaw('? as updated_at', [Carbon::now()])
50+
->selectRaw('? as created_at', [DB::raw('now()')])
51+
->selectRaw('? as updated_at', [DB::raw('now()')])
5352
->groupBy('holder_type', 'holder_id')
5453
->orderBy('holder_type');
5554

database/migrations_v2/2018_11_19_164609_update_transactions_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected function walletTable(): string
3333
public function up(): void
3434
{
3535
Schema::table($this->table(), function (Blueprint $table) {
36-
$table->unsignedInteger('wallet_id')
36+
$table->unsignedBigInteger('wallet_id')
3737
->nullable()
3838
->after('payable_id');
3939

docs/basic-usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Item extends Model implements Product
6262
return true;
6363
}
6464

65-
public function getAmountProduct(Customer $customer): int
65+
public function getAmountProduct(Customer $customer)
6666
{
6767
return 100;
6868
}

0 commit comments

Comments
 (0)