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

Updated IntegerToMoneyFormatHandler.php to fix process method #268

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public function __construct(array $allowedKeys, string $format = '%.2n')
*/
protected function process($key, $value): ?string
{
return money_format($this->format, $value / 100);
$locale = '';

Choose a reason for hiding this comment

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

use LocaleContextInterface

Copy link
Contributor

Choose a reason for hiding this comment

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

Using LocaleContextInterface makes a lot of assumptions. I just inlined a version of this handler to an app to un-block upgrading to PHP 8 and ended up hardcoding the locale in for now because the one spot this handler gets used in our app (order export) the handler lacks the context needed to get the locale from the order, which would mean that the field would be exported in the request's active locale (and in a multi-lingual store that may not be desired).

if(isset($GLOBALS['request']) && $GLOBALS['request']) {
$locale = $GLOBALS['request']->getLocale();
}
$fmt = new \NumberFormatter( $locale, \NumberFormatter::CURRENCY );

Choose a reason for hiding this comment

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

$formatter or $numberFormatter

Choose a reason for hiding this comment

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

no spaces inside braces

return $fmt->formatCurrency($value/100, "EUR");

Choose a reason for hiding this comment

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

EUR should not be hardcoded

Copy link
Contributor

Choose a reason for hiding this comment

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

Same issue as I noted above, we don't have the context to get the right currency code to use here.

}

protected function allows($key, $value): bool
Expand Down