Skip to content

Commit fdac295

Browse files
author
Yurii Hryhoriev
committed
MAGETWO-57070: [GitHub] Locale\Format throws away country part, results in wrong number format #5073
1 parent 13fa125 commit fdac295

File tree

3 files changed

+137
-29
lines changed

3 files changed

+137
-29
lines changed

lib/internal/Magento/Framework/Locale/Bundle/DataBundle.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ protected function cleanLocale($locale)
6666
if (isset($localeParts['language'])) {
6767
$cleanLocaleParts['language'] = $localeParts['language'];
6868
}
69-
if (isset($localeParts['region'])) {
70-
$cleanLocaleParts['region'] = $localeParts['region'];
71-
}
7269
if (isset($localeParts['script'])) {
7370
$cleanLocaleParts['script'] = $localeParts['script'];
7471
}

lib/internal/Magento/Framework/Locale/Format.php

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@
55
*/
66
namespace Magento\Framework\Locale;
77

8-
use Magento\Framework\Locale\Bundle\DataBundle;
9-
108
class Format implements \Magento\Framework\Locale\FormatInterface
119
{
12-
/**
13-
* @var string
14-
*/
15-
private static $defaultNumberSet = 'latn';
16-
1710
/**
1811
* @var \Magento\Framework\App\ScopeResolverInterface
1912
*/
@@ -68,7 +61,7 @@ public function getNumber($value)
6861
}
6962

7063
if (!is_string($value)) {
71-
return floatval($value);
64+
return (float)$value;
7265
}
7366

7467
//trim spaces and apostrophes
@@ -79,16 +72,15 @@ public function getNumber($value)
7972

8073
if ($separatorComa !== false && $separatorDot !== false) {
8174
if ($separatorComa > $separatorDot) {
82-
$value = str_replace('.', '', $value);
83-
$value = str_replace(',', '.', $value);
75+
$value = str_replace(['.', ','], ['', '.'], $value);
8476
} else {
8577
$value = str_replace(',', '', $value);
8678
}
8779
} elseif ($separatorComa !== false) {
8880
$value = str_replace(',', '.', $value);
8981
}
9082

91-
return floatval($value);
83+
return (float)$value;
9284
}
9385

9486
/**
@@ -97,8 +89,6 @@ public function getNumber($value)
9789
* @param string $localeCode Locale code.
9890
* @param string $currencyCode Currency code.
9991
* @return array
100-
* @SuppressWarnings(PHPMD.NPathComplexity)
101-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
10292
*/
10393
public function getPriceFormat($localeCode = null, $currencyCode = null)
10494
{
@@ -108,25 +98,17 @@ public function getPriceFormat($localeCode = null, $currencyCode = null)
10898
} else {
10999
$currency = $this->_scopeResolver->getScope()->getCurrentCurrency();
110100
}
111-
$localeData = (new DataBundle())->get($localeCode);
112-
$defaultSet = $localeData['NumberElements']['default'] ?: self::$defaultNumberSet;
113-
$format = $localeData['NumberElements'][$defaultSet]['patterns']['currencyFormat']
114-
?: ($localeData['NumberElements'][self::$defaultNumberSet]['patterns']['currencyFormat']
115-
?: explode(';', $localeData['NumberPatterns'][1])[0]);
116-
117-
$decimalSymbol = $localeData['NumberElements'][$defaultSet]['symbols']['decimal']
118-
?: ($localeData['NumberElements'][self::$defaultNumberSet]['symbols']['decimal']
119-
?: $localeData['NumberElements'][0]);
120101

121-
$groupSymbol = $localeData['NumberElements'][$defaultSet]['symbols']['group']
122-
?: ($localeData['NumberElements'][self::$defaultNumberSet]['symbols']['group']
123-
?: $localeData['NumberElements'][1]);
102+
$formatter = new \NumberFormatter($localeCode, \NumberFormatter::CURRENCY);
103+
$format = $formatter->getPattern();
104+
$decimalSymbol = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
105+
$groupSymbol = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
124106

