13
13
use Magento \Framework \Locale \ResolverInterface as LocalResolverInterface ;
14
14
use Magento \Framework \NumberFormatterFactory ;
15
15
use Magento \Framework \Serialize \Serializer \Json ;
16
+ use Magento \Framework \Exception \LocalizedException ;
16
17
17
18
/**
18
19
* Currency model
@@ -39,8 +40,6 @@ class Currency extends \Magento\Framework\Model\AbstractModel
39
40
protected $ _filter ;
40
41
41
42
/**
42
- * Currency Rates
43
- *
44
43
* @var array
45
44
*/
46
45
protected $ _rates ;
@@ -147,11 +146,14 @@ public function __construct(
147
146
$ this ->_localeCurrency = $ localeCurrency ;
148
147
$ this ->currencyConfig = $ currencyConfig ?: ObjectManager::getInstance ()->get (CurrencyConfig::class);
149
148
$ 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);
151
151
$ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
152
152
}
153
153
154
154
/**
155
+ * Initializing Currency Resource model
156
+ *
155
157
* @return void
156
158
*/
157
159
protected function _construct ()
@@ -253,10 +255,10 @@ public function getAnyRate($toCurrency)
253
255
/**
254
256
* Convert price to currency format
255
257
*
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
260
262
*/
261
263
public function convert ($ price , $ toCurrency = null )
262
264
{
@@ -266,15 +268,18 @@ public function convert($price, $toCurrency = null)
266
268
return (float )$ price * (float )$ rate ;
267
269
}
268
270
269
- throw new \ Exception (__ (
271
+ throw new LocalizedException (__ (
270
272
'Undefined rate from "%1-%2". ' ,
271
273
$ this ->getCode (),
272
274
$ this ->getCurrencyCodeFromToCurrency ($ toCurrency )
273
275
));
274
276
}
275
277
276
278
/**
279
+ * Return the currency code
280
+ *
277
281
* @param mixed $toCurrency
282
+ *
278
283
* @return string
279
284
* @throws \Magento\Framework\Exception\InputException
280
285
*/
@@ -348,8 +353,11 @@ public function formatPrecision(
348
353
}
349
354
350
355
/**
356
+ * Return formatted currency
357
+ *
351
358
* @param float $price
352
359
* @param array $options
360
+ *
353
361
* @return string
354
362
*/
355
363
public function formatTxt ($ price , $ options = [])
@@ -420,7 +428,8 @@ private function formatCurrency(string $price, array $options): string
420
428
$ this ->numberFormatter = $ this ->getNumberFormatter ($ options );
421
429
422
430
$ 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 )
424
433
);
425
434
426
435
if (array_key_exists (LocaleCurrency::CURRENCY_OPTION_SYMBOL , $ options )) {
@@ -430,7 +439,7 @@ private function formatCurrency(string $price, array $options): string
430
439
431
440
if ((array_key_exists (LocaleCurrency::CURRENCY_OPTION_DISPLAY , $ options )
432
441
&& $ options [LocaleCurrency::CURRENCY_OPTION_DISPLAY ] === \Magento \Framework \Currency::NO_SYMBOL )) {
433
- $ formattedCurrency = str_replace ( ' ' , '' , $ formattedCurrency );
442
+ $ formattedCurrency = preg_replace ([ ' /[^0-9.,۰٫]+/ ' , ' / / ' ] , '' , $ formattedCurrency );
434
443
}
435
444
436
445
return preg_replace ('/^\s+|\s+$/u ' , '' , $ formattedCurrency );
@@ -444,7 +453,10 @@ private function formatCurrency(string $price, array $options): string
444
453
*/
445
454
private function getNumberFormatter (array $ options ): \Magento \Framework \NumberFormatter
446
455
{
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
+ );
448
460
if (!isset ($ this ->numberFormatterCache [$ key ])) {
449
461
$ this ->numberFormatter = $ this ->numberFormatterFactory ->create (
450
462
['locale ' => $ this ->localeResolver ->getLocale (), 'style ' => \NumberFormatter::CURRENCY ]
@@ -467,7 +479,8 @@ private function setOptions(array $options): void
467
479
{
468
480
if (array_key_exists (LocaleCurrency::CURRENCY_OPTION_SYMBOL , $ options )) {
469
481
$ this ->numberFormatter ->setSymbol (
470
- \NumberFormatter::CURRENCY_SYMBOL , $ options [LocaleCurrency::CURRENCY_OPTION_SYMBOL ]
482
+ \NumberFormatter::CURRENCY_SYMBOL ,
483
+ $ options [LocaleCurrency::CURRENCY_OPTION_SYMBOL ]
471
484
);
472
485
}
473
486
if (array_key_exists (LocaleCurrency::CURRENCY_OPTION_DISPLAY , $ options )
@@ -490,6 +503,8 @@ public function getCurrencySymbol()
490
503
}
491
504
492
505
/**
506
+ * Return the price format to be displayed to user
507
+ *
493
508
* @return string
494
509
*/
495
510
public function getOutputFormat ()
@@ -532,6 +547,8 @@ public function getConfigDefaultCurrencies()
532
547
}
533
548
534
549
/**
550
+ * Retrieve base config currency data by config path.
551
+ *
535
552
* @return array
536
553
*/
537
554
public function getConfigBaseCurrencies ()
0 commit comments