Repository navigation
Add support for Money based formatters - #103
mklepaczewski wants to merge 4 commits into
Conversation
|
Hi, I think that adding a However, I'm a bit hesistant about adding it to the signature of |
Good question! Also, thank you for all the work on the project. We already have Moreover, when a developer wants to switch to custom formatting using /* $formatter is MoneyFormatter or NumberFormatter *
$logger->info("Processing transaction", [ $amount->formatWith( $formatter) ]);vs /* $formatter is NumberFormatter */
$logger->info("Processing transaction", [ $amount->formatWith( $formatter) ]);
/* But if we decide to use MoneyFormatter we need to change more than just the argument */
$logger->info("Processing transaction", [ $formatter->format($amount) ]); |
|
I think the main takeaway is that it indeed doesn't hurt. While I'm not opposed to your PR as it is, I'd like to also explore another idea:
This way, we delegate all formatting concerns to other classes, and only keep the utility function What do you think? |
|
Yes to all. I’ll work on it in a day or two. However, I’d like to avoid breaking changes since we plan to use this feature in our project (and you might decide to bump the PHP version with this change, which would prevent us from using the PR for a while). So, I propose we do everything you suggested, except for any changes to |
|
OK for marking |
|
Replaced by #105 |
Description
This PR extends
Money::formatWith()to support custom formatters by broadening its signature:This change uses union types introduced in PHP 8.0. The project requires PHP 8.1.
A new
MoneyFormatterinterface is introduced, but this PR does not include any concrete implementations. A simple anonymous implementation is used in the test suite to demonstrate usage.Problem this PR solves
NumberFormatteronly acceptsfloat, which can lead to loss of precision (it's acknowleded here https://github.com/brick/money#formatting). The newMoneyFormatterinterface accepts aMoneyobject directly, giving the formatter full control and avoiding conversion issues.NumberFormatterto customize formatting, which is cumbersome since that class handles far more than just currency formatting.MoneyFormatterhas only a single purpose and one method.Backward compatibility
This change is backward-compatible:
Money::formatWith()with aNumberFormatterinstance will continue to work unchanged.Moneyis declared final, so it cannot be subclassed - meaning no signature conflicts can occur.