From 43e19e3eef40c42c80c68784087b69e5822d8b2a Mon Sep 17 00:00:00 2001 From: Luan Schons Griebler <52085622+Luan1Schons@users.noreply.github.com> Date: Tue, 15 Oct 2024 17:51:30 -0300 Subject: [PATCH] Default currency option when calling $user->creditCurrency method. (#5) Default currency option in config --- README.md | 3 ++- config/balance.php | 3 ++- src/Traits/HasBalance.php | 14 ++++++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8fd49d6..a62c438 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,8 @@ return [ * Default table name. * If you have changed the migration, make sure you change this too. */ - 'table' => 'balances' + 'table' => 'balances', + 'default_currency' => 'EUR', // Default Currency ]; ``` diff --git a/config/balance.php b/config/balance.php index a06c470..809c8ae 100644 --- a/config/balance.php +++ b/config/balance.php @@ -5,5 +5,6 @@ * Default table name. * If you have changed the migration, make sure you change this too. */ - 'table' => 'balances' + 'table' => 'balances', + 'default_currency' => 'USD', // Default currency ]; diff --git a/src/Traits/HasBalance.php b/src/Traits/HasBalance.php index 7369e01..c4f64a1 100644 --- a/src/Traits/HasBalance.php +++ b/src/Traits/HasBalance.php @@ -9,7 +9,12 @@ trait HasBalance { - protected string $currency = 'USD'; + protected string $currency; + + public function __construct() + { + $this->currency = config('balance.default_currency', 'USD'); + } public function credits(): MorphMany { @@ -18,11 +23,12 @@ public function credits(): MorphMany public function withCurrency(string $currency): self { - $this->currency = $currency; + $clone = clone $this; + $clone->currency = $currency; - return $this; + return $clone; } - + protected function credit(): Attribute { return Attribute::make(