Skip to content

Commit ce4f831

Browse files
committed
Merge branch 'ACP2E-2022' of https://github.com/magento-l3/magento2ce into L3-PR-2023-06-23
2 parents 6b91873 + 637dd69 commit ce4f831

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed

lib/internal/Magento/Framework/Currency/Data/Currency.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ public function toCurrency($value = null, array $options = []): string
167167
}
168168
$options = array_merge($this->options, $this->checkOptions($options));
169169
$numberFormatter = new NumberFormatter($options['locale'], NumberFormatter::CURRENCY);
170+
if (isset($options['precision'])) {
171+
$numberFormatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $options['precision']);
172+
}
173+
170174
$value = $numberFormatter->format((float) $value);
171175

172176
if (is_numeric($options['display']) === false) {
@@ -188,7 +192,23 @@ public function toCurrency($value = null, array $options = []): string
188192
}
189193
}
190194

191-
return str_replace($this->getSymbol(null, $options['locale']), (string) $sign, $value);
195+
$currencySymbol = $this->getSymbol(null, $options['locale']);
196+
if ($options['position'] !== self::STANDARD) {
197+
$value = str_replace($currencySymbol, '', $value);
198+
$space = '';
199+
if (strpos($value, ' ') !== false) {
200+
$value = str_replace(' ', '', $value);
201+
$space = ' ';
202+
}
203+
204+
if ($options['position'] == self::LEFT) {
205+
$value = $currencySymbol . $space . $value;
206+
} else {
207+
$value = $value . $space . $currencySymbol;
208+
}
209+
}
210+
211+
return str_replace($currencySymbol, (string) $sign, $value);
192212
}
193213

194214
/**
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\Framework\Test\Unit\Currency\Data;
10+
11+
use Magento\Framework\Currency\Data\Currency;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Test for Magento\Framework\Currency\Data\Currency
16+
*/
17+
class CurrencyTest extends TestCase
18+
{
19+
/**
20+
* @param float|int|null $value
21+
* @param array $options
22+
* @param string $expectedResult
23+
* @return void
24+
* @throws \Magento\Framework\Currency\Exception\CurrencyException
25+
*
26+
* @dataProvider optionsDataProvider
27+
*/
28+
public function testToCurrencyWithOptions(float|int|null $value, array $options, string $expectedResult): void
29+
{
30+
$currency = new Currency();
31+
$result = $currency->toCurrency($value, $options);
32+
33+
$this->assertEquals($expectedResult, $result);
34+
}
35+
36+
/**
37+
* @return array[]
38+
*/
39+
public function optionsDataProvider(): array
40+
{
41+
return [
42+
'rightPosition_en_AU' => [
43+
'value' => 3,
44+
'options' => [
45+
'position' => Currency::RIGHT,
46+
'locale' => 'en_AU',
47+
'currency' => 'AUD',
48+
],
49+
'expectedResult' => '3.00$',
50+
],
51+
'leftPosition_en_AU' => [
52+
'value' => 3,
53+
'options' => [
54+
'position' => Currency::LEFT,
55+
'locale' => 'en_AU',
56+
'currency' => 'AUD',
57+
],
58+
'expectedResult' => '$3.00',
59+
],
60+
'defaultPosition_en_AU' => [
61+
'value' => 22,
62+
'options' => [
63+
'locale' => 'en_AU',
64+
'currency' => 'AUD',
65+
],
66+
'expectedResult' => '$22.00',
67+
],
68+
69+
'rightPosition_CUST_en_US' => [
70+
'value' => 12,
71+
'options' => [
72+
'position' => Currency::RIGHT,
73+
'locale' => 'en_US',
74+
'symbol' => 'CUST',
75+
],
76+
'expectedResult' => '12.00CUST',
77+
],
78+
'leftPosition_CUST_en_US' => [
79+
'value' => 12,
80+
'options' => [
81+
'position' => Currency::LEFT,
82+
'locale' => 'en_US',
83+
'symbol' => 'CUST',
84+
],
85+
'expectedResult' => 'CUST12.00',
86+
],
87+
'rightPosition_CUST_with_space_zu_ZA' => [
88+
'value' => 12,
89+
'options' => [
90+
'position' => Currency::RIGHT,
91+
'locale' => 'zu_ZA',
92+
'symbol' => 'CUST',
93+
],
94+
'expectedResult' => '12.00 CUST',
95+
],
96+
'leftPosition_CUST_with_space_zu_ZA' => [
97+
'value' => 12,
98+
'options' => [
99+
'position' => Currency::LEFT,
100+
'locale' => 'zu_ZA',
101+
'symbol' => 'CUST',
102+
],
103+
'expectedResult' => 'CUST 12.00',
104+
],
105+
'precisionIsGreaterThanZero' => [
106+
'value' => 12.16,
107+
'options' => [
108+
'locale' => 'en_US',
109+
'currency' => 'USD',
110+
'precision'=> 1,
111+
],
112+
'expectedResult' => '$12.2',
113+
],
114+
'precisionIsZero' => [
115+
'value' => 12.16,
116+
'options' => [
117+
'locale' => 'en_US',
118+
'currency' => 'USD',
119+
'precision'=> 0,
120+
],
121+
'expectedResult' => '$12',
122+
],
123+
];
124+
}
125+
}

0 commit comments

Comments
 (0)