| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bavix\Wallet\Models; |
||
| 4 | |||
| 5 | use function array_merge; |
||
| 6 | use Bavix\Wallet\Interfaces\Mathable; |
||
| 7 | use Bavix\Wallet\Interfaces\Wallet; |
||
|
0 ignored issues
–
show
|
|||
| 8 | use Bavix\Wallet\Models\Wallet as WalletModel; |
||
| 9 | use Bavix\Wallet\Services\WalletService; |
||
| 10 | use function config; |
||
| 11 | use Illuminate\Database\Eloquent\Model; |
||
| 12 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
| 13 | use Illuminate\Database\Eloquent\Relations\MorphTo; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Class Transaction. |
||
| 17 | * |
||
| 18 | * @property string $payable_type |
||
| 19 | * @property int $payable_id |
||
| 20 | * @property int $wallet_id |
||
| 21 | * @property string $uuid |
||
| 22 | * @property string $type |
||
| 23 | * @property int $amount |
||
| 24 | * @property float $amountFloat |
||
| 25 | * @property bool $confirmed |
||
| 26 | * @property array $meta |
||
| 27 | * @property Wallet $payable |
||
| 28 | * @property WalletModel $wallet |
||
| 29 | */ |
||
| 30 | class Transaction extends Model |
||
| 31 | { |
||
| 32 | public const TYPE_DEPOSIT = 'deposit'; |
||
| 33 | public const TYPE_WITHDRAW = 'withdraw'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $fillable = [ |
||
| 39 | 'payable_type', |
||
| 40 | 'payable_id', |
||
| 41 | 'wallet_id', |
||
| 42 | 'uuid', |
||
| 43 | 'type', |
||
| 44 | 'amount', |
||
| 45 | 'confirmed', |
||
| 46 | 'meta', |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $casts = [ |
||
| 53 | 'wallet_id' => 'int', |
||
| 54 | 'confirmed' => 'bool', |
||
| 55 | 'meta' => 'json', |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritdoc} |
||
| 60 | */ |
||
| 61 | 110 | public function getCasts(): array |
|
| 62 | { |
||
| 63 | 110 | return array_merge( |
|
| 64 | 110 | parent::getCasts(), |
|
| 65 | 110 | config('wallet.transaction.casts', []) |
|
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | 180 | public function getTable(): string |
|
| 73 | { |
||
| 74 | 180 | if (! $this->table) { |
|
| 75 | 180 | $this->table = config('wallet.transaction.table', 'transactions'); |
|
| 76 | } |
||
| 77 | |||
| 78 | 180 | return parent::getTable(); |
|
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return MorphTo |
||
| 83 | */ |
||
| 84 | 8 | public function payable(): MorphTo |
|
| 85 | { |
||
| 86 | 8 | return $this->morphTo(); |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return BelongsTo |
||
| 91 | */ |
||
| 92 | 31 | public function wallet(): BelongsTo |
|
| 93 | { |
||
| 94 | 31 | return $this->belongsTo(config('wallet.wallet.model', WalletModel::class)); |
|
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return int|float |
||
| 99 | */ |
||
| 100 | 2 | public function getAmountFloatAttribute() |
|
| 101 | { |
||
| 102 | 2 | $decimalPlaces = app(WalletService::class) |
|
| 103 | 2 | ->decimalPlaces($this->wallet); |
|
| 104 | |||
| 105 | 2 | return app(Mathable::class) |
|
| 106 | 2 | ->div($this->amount, $decimalPlaces); |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param int|float $amount |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | 1 | public function setAmountFloatAttribute($amount): void |
|
| 114 | { |
||
| 115 | 1 | $math = app(Mathable::class); |
|
| 116 | 1 | $decimalPlaces = app(WalletService::class) |
|
| 117 | 1 | ->decimalPlaces($this->wallet); |
|
| 118 | |||
| 119 | 1 | $this->amount = $math->round($math->mul($amount, $decimalPlaces)); |
|
|
0 ignored issues
–
show
The property
$amount was declared of type integer, but $math->round($math->mul($amount, $decimalPlaces)) is of type string. Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
| 120 | 1 | } |
|
| 121 | } |
||
| 122 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: