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

Logic Money->format( $format = null ) and Money->format_i18n( $format = null ) confusing? #2

Open
remcotolsma opened this issue Jun 28, 2021 · 1 comment
Labels
enhancement New feature or request question Further information is requested

Comments

@remcotolsma
Copy link
Member

In both functions the $format parameter is optional, but the parameter doesn't work the same way. If null is passed in Money->format_i18n( $format = null ) function it will fall back to Money::get_default_format():

wp-money/src/Money.php

Lines 71 to 73 in 86b41ad

if ( is_null( $format ) ) {
$format = self::get_default_format();
}

If null is passed in Money->format( $format = null ) function it will fall back to '%2$s':

wp-money/src/Money.php

Lines 127 to 129 in 86b41ad

if ( is_null( $format ) ) {
$format = '%2$s';
}

Currently it's very imported that Money->format( $format = null ) falls back to '%2$s', otherwise some of the gateways will break:

pronamic/wp-pronamic-pay-mollie@08bc30c

However, it is not very logical that the Money->format( $format = null ) does not process currency information by default, without currency information it's just a number.

We could make this more clear like this: Money->format_amount( $number_decimals = null );, if $number_decimals is null we can fall back to the currency $number_decimals value, similarly an i18n version: $money->format_i18n_amount( $number_decimals );.

Naming can also be different:

  • Money->number_format( $number_decimals = null )
  • Money->amount_format( $number_decimals = null )
  • Money->number_format_i18n( $number_decimals = null )
  • Money->amount_format_i18n( $number_decimals = null )

I think i prefer number_format and number_format_i18n:

The default format for Money->format( $format = null ) can then be changed to '%3$s%2$s' so that it contains at least the currency code by default.

Maybe we should also consider replacing the printf directives with placeholders:

$placeholders = array(
	'{non_breaking_space}'            => ...,
	'{currency_symbol}'               => ...,
	'{currency_alphabetic_code}'      => ...,
	'{currency_numeric_code}'         => ...,
	'{currency_name}'                 => ...,
	'{amount}'                        => ...,
	'{amount_i18n}'                   => ...,
	'{amount_no_trailing_zeros}'      => ...,
	'{amount_i18n_no_trailing_zeros}' => ...,
);

Current use is not very clear:

echo $money->format( '%3$s%2$s' );

You have no idea what the result is, following is more clear:

echo $money->format( '{currency_alphabetic_code}{non_breaking_space}{amount}' );

@rvdsteege thoughts?

@remcotolsma remcotolsma added enhancement New feature or request question Further information is requested labels Jun 28, 2021
@rvdsteege
Copy link
Member

New methods added by @remcotolsma in a08b3f8:

  • Money->number_format( $decimals, $decimal_separator, $thousands_separator )
  • Money->number_format_i18n( $decimals )

@rvdsteege rvdsteege removed their assignment Sep 6, 2021
@remcotolsma remcotolsma transferred this issue from pronamic/wp-pronamic-pay Feb 14, 2023
@remcotolsma remcotolsma moved this to Todo in Pronamic Pay Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
Status: Todo
Development

No branches or pull requests

2 participants