@@ -83,6 +83,11 @@ class Currency extends \Magento\Framework\Model\AbstractModel
83
83
*/
84
84
private $ numberFormatterFactory ;
85
85
86
+ /**
87
+ * @var \Magento\Framework\NumberFormatter
88
+ */
89
+ private $ numberFormatter ;
90
+
86
91
/**
87
92
* @param \Magento\Framework\Model\Context $context
88
93
* @param \Magento\Framework\Registry $registry
@@ -345,31 +350,81 @@ public function formatTxt($price, $options = [])
345
350
*/
346
351
$ price = sprintf ("%F " , $ price );
347
352
353
+ if ($ this ->useNumberFormatter ($ options )) {
354
+ return $ this ->formatCurrency ($ price , $ options );
355
+ }
356
+
357
+ return $ this ->_localeCurrency ->getCurrency ($ this ->getCode ())->toCurrency ($ price , $ options );
358
+ }
359
+
360
+ /**
361
+ * Check if to use Intl.NumberFormatter to format currency.
362
+ *
363
+ * @param array $options
364
+ * @return bool
365
+ */
366
+ private function useNumberFormatter (array $ options ): bool
367
+ {
368
+ return (empty ($ options ) || array_key_exists (LocaleCurrency::CURRENCY_OPTION_SYMBOL , $ options )
369
+ || (array_key_exists (LocaleCurrency::CURRENCY_OPTION_DISPLAY , $ options )
370
+ && ($ options [LocaleCurrency::CURRENCY_OPTION_DISPLAY ] === \Magento \Framework \Currency::NO_SYMBOL
371
+ || $ options [LocaleCurrency::CURRENCY_OPTION_DISPLAY ] === \Magento \Framework \Currency::USE_SYMBOL ))
372
+ || array_key_exists ('precision ' , $ options ));
373
+ }
374
+
375
+ /**
376
+ * Format currency.
377
+ *
378
+ * @param string $price
379
+ * @param array $options
380
+ * @return string
381
+ */
382
+ private function formatCurrency (string $ price , array $ options ): string
383
+ {
348
384
$ customerOptions = new \Magento \Framework \DataObject ([]);
385
+
349
386
$ this ->_eventManager ->dispatch (
350
387
'currency_display_options_forming ' ,
351
388
['currency_options ' => $ customerOptions , 'base_code ' => $ this ->getCode ()]
352
389
);
353
390
$ options = array_merge ($ options , $ customerOptions ->toArray ());
354
391
355
- $ numberFormatter = $ this ->numberFormatterFactory ->create (
392
+ $ this -> numberFormatter = $ this ->numberFormatterFactory ->create (
356
393
['locale ' => $ this ->localeResolver ->getLocale (), 'style ' => \NumberFormatter::CURRENCY ]
357
394
);
358
395
396
+ $ this ->setOptions ($ options );
397
+
398
+ $ formattedCurrency = $ this ->numberFormatter ->formatCurrency ($ price , $ this ->getCode ());
399
+
400
+ if (array_key_exists (LocaleCurrency::CURRENCY_OPTION_DISPLAY , $ options )
401
+ && $ options [LocaleCurrency::CURRENCY_OPTION_DISPLAY ] === \Magento \Framework \Currency::NO_SYMBOL ) {
402
+ $ formattedCurrency = str_replace (' ' , '' , $ formattedCurrency );
403
+ }
404
+
405
+ return $ formattedCurrency ;
406
+ }
407
+
408
+ /**
409
+ * Set number formatter custom options.
410
+ *
411
+ * @param array $options
412
+ * @return void
413
+ */
414
+ private function setOptions (array $ options ): void
415
+ {
359
416
if (array_key_exists (LocaleCurrency::CURRENCY_OPTION_DISPLAY , $ options )
360
417
&& $ options [LocaleCurrency::CURRENCY_OPTION_DISPLAY ] === \Magento \Framework \Currency::NO_SYMBOL ) {
361
- $ numberFormatter ->setSymbol (\NumberFormatter::CURRENCY_SYMBOL , '' );
418
+ $ this -> numberFormatter ->setSymbol (\NumberFormatter::CURRENCY_SYMBOL , '' );
362
419
}
363
420
if (array_key_exists ('precision ' , $ options )) {
364
- $ numberFormatter ->setAttribute (\NumberFormatter::FRACTION_DIGITS , $ options ['precision ' ]);
421
+ $ this -> numberFormatter ->setAttribute (\NumberFormatter::FRACTION_DIGITS , $ options ['precision ' ]);
365
422
}
366
423
if (array_key_exists (LocaleCurrency::CURRENCY_OPTION_SYMBOL , $ options )) {
367
- $ numberFormatter ->setSymbol (
424
+ $ this -> numberFormatter ->setSymbol (
368
425
\NumberFormatter::CURRENCY_SYMBOL , $ options [LocaleCurrency::CURRENCY_OPTION_SYMBOL ]
369
426
);
370
427
}
371
-
372
- return $ numberFormatter ->formatCurrency ($ price , $ this ->getCode ());
373
428
}
374
429
375
430
/**
0 commit comments