Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(currency): allow enums to be passed as currency #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

CodeLieutenant
Copy link

It's really a common case to define currencies as enum, espacially with PHP 8.1, laravel also supports them (both saving and retrieving from the database).

Usecase

// Enums/Currency.php
enum Currency: string {
   case USD = 'USD';
}

// Models/Item.php
class Item extends Model {
   protected function casts(): array {
       return [
           'total' => MoneyDecimalCast::class . ':currency',
           'currency' => Currency::class,
       ];
   }
}

Without this, Money::parseCurrency just returns the enum passed to it and then Money\Money fails with exception, since it require $currency to be either string or Money\Currency

Copy link

codecov bot commented Mar 8, 2025

Codecov Report

Attention: Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 99.79%. Comparing base (fdca65e) to head (20bcac9).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/CurrenciesTrait.php 75.00% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##                main     #177      +/-   ##
=============================================
- Coverage     100.00%   99.79%   -0.21%     
- Complexity       172      174       +2     
=============================================
  Files             22       22              
  Lines            475      479       +4     
=============================================
+ Hits             475      478       +3     
- Misses             0        1       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@ricardogobbosouza ricardogobbosouza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @CodeLieutenant

Just one thing the coverage complained about.

Thanks for the PR

@@ -34,6 +34,14 @@ public static function parseCurrency($currency)
return new Currency($currency);
}

if ($currency instanceof \StringBackedEnum) {
return new Currency($currency->value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not being covered by the tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants