1313use Magento \Framework \Locale \ResolverInterface as LocalResolverInterface ;
1414use Magento \Framework \NumberFormatterFactory ;
1515use Magento \Framework \Serialize \Serializer \Json ;
16+ use Magento \Framework \Exception \LocalizedException ;
1617
1718/**
1819 * Currency model
@@ -39,8 +40,6 @@ class Currency extends \Magento\Framework\Model\AbstractModel
3940 protected $ _filter ;
4041
4142 /**
42- * Currency Rates
43- *
4443 * @var array
4544 */
4645 protected $ _rates ;
@@ -147,11 +146,14 @@ public function __construct(
147146 $ this ->_localeCurrency = $ localeCurrency ;
148147 $ this ->currencyConfig = $ currencyConfig ?: ObjectManager::getInstance ()->get (CurrencyConfig::class);
149148 $ this ->localeResolver = $ localeResolver ?: ObjectManager::getInstance ()->get (LocalResolverInterface::class);
150- $ this ->numberFormatterFactory = $ numberFormatterFactory ?: ObjectManager::getInstance ()->get (NumberFormatterFactory::class);
149+ $ this ->numberFormatterFactory = $ numberFormatterFactory ?:
150+ ObjectManager::getInstance ()->get (NumberFormatterFactory::class);
151151 $ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
152152 }
153153
154154 /**
155+ * Initializing Currency Resource model
156+ *
155157 * @return void
156158 */
157159 protected function _construct ()
@@ -253,10 +255,10 @@ public function getAnyRate($toCurrency)
253255 /**
254256 * Convert price to currency format
255257 *
256- * @param float $price
257- * @param mixed $toCurrency
258- * @return float
259- * @throws \Exception
258+ * @param float $price
259+ * @param mixed $toCurrency
260+ * @return float
261+ * @throws LocalizedException
260262 */
261263 public function convert ($ price , $ toCurrency = null )
262264 {
@@ -266,15 +268,18 @@ public function convert($price, $toCurrency = null)
266268 return (float )$ price * (float )$ rate ;
267269 }
268270
269- throw new \ Exception (__ (
271+ throw new LocalizedException (__ (
270272 'Undefined rate from "%1-%2". ' ,
271273 $ this ->getCode (),
272274 $ this ->getCurrencyCodeFromToCurrency ($ toCurrency )
273275 ));
274276 }
275277
276278 /**
279+ * Return the currency code
280+ *
277281 * @param mixed $toCurrency
282+ *
278283 * @return string
279284 * @throws \Magento\Framework\Exception\InputException
280285 */
@@ -348,8 +353,11 @@ public function formatPrecision(
348353 }
349354
350355 /**
356+ * Return formatted currency
357+ *
351358 * @param float $price
352359 * @param array $options
360+ *
353361 * @return string
354362 */
355363 public function formatTxt ($ price , $ options = [])
@@ -420,7 +428,8 @@ private function formatCurrency(string $price, array $options): string
420428 $ this ->numberFormatter = $ this ->getNumberFormatter ($ options );
421429
422430 $ formattedCurrency = $ this ->numberFormatter ->formatCurrency (
423- $ price , $ this ->getCode () ?? $ this ->numberFormatter ->getTextAttribute (\NumberFormatter::CURRENCY_CODE )
431+ $ price ,
432+ $ this ->getCode () ?? $ this ->numberFormatter ->getTextAttribute (\NumberFormatter::CURRENCY_CODE )
424433 );
425434
426435 if (array_key_exists (LocaleCurrency::CURRENCY_OPTION_SYMBOL , $ options )) {
@@ -430,7 +439,7 @@ private function formatCurrency(string $price, array $options): string
430439
431440 if ((array_key_exists (LocaleCurrency::CURRENCY_OPTION_DISPLAY , $ options )
432441 && $ options [LocaleCurrency::CURRENCY_OPTION_DISPLAY ] === \Magento \Framework \Currency::NO_SYMBOL )) {
433- $ formattedCurrency = str_replace ( ' ' , '' , $ formattedCurrency );
442+ $ formattedCurrency = preg_replace ([ ' /[^0-9.,۰٫]+/ ' , ' / / ' ] , '' , $ formattedCurrency );
434443 }
435444
436445 return preg_replace ('/^\s+|\s+$/u ' , '' , $ formattedCurrency );
@@ -444,7 +453,10 @@ private function formatCurrency(string $price, array $options): string
444453 */
445454 private function getNumberFormatter (array $ options ): \Magento \Framework \NumberFormatter
446455 {
447- $ key = 'currency_ ' . md5 ($ this ->localeResolver ->getLocale () . $ this ->serializer ->serialize ($ options ));
456+ $ key = 'currency_ ' . hash (
457+ 'sha256 ' ,
458+ ($ this ->localeResolver ->getLocale () . $ this ->serializer ->serialize ($ options ))
459+ );
448460 if (!isset ($ this ->numberFormatterCache [$ key ])) {
449461 $ this ->numberFormatter = $ this ->numberFormatterFactory ->create (
450462 ['locale ' => $ this ->localeResolver ->getLocale (), 'style ' => \NumberFormatter::CURRENCY ]
@@ -467,7 +479,8 @@ private function setOptions(array $options): void
467479 {
468480 if (array_key_exists (LocaleCurrency::CURRENCY_OPTION_SYMBOL , $ options )) {
469481 $ this ->numberFormatter ->setSymbol (
470- \NumberFormatter::CURRENCY_SYMBOL , $ options [LocaleCurrency::CURRENCY_OPTION_SYMBOL ]
482+ \NumberFormatter::CURRENCY_SYMBOL ,
483+ $ options [LocaleCurrency::CURRENCY_OPTION_SYMBOL ]
471484 );
472485 }
473486 if (array_key_exists (LocaleCurrency::CURRENCY_OPTION_DISPLAY , $ options )
@@ -490,6 +503,8 @@ public function getCurrencySymbol()
490503 }
491504
492505 /**
506+ * Return the price format to be displayed to user
507+ *
493508 * @return string
494509 */
495510 public function getOutputFormat ()
@@ -532,6 +547,8 @@ public function getConfigDefaultCurrencies()
532547 }
533548
534549 /**
550+ * Retrieve base config currency data by config path.
551+ *
535552 * @return array
536553 */
537554 public function getConfigBaseCurrencies ()
0 commit comments