From b57b9837a0baf8314c128ac4dea5e012731bac31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 16 Nov 2021 21:34:22 +0100 Subject: [PATCH 1/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 48c47b1..b0cbf82 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Main features: - Simple API - Livewire integration - Custom currency support -- Highly customizable currencies +- Highly customizable formatting - Rounding logic for compliant accounting This package is our implementation of the [Money pattern](https://martinfowler.com/eaaCatalog/money.html). From 0a1552b1f0f3b34ce38477764deea3e9474f84f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 16 Nov 2021 21:57:09 +0100 Subject: [PATCH 2/5] Update README.md --- README.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b0cbf82..90d28e8 100644 --- a/README.md +++ b/README.md @@ -498,9 +498,9 @@ $money->is($bar); // true ## Tips -### 💡 Accepted currency formats +### 💡 Accepted currency code formats -Most methods which accept a currency accept it in any format: +Most methods which accept a currency accept it in any of these formats: ```php currency(USD::class); currency(new USD); @@ -511,6 +511,20 @@ money(1000, 'USD')->convertTo(new CZK); money(1000, new USD)->convertTo(CZK::class); ``` +### 💡 Dynamically add currencies + +Class currencies are elegant, but not necessary. If your currency specs come from the database, or some API, you can register them as arrays. + +```php +// LoadCurrencies middleware + +currencies()->add(cache()->remember('currencies', 3600, function () { + return UserCurrencies::where('user_id', auth()->id())->get()->toArray(); +}); +``` + +Where the DB call returns an array of array currencies following the [format mentioned above](#creating-a-currency). + ## Development & contributing Run all checks locally: From ab97f27fd10e8a2af4955948d54461336fb53b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 17 Nov 2021 10:37:58 +0100 Subject: [PATCH 3/5] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 90d28e8..d45daaa 100644 --- a/README.md +++ b/README.md @@ -471,9 +471,9 @@ The component can look roughly like this: }, init() { - $watch('money', () => $wire.set('money', { - value: Math.round(this.value / 100), - currency: this.currency. + $watch('money', money => $wire.set('money', { + value: Math.round(money.value / 100), + currency: money.currency. })) }, }" x-init="init"> From 92838957696f1ef7dd0c7a2ce15d4325185148fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 17 Nov 2021 20:53:14 +0100 Subject: [PATCH 4/5] Fix syntax highlighting --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d45daaa..c32dae6 100644 --- a/README.md +++ b/README.md @@ -337,7 +337,7 @@ For example, say we want to use the `currency` session key to keep track of the ```php currencies() ->storeCurrentUsing(fn (string $code) => session()->put('currency', $code)) - ->resolveCurrentUsing(fn () => session()->get('currency)); + ->resolveCurrentUsing(fn () => session()->get('currency')); ``` You can add this code to your AppServiceProvider's `boot()` method. From 83f86e851f3fce3f077c57c491e0e7f93fc55e2f Mon Sep 17 00:00:00 2001 From: lukinovec Date: Thu, 18 Nov 2021 10:27:21 +0100 Subject: [PATCH 5/5] Fix typo (fromDecimals -> fromDecimal) --- README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c32dae6..7a76b0c 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,9 @@ $money = money(2000, new USD); // $20.00 $money = money(3000, CZK::class); // 20 Kč // Using decimals -$money = Money::fromDecimals(15.00, 'EUR'); // 15.00 € -$money = Money::fromDecimals(20.00, new USD); // $20.00 -$money = Money::fromDecimals(30.00, CZK::class); // 20 Kč +$money = Money::fromDecimal(15.00, 'EUR'); // 15.00 € +$money = Money::fromDecimal(20.00, new USD); // $20.00 +$money = Money::fromDecimal(30.00, CZK::class); // 20 Kč ``` ### Arithmetics @@ -136,7 +136,7 @@ $money->value(); // 1200 ### Accessing the decimal value ```php -$money = Money::fromDecimals(100.0, new USD); +$money = Money::fromDecimal(100.0, new USD); $money->value(); // 10000 $money->decimals(); // 100.0 ``` @@ -146,13 +146,13 @@ $money->decimals(); // 100.0 You can format money using the `->formatted()` method: ```php -$money = Money::fromDecimals(40.25, USD::class); +$money = Money::fromDecimal(40.25, USD::class); $money->formatted(); // $40.25 ``` The method optionally accepts overrides for the [currency specification](#currency-logic): ```php -$money = Money::fromDecimals(40.25, USD::class); +$money = Money::fromDecimal(40.25, USD::class); // $ 40.25 USD $money->formatted(decimalSeparator: ',', prefix: '$ ', suffix: ' USD'); @@ -160,7 +160,7 @@ $money->formatted(decimalSeparator: ',', prefix: '$ ', suffix: ' USD'); The overrides can also be passed as an array: ```php -$money = Money::fromDecimals(40.25, USD::class); +$money = Money::fromDecimal(40.25, USD::class); // $ 40.25 USD $money->formatted(['decimalSeparator' => ',', 'prefix' => '$ ', 'suffix' => ' USD']); @@ -171,7 +171,7 @@ $money->formatted(['decimalSeparator' => ',', 'prefix' => '$ ', 'suffix' => ' US Some currencies, such as the Czech Crown (CZK), generally display final prices in full crowns, but use cents for the intermediate math operations. For example: ```php -$money = Money::fromDecimals(3.30, CZK::class); +$money = Money::fromDecimal(3.30, CZK::class); $money->value(); // 330 $money->formatted(); // 3 Kč @@ -188,12 +188,12 @@ This rounding (to full crowns) is standard and legal per the accounting legislat For that use case, our package lets you get the rounding difference using a simple method call: ```php -$money = Money::fromDecimals(9.90, CZK::class); +$money = Money::fromDecimal(9.90, CZK::class); $money->decimals(); // 9.90 $money->formatted(); // 10 Kč $money->rounding(); // +0.10 Kč = 10 -$money = Money::fromDecimals(3.30, CZK::class); +$money = Money::fromDecimal(3.30, CZK::class); $money->decimals(); // 3.30 $money->formatted(); // 3 Kč $money->rounding(); // -0.30 Kč = -30 @@ -203,13 +203,13 @@ $money->rounding(); // -0.30 Kč = -30 ```php // Using the currency rounding -$money = Money::fromDecimals(9.90, CZK::class); +$money = Money::fromDecimal(9.90, CZK::class); $money->decimals(); // 9.90 $money = $money->rounded(); // currency rounding $money->decimals(); // 10.0 // Using custom rounding -$money = Money::fromDecimals(2.22, USD::class); +$money = Money::fromDecimal(2.22, USD::class); $money->decimals(); // 2.22 $money = $money->rounded(1); // custom rounding: 1 decimal $money->decimals(); // 2.20 @@ -378,7 +378,7 @@ The package uses the base value for all money calculations. The decimal value isn't used for calculations, but it is the human-readable one. It's typically used in the formatted value. ```php -$money = Money::fromDecimals(100.0); // $100 USD +$money = Money::fromDecimal(100.0); // $100 USD $money->value(); // 10000 $money->decimal(); // 100.0 ``` @@ -431,7 +431,7 @@ The current currency is something you can convert money to in the final step of The default currency is the currency that Money defaults to in the context of your codebase. -The `money()` helper, `Money::fromDecimals()` method, and `new Money()` all use this currency (unless a specific one is provided). +The `money()` helper, `Money::fromDecimal()` method, and `new Money()` all use this currency (unless a specific one is provided). It can be a good idea to use the default currency for data storage. See more about this in the [Value in default currency](#value-in-default-currency) section. @@ -439,7 +439,7 @@ It can be a good idea to use the default currency for data storage. See more abo The math decimals refer to the amount of decimal points the currency has in a math context. -All math operations are still done in floats, using the [base value](#base-value), but the math decimals are used for knowing how to round the money after each operation, how to instantiate it with the `Money::fromDecimals()` method, and more. +All math operations are still done in floats, using the [base value](#base-value), but the math decimals are used for knowing how to round the money after each operation, how to instantiate it with the `Money::fromDecimal()` method, and more. ### Display decimals