Skip to content

Commit 2d2dbcb

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-1841' into ACP2E-2022
2 parents 827073a + 5f8e4b6 commit 2d2dbcb

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,23 @@ public function toCurrency($value = null, array $options = []): string
188188
}
189189
}
190190

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

194210
/**
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 toCurrencyPositionsDataProvider
27+
*/
28+
public function testToCurrencyPositions(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 toCurrencyPositionsDataProvider(): 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+
];
106+
}
107+
}

0 commit comments

Comments
 (0)