125107
$pos = strpos($format, ';');
126108
if ($pos !== false) {
127109
$format = substr($format, 0, $pos);
128110
}
129-
$format = preg_replace("/[^0\#\.,]/", "", $format);
111+
$format = preg_replace("/[^0\#\.,]/", '', $format);
130112
$totalPrecision = 0;
131113
$decimalPoint = strpos($format, '.');
132114
if ($decimalPoint !== false) {
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Locale\Test\Unit;
8+
9+
class FormatTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var \Magento\Framework\Locale\Format
13+
*/
14+
protected $formatModel;
15+
16+
/**
17+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Locale\ResolverInterface
18+
*/
19+
protected $localeResolver;
20+
21+
/**
22+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ScopeInterface
23+
*/
24+
protected $scope;
25+
26+
/**
27+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ScopeResolverInterface
28+
*/
29+
protected $scopeResolver;
30+
31+
/**
32+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Directory\Model\Currency
33+
*/
34+
protected $currency;
35+
36+
protected function setUp()
37+
{
38+
$this->currency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
39+
->disableOriginalConstructor()
40+
->getMock();
41+
$this->scope = $this->getMockBuilder(\Magento\Framework\App\ScopeInterface::class)
42+
->setMethods(['getCurrentCurrency'])
43+
->getMockForAbstractClass();
44+
$this->scope->expects($this->once())
45+
->method('getCurrentCurrency')
46+
->willReturn($this->currency);
47+
$this->scopeResolver = $this->getMockBuilder(\Magento\Framework\App\ScopeResolverInterface::class)
48+
->setMethods(['getScope'])
49+
->getMockForAbstractClass();
50+
$this->scopeResolver->expects($this->any())
51+
->method('getScope')
52+
->willReturn($this->scope);
53+
$this->localeResolver = $this->getMockBuilder(\Magento\Framework\Locale\ResolverInterface::class)
54+
->getMock();
55+
$currencyFactory = $this->getMockBuilder(\Magento\Directory\Model\CurrencyFactory::class)
56+
->getMock();
57+
58+
$this->formatModel = new \Magento\Framework\Locale\Format(
59+
$this->scopeResolver,
60+
$this->localeResolver,
61+
$currencyFactory
62+
);
63+
}
64+
65+
/**
66+
* @param $localeCode
67+
* @param $expectedResult
68+
* @dataProvider getPriceFormatDataProvider
69+
*/
70+
public function testGetPriceFormat($localeCode, $expectedResult)
71+
{
72+
$result = $this->formatModel->getPriceFormat($localeCode);
73+
$this->assertEquals($result, $expectedResult);
74+
}
75+
76+
/**
77+
* @return array
78+
*/
79+
public function getPriceFormatDataProvider()
80+
{
81+
return [
82+
[
83+
'en_US',
84+
[
85+
'pattern' => null,
86+
'precision' => 2,
87+
'requiredPrecision' => 2,
88+
'decimalSymbol' => '.',
89+
'groupSymbol' => ',',
90+
'groupLength' => 3,
91+
'integerRequired' => 1
92+
]
93+
], [
94+
'de_DE',
95+
[
96+
'pattern' => null,
97+
'precision' => 2,
98+
'requiredPrecision' => 2,
99+
'decimalSymbol' => ',',
100+
'groupSymbol' => '.',
101+
'groupLength' => 3,
102+
'integerRequired' => 1
103+
]
104+
], [
105+
'de_CH',
106+
[
107+
'pattern' => null,
108+
'precision' => 2,
109+
'requiredPrecision' => 2,
110+
'decimalSymbol' => '.',
111+
'groupSymbol' => '\'',
112+
'groupLength' => 3,
113+
'integerRequired' => 1
114+
]
115+
], [
116+
'uk_UA',
117+
[
118+
'pattern' => null,
119+
'precision' => 2,
120+
'requiredPrecision' => 2,
121+
'decimalSymbol' => ',',
122+
'groupSymbol' => ' ',
123+
'groupLength' => 3,
124+
'integerRequired' => 1
125+
]
126+
]
127+
];
128+
}
129+
}

0 commit comments

Comments
 (0)