Skip to content

Commit 55560e0

Browse files
committed
merged branch fabpot/locale-fix (PR symfony#9098)
This PR was merged into the 2.2 branch. Discussion ---------- [Locale] added some more stubs for the number formatter | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#8588, symfony#6045 | License | MIT | Doc PR | n/a I've used this snippet of code to populate the default values for the en locale: ```php for ($style = 0; $style <= 8; $style++) { $f = new \NumberFormatter('en', $style); echo 'array('; for ($i = 0; $i <= 17; $i++) { echo "'".$f->getSymbol($i)."', "; } echo "),\n"; } ``` Commits ------- 3108c71 [Locale] added support for the position argument to NumberFormatter::parse() 0774c79 [Locale] added some more stubs for the number formatter
2 parents 2e87d1d + 3108c71 commit 55560e0

File tree

2 files changed

+56
-38
lines changed

2 files changed

+56
-38
lines changed

src/Symfony/Component/Locale/Stub/StubNumberFormatter.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ class StubNumberFormatter
221221
'negative' => -9223372036854775808
222222
);
223223

224+
private static $enSymbols = array(
225+
self::DECIMAL => array('.', ',', ';', '%', '0', '#', '-', '+', '¤', '¤¤', '.', 'E', '', '*', '', 'NaN', '@', ','),
226+
self::CURRENCY => array('.', ',', ';', '%', '0', '#', '-', '+', '¤', '¤¤', '.', 'E', '', '*', '', 'NaN', '@', ','),
227+
);
228+
229+
private static $enTextAttributes = array(
230+
self::DECIMAL => array('', '', '-', '', '*', '', ''),
231+
self::CURRENCY => array('¤', '', '', ')', '*', ''),
232+
);
233+
224234
/**
225235
* Constructor
226236
*
@@ -435,12 +445,10 @@ public function getPattern()
435445
* @return Boolean|string The symbol value or false on error
436446
*
437447
* @see http://www.php.net/manual/en/numberformatter.getsymbol.php
438-
*
439-
* @throws MethodNotImplementedException
440448
*/
441449
public function getSymbol($attr)
442450
{
443-
throw new MethodNotImplementedException(__METHOD__);
451+
return array_key_exists($this->style, self::$enSymbols) && array_key_exists($attr, self::$enSymbols[$this->style]) ? self::$enSymbols[$this->style][$attr] : false;
444452
}
445453

446454
/**
@@ -451,12 +459,10 @@ public function getSymbol($attr)
451459
* @return Boolean|string The attribute value or false on error
452460
*
453461
* @see http://www.php.net/manual/en/numberformatter.gettextattribute.php
454-
*
455-
* @throws MethodNotImplementedException
456462
*/
457463
public function getTextAttribute($attr)
458464
{
459-
throw new MethodNotImplementedException(__METHOD__);
465+
return array_key_exists($this->style, self::$enTextAttributes) && array_key_exists($attr, self::$enTextAttributes[$this->style]) ? self::$enTextAttributes[$this->style][$attr] : false;
460466
}
461467

462468
/**
@@ -487,36 +493,31 @@ public function parseCurrency($value, &$currency, &$position = null)
487493
* @return Boolean|string The parsed value of false on error
488494
*
489495
* @see http://www.php.net/manual/en/numberformatter.parse.php
490-
*
491-
* @throws MethodArgumentNotImplementedException When $position different than null, behavior not implemented
492496
*/
493-
public function parse($value, $type = self::TYPE_DOUBLE, &$position = null)
497+
public function parse($value, $type = self::TYPE_DOUBLE, &$position = 0)
494498
{
495499
if ($type == self::TYPE_DEFAULT || $type == self::TYPE_CURRENCY) {
496500
trigger_error(__METHOD__.'(): Unsupported format type '.$type, \E_USER_WARNING);
497501

498502
return false;
499503
}
500504

501-
// We don't calculate the position when parsing the value
502-
if (null !== $position) {
503-
throw new MethodArgumentNotImplementedException(__METHOD__, 'position');
504-
}
505-
506-
preg_match('/^([^0-9\-]{0,})(.*)/', $value, $matches);
505+
preg_match('/^([^0-9\-\.]{0,})(.*)/', $value, $matches);
507506

508507
// Any string before the numeric value causes error in the parsing
509508
if (isset($matches[1]) && !empty($matches[1])) {
510509
StubIntl::setError(StubIntl::U_PARSE_ERROR, 'Number parsing failed');
511510
$this->errorCode = StubIntl::getErrorCode();
512511
$this->errorMessage = StubIntl::getErrorMessage();
512+
$position = 0;
513513

514514
return false;
515515
}
516516

517-
// Remove everything that is not number or dot (.)
518-
$value = preg_replace('/[^0-9\.\-]/', '', $value);
517+
preg_match('/^[0-9\-\.\,]*/', $value, $matches);
518+
$value = preg_replace('/[^0-9\.\-]/', '', $matches[0]);
519519
$value = $this->convertValueDataType($value, $type);
520+
$position = strlen($matches[0]);
520521

521522
// behave like the intl extension
522523
$this->resetError();

src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -707,22 +707,36 @@ public function testGetPattern()
707707
$formatter->getPattern();
708708
}
709709

710-
/**
711-
* @expectedException \Symfony\Component\Locale\Exception\MethodNotImplementedException
712-
*/
713710
public function testGetSymbol()
714711
{
715-
$formatter = $this->getStubFormatterWithDecimalStyle();
716-
$formatter->getSymbol(null);
712+
$this->skipIfIntlExtensionIsNotLoaded();
713+
714+
$intlDecimalFormatter = new \NumberFormatter('en', \NumberFormatter::DECIMAL);
715+
$intlCurrencyFormatter = new \NumberFormatter('en', \NumberFormatter::CURRENCY);
716+
717+
$stubDecimalFormatter = $this->getStubFormatterWithDecimalStyle();
718+
$stubCurrencyFormatter = $this->getStubFormatterWithCurrencyStyle();
719+
720+
for ($i = 0; $i <= 17; $i++) {
721+
$this->assertSame($stubDecimalFormatter->getSymbol($i), $intlDecimalFormatter->getSymbol($i), $i);
722+
$this->assertSame($stubCurrencyFormatter->getSymbol($i), $intlCurrencyFormatter->getSymbol($i), $i);
723+
}
717724
}
718725

719-
/**
720-
* @expectedException \Symfony\Component\Locale\Exception\MethodNotImplementedException
721-
*/
722726
public function testGetTextAttribute()
723727
{
724-
$formatter = $this->getStubFormatterWithDecimalStyle();
725-
$formatter->getTextAttribute(null);
728+
$this->skipIfIntlExtensionIsNotLoaded();
729+
730+
$intlDecimalFormatter = new \NumberFormatter('en', \NumberFormatter::DECIMAL);
731+
$intlCurrencyFormatter = new \NumberFormatter('en', \NumberFormatter::CURRENCY);
732+
733+
$stubDecimalFormatter = $this->getStubFormatterWithDecimalStyle();
734+
$stubCurrencyFormatter = $this->getStubFormatterWithCurrencyStyle();
735+
736+
for ($i = 0; $i <= 5; $i++) {
737+
$this->assertSame($stubDecimalFormatter->getTextAttribute($i), $intlDecimalFormatter->getTextAttribute($i), $i);
738+
$this->assertSame($stubCurrencyFormatter->getTextAttribute($i), $intlCurrencyFormatter->getTextAttribute($i), 'fooo '.$i);
739+
}
726740
}
727741

728742
/**
@@ -737,11 +751,13 @@ public function testParseCurrency()
737751
/**
738752
* @dataProvider parseProvider
739753
*/
740-
public function testParseStub($value, $expected, $message = '')
754+
public function testParseStub($value, $expected, $message, $expectedPosition)
741755
{
742756
$formatter = $this->getStubFormatterWithDecimalStyle();
743-
$parsedValue = $formatter->parse($value, StubNumberFormatter::TYPE_DOUBLE);
757+
$position = 0;
758+
$parsedValue = $formatter->parse($value, StubNumberFormatter::TYPE_DOUBLE, $position);
744759
$this->assertSame($expected, $parsedValue, $message);
760+
$this->assertSame($expectedPosition, $position, $message);
745761

746762
if ($expected === false) {
747763
$errorCode = StubIntl::U_PARSE_ERROR;
@@ -762,14 +778,16 @@ public function testParseStub($value, $expected, $message = '')
762778
/**
763779
* @dataProvider parseProvider
764780
*/
765-
public function testParseIntl($value, $expected, $message = '')
781+
public function testParseIntl($value, $expected, $message, $expectedPosition)
766782
{
767783
$this->skipIfIntlExtensionIsNotLoaded();
768784
$this->skipIfICUVersionIsTooOld();
769785

770786
$formatter = $this->getIntlFormatterWithDecimalStyle();
771-
$parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE);
787+
$position = 0;
788+
$parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE, $position);
772789
$this->assertSame($expected, $parsedValue, $message);
790+
$this->assertSame($expectedPosition, $position, $message);
773791

774792
if ($expected === false) {
775793
$errorCode = StubIntl::U_PARSE_ERROR;
@@ -790,8 +808,8 @@ public function testParseIntl($value, $expected, $message = '')
790808
public function parseProvider()
791809
{
792810
return array(
793-
array('prefix1', false, '->parse() does not parse a number with a string prefix.'),
794-
array('1suffix', (float) 1, '->parse() parses a number with a string suffix.'),
811+
array('prefix1', false, '->parse() does not parse a number with a string prefix.', 0),
812+
array('1.4suffix', (float) 1.4, '->parse() parses a number with a string suffix.', 3),
795813
);
796814
}
797815

@@ -840,6 +858,7 @@ public function parseTypeInt32Provider()
840858
return array(
841859
array('1', 1),
842860
array('1.1', 1),
861+
array('.1', 0),
843862
array('2,147,483,647', 2147483647),
844863
array('-2,147,483,648', -2147483647 - 1),
845864
array('2,147,483,648', false, '->parse() TYPE_INT32 returns false when the number is greater than the integer positive range.'),
@@ -1104,10 +1123,10 @@ public function testParseTypeCurrencyIntl()
11041123

11051124
public function testParseWithNullPositionValueStub()
11061125
{
1107-
$position = null;
1126+
$position = 0;
11081127
$formatter = $this->getStubFormatterWithDecimalStyle();
11091128
$formatter->parse('123', StubNumberFormatter::TYPE_INT32, $position);
1110-
$this->assertNull($position);
1129+
$this->assertEquals(3, $position);
11111130
}
11121131

11131132
public function testParseWithNullPositionValueIntl()
@@ -1119,14 +1138,12 @@ public function testParseWithNullPositionValueIntl()
11191138
$this->assertEquals(3, $position);
11201139
}
11211140

1122-
/**
1123-
* @expectedException \Symfony\Component\Locale\Exception\MethodArgumentNotImplementedException
1124-
*/
11251141
public function testParseWithNotNullPositionValueStub()
11261142
{
11271143
$position = 1;
11281144
$formatter = $this->getStubFormatterWithDecimalStyle();
11291145
$formatter->parse('123', StubNumberFormatter::TYPE_INT32, $position);
1146+
$this->assertEquals(3, $position);
11301147
}
11311148

11321149
public function testParseWithNotNullPositionValueIntl()

0 commit comments

Comments
 (0)