8000 feature #28555 [VarDumper] add caster for NumberFormatter (jschaedl) · symfony/symfony@a43f307 · GitHub
[go: up one dir, main page]

Skip to content

Commit a43f307

Browse files
feature #28555 [VarDumper] add caster for NumberFormatter (jschaedl)
This PR was squashed before being merged into the 4.2-dev branch (closes #28555). Discussion ---------- [VarDumper] add caster for NumberFormatter | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28554 | License | MIT | Doc PR | - Commits ------- 0bd8a0b [VarDumper] add caster for NumberFormatter
2 parents a26bd36 + 0bd8a0b commit a43f307

File tree

3 files changed

+202
-1
lines changed

3 files changed

+202
-1
lines changed

src/Symfony/Component/VarDumper/Caster/IntlCaster.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
/**
1717
* @author Nicolas Grekas <p@tchwork.com>
18+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
1819
*/
1920
class IntlCaster
2021
{
@@ -25,6 +26,88 @@ public static function castMessageFormatter(\MessageFormatter $c, array $a, Stub
2526
Caster::PREFIX_VIRTUAL.'pattern' => $c->getPattern(),
2627
);
2728

29+
return self::castError($c, $a);
30+
}
31+
32+
public static function castNumberFormatter(\NumberFormatter $c, array $a, Stub $stub, $isNested, $filter = 0)
33+
{
34+
$a += array(
35+
Caster::PREFIX_VIRTUAL.'locale' => $c->getLocale(),
36+
Caster::PREFIX_VIRTUAL.'pattern' => $c->getPattern(),
37+
);
38+
39+
if ($filter & Caster::EXCLUDE_VERBOSE) {
40+
$stub->cut += 3;
41+
42+
return self::castError($c, $a);
43+
}
44+
45+
$a += array(
46+
Caster::PREFIX_VIRTUAL.'attributes' => new EnumStub(
47+
array(
48+
'PARSE_INT_ONLY' => $c->getAttribute(\NumberFormatter::PARSE_INT_ONLY),
49+
'GROUPING_USED' => $c->getAttribute(\NumberFormatter::GROUPING_USED),
50+
'DECIMAL_ALWAYS_SHOWN' => $c->getAttribute(\NumberFormatter::DECIMAL_ALWAYS_SHOWN),
51+
'MAX_INTEGER_DIGITS' => $c->getAttribute(\NumberFormatter::MAX_INTEGER_DIGITS),
52+
'MIN_INTEGER_DIGITS' => $c->getAttribute(\NumberFormatter::MIN_INTEGER_DIGITS),
53+
'INTEGER_DIGITS' => $c->getAttribute(\NumberFormatter::INTEGER_DIGITS),
54+
'MAX_FRACTION_DIGITS' => $c->getAttribute(\NumberFormatter::MAX_FRACTION_DIGITS),
55+
'MIN_FRACTION_DIGITS' => $c->getAttribute(\NumberFormatter::MIN_FRACTION_DIGITS),
56+
'FRACTION_DIGITS' => $c->getAttribute(\NumberFormatter::FRACTION_DIGITS),
57+
'MULTIPLIER' => $c->getAttribute(\NumberFormatter::MULTIPLIER),
58+
'GROUPING_SIZE' => $c->getAttribute(\NumberFormatter::GROUPING_SIZE),
59+
'ROUNDING_MODE' => $c->getAttribute(\NumberFormatter::ROUNDING_MODE),
60+
'ROUNDING_INCREMENT' => $c->getAttribute(\NumberFormatter::ROUNDING_INCREMENT),
61+
'FORMAT_WIDTH' => $c->getAttribute(\NumberFormatter::FORMAT_WIDTH),
62+
'PADDING_POSITION' => $c->getAttribute(\NumberFormatter::PADDING_POSITION),
63+
'SECONDARY_GROUPING_SIZE' => $c->getAttribute(\NumberFormatter::SECONDARY_GROUPING_SIZE),
64+
'SIGNIFICANT_DIGITS_USED' => $c->getAttribute(\NumberFormatter::SIGNIFICANT_DIGITS_USED),
65+
'MIN_SIGNIFICANT_DIGITS' => $c->getAttribute(\NumberFormatter::MIN_SIGNIFICANT_DIGITS),
66+
'MAX_SIGNIFICANT_DIGITS' => $c->getAttribute(\NumberFormatter::MAX_SIGNIFICANT_DIGITS),
67+
'LENIENT_PARSE' => $c->getAttribute(\NumberFormatter::LENIENT_PARSE),
68+
)
69+
),
70+
Caster::PREFIX_VIRTUAL.'text_attributes' => new EnumStub(
71+
array(
72+
'POSITIVE_PREFIX' => $c->getTextAttribute(\NumberFormatter::POSITIVE_PREFIX),
73+
'POSITIVE_SUFFIX' => $c->getTextAttribute(\NumberFormatter::POSITIVE_SUFFIX),
74+
'NEGATIVE_PREFIX' => $c->getTextAttribute(\NumberFormatter::NEGATIVE_PREFIX),
75+
'NEGATIVE_SUFFIX' => $c->getTextAttribute(\NumberFormatter::NEGATIVE_SUFFIX),
76+
'PADDING_CHARACTER' => $c->getTextAttribute(\NumberFormatter::PADDING_CHARACTER),
77+
'CURRENCY_CODE' => $c->getTextAttribute(\NumberFormatter::CURRENCY_CODE),
78+
'DEFAULT_RULESET' => $c->getTextAttribute(\NumberFormatter::DEFAULT_RULESET),
79+
'PUBLIC_RULESETS' => $c->getTextAttribute(\NumberFormatter::PUBLIC_RULESETS),
80+
)
81+
),
82+
Caster::PREFIX_VIRTUAL.'symbols' => new EnumStub(
83+
array(
84+
'DECIMAL_SEPARATOR_SYMBOL' => $c->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL),
85+
'GROUPING_SEPARATOR_SYMBOL' => $c->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL),
86+
'PATTERN_SEPARATOR_SYMBOL' => $c->getSymbol(\NumberFormatter::PATTERN_SEPARATOR_SYMBOL),
87+
'PERCENT_SYMBOL' => $c->getSymbol(\NumberFormatter::PERCENT_SYMBOL),
88+
'ZERO_DIGIT_SYMBOL' => $c->getSymbol(\NumberFormatter::ZERO_DIGIT_SYMBOL),
89+
'DIGIT_SYMBOL' => $c->getSymbol(\NumberFormatter::DIGIT_SYMBOL),
90+
'MINUS_SIGN_SYMBOL' => $c->getSymbol(\NumberFormatter::MINUS_SIGN_SYMBOL),
91+
'PLUS_SIGN_SYMBOL' => $c->getSymbol(\NumberFormatter::PLUS_SIGN_SYMBOL),
92+
'CURRENCY_SYMBOL' => $c->getSymbol(\NumberFormatter::CURRENCY_SYMBOL),
93+
'INTL_CURRENCY_SYMBOL' => $c->getSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL),
94+
'MONETARY_SEPARATOR_SYMBOL' => $c->getSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL),
95+
'EXPONENTIAL_SYMBOL' => $c->getSymbol(\NumberFormatter::EXPONENTIAL_SYMBOL),
96+
'PERMILL_SYMBOL' => $c->getSymbol(\NumberFormatter::PERMILL_SYMBOL),
97+
'PAD_ESCAPE_SYMBOL' => $c->getSymbol(\NumberFormatter::PAD_ESCAPE_SYMBOL),
98+
'INFINITY_SYMBOL' => $c->getSymbol(\NumberFormatter::INFINITY_SYMBOL),
99+
'NAN_SYMBOL' => $c->getSymbol(\NumberFormatter::NAN_SYMBOL),
100+
'SIGNIFICANT_DIGIT_SYMBOL' => $c->getSymbol(\NumberFormatter::SIGNIFICANT_DIGIT_SYMBOL),
101+
'MONETARY_GROUPING_SEPARATOR_SYMBOL' => $c->getSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL),
102+
)
103+
),
104+
);
105+
106+
return self::castError($c, $a);
107+
}
108+
109+
private static function castError($c, array $a): array
110+
{
28111
if ($errorCode = $c->getErrorCode()) {
29112
$a += array(
30113
Caster::PREFIX_VIRTUAL.'error_code' => $errorCode,

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ abstract class AbstractCloner implements ClonerInterface
118118
'GMP' => array('Symfony\Component\VarDumper\Caster\GmpCaster', 'castGmp'),
119119

120120
'MessageFormatter' => array('Symfony\Component\VarDumper\Caster\IntlCaster', 'castMessageFormatter'),
121+
'NumberFormatter' => array('Symfony\Component\VarDumper\Caster\IntlCaster', 'castNumberFormatter'),
121122

122123
':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
123124
':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),

src/Symfony/Component/VarDumper/Tests/Caster/IntlCasterTest.php

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class IntlCasterTest extends TestCase
2121
{
2222
use VarDumperTestTrait;
2323

24-
public function testArrayIterator()
24+
public function testMessageFormatter()
2525
{
2626
$var = new \MessageFormatter('en', 'Hello {name}');
2727

@@ -30,6 +30,123 @@ public function testArrayIterator()
3030
locale: "en"
3131
pattern: "Hello {name}"
3232
}
33+
EOTXT;
34+
$this->assertDumpEquals($expected, $var);
35+
}
36+
37+
public function testCastNumberFormatter()
38+
{
39+
$var = new \NumberFormatter('en', \NumberFormatter::DECIMAL);
40+
41+
$expectedLocale = $var->getLocale();
42+
$expectedPattern = $var->getPattern();
43+
44+
$expectedAttribute1 = $var->getAttribute(\NumberFormatter::PARSE_INT_ONLY);
45+
$expectedAttribute2 = $var->getAttribute(\NumberFormatter::GROUPING_USED);
46+
$expectedAttribute3 = $var->getAttribute(\NumberFormatter::DECIMAL_ALWAYS_SHOWN);
47+
$expectedAttribute4 = $var->getAttribute(\NumberFormatter::MAX_INTEGER_DIGITS);
48+
$expectedAttribute5 = $var->getAttribute(\NumberFormatter::MIN_INTEGER_DIGITS);
49+
$expectedAttribute6 = $var->getAttribute(\NumberFormatter::INTEGER_DIGITS);
50+
$expectedAttribute7 = $var->getAttribute(\NumberFormatter::MAX_FRACTION_DIGITS);
51+
$expectedAttribute8 = $var->getAttribute(\NumberFormatter::MIN_FRACTION_DIGITS);
52+
$expectedAttribute9 = $var->getAttribute(\NumberFormatter::FRACTION_DIGITS);
53+
$expectedAttribute10 = $var->getAttribute(\NumberFormatter::MULTIPLIER);
54+
$expectedAttribute11 = $var->getAttribute(\NumberFormatter::GROUPING_SIZE);
55+
$expectedAttribute12 = $var->getAttribute(\NumberFormatter::ROUNDING_MODE);
56+
$expectedAttribute13 = number_format($var->getAttribute(\NumberFormatter::ROUNDING_INCREMENT), 1);
57+
$expectedAttribute14 = $var->getAttribute(\NumberFormatter::FORMAT_WIDTH);
58+
$expectedAttribute15 = $var->getAttribute(\NumberFormatter::PADDING_POSITION);
59+
$expectedAttribute16 = $var->getAttribute(\NumberFormatter::SECONDARY_GROUPING_SIZE);
60+
$expectedAttribute17 = $var->getAttribute(\NumberFormatter::SIGNIFICANT_DIGITS_USED);
61+
$expectedAttribute18 = $var->getAttribute(\NumberFormatter::MIN_SIGNIFICANT_DIGITS);
62+
$expectedAttribute19 = $var->getAttribute(\NumberFormatter::MAX_SIGNIFICANT_DIGITS);
63+
$expectedAttribute20 = $var->getAttribute(\NumberFormatter::LENIENT_PARSE);
64+
65+
$expectedTextAttribute1 = $var->getTextAttribute(\NumberFormatter::POSITIVE_PREFIX);
66+
$expectedTextAttribute2 = $var->getTextAttribute(\NumberFormatter::POSITIVE_SUFFIX);
67+
$expectedTextAttribute3 = $var->getTextAttribute(\NumberFormatter::NEGATIVE_PREFIX);
68+
$expectedTextAttribute4 = $var->getTextAttribute(\NumberFormatter::NEGATIVE_SUFFIX);
69+
$expectedTextAttribute5 = $var->getTextAttribute(\NumberFormatter::PADDING_CHARACTER);
70+
$expectedTextAttribute6 = $var->getTextAttribute(\NumberFormatter::CURRENCY_CODE);
71+
$expectedTextAttribute7 = $var->getTextAttribute(\NumberFormatter::DEFAULT_RULESET) ? 'true' : 'false';
72+
$expectedTextAttribute8 = $var->getTextAttribute(\NumberFormatter::PUBLIC_RULESETS) ? 'true' : 'false';
73+
74+
$expectedSymbol1 = $var->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
75+
$expectedSymbol2 = $var->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
76+
$expectedSymbol3 = $var->getSymbol(\NumberFormatter::PATTERN_SEPARATOR_SYMBOL);
77+
$expectedSymbol4 = $var->getSymbol(\NumberFormatter::PERCENT_SYMBOL);
78+
$expectedSymbol5 = $var->getSymbol(\NumberFormatter::ZERO_DIGIT_SYMBOL);
79+
$expectedSymbol6 = $var->getSymbol(\NumberFormatter::DIGIT_SYMBOL);
80+
$expectedSymbol7 = $var->getSymbol(\NumberFormatter::MINUS_SIGN_SYMBOL);
81+
$expectedSymbol8 = $var->getSymbol(\NumberFormatter::PLUS_SIGN_SYMBOL);
82+
$expectedSymbol9 = $var->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
83+
$expectedSymbol10 = $var->getSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL);
84+
$expectedSymbol11 = $var->getSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL);
85+
$expectedSymbol12 = $var->getSymbol(\NumberFormatter::EXPONENTIAL_SYMBOL);
86+
$expectedSymbol13 = $var->getSymbol(\NumberFormatter::PERMILL_SYMBOL);
87+
$expectedSymbol14 = $var->getSymbol(\NumberFormatter::PAD_ESCAPE_SYMBOL);
88+
$expectedSymbol15 = $var->getSymbol(\NumberFormatter::INFINITY_SYMBOL);
89+
$expectedSymbol16 = $var->getSymbol(\NumberFormatter::NAN_SYMBOL);
90+
$expectedSymbol17 = $var->getSymbol(\NumberFormatter::SIGNIFICANT_DIGIT_SYMBOL);
91+
$expectedSymbol18 = $var->getSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL);
92+
93+
$expected = <<<EOTXT
94+
NumberFormatter {
95+
locale: "$expectedLocale"
96+
pattern: "$expectedPattern"
97+
attributes: {
98+
PARSE_INT_ONLY: $expectedAttribute1
99+
GROUPING_USED: $expectedAttribute2
100+
DECIMAL_ALWAYS_SHOWN: $expectedAttribute3
101+
MAX_INTEGER_DIGITS: $expectedAttribute4
102+
MIN_INTEGER_DIGITS: $expectedAttribute5
103+
INTEGER_DIGITS: $expectedAttribute6
104+
MAX_FRACTION_DIGITS: $expectedAttribute7
105+
MIN_FRACTION_DIGITS: $expectedAttribute8
106+
FRACTION_DIGITS: $expectedAttribute9
107+
MULTIPLIER: $expectedAttribute10
108+
GROUPING_SIZE: $expectedAttribute11
109+
ROUNDING_MODE: $expectedAttribute12
110+
ROUNDING_INCREMENT: $expectedAttribute13
111+
FORMAT_WIDTH: $expectedAttribute14
112+
PADDING_POSITION: $expectedAttribute15
113+
SECONDARY_GROUPING_SIZE: $expectedAttribute16
114+
SIGNIFICANT_DIGITS_USED: $expectedAttribute17
115+
MIN_SIGNIFICANT_DIGITS: $expectedAttribute18
116+
MAX_SIGNIFICANT_DIGITS: $expectedAttribute19
117+
LENIENT_PARSE: $expectedAttribute20
118+
}
119+
text_attributes: {
120+
POSITIVE_PREFIX: "$expectedTextAttribute1"
121+
POSITIVE_SUFFIX: "$expectedTextAttribute2"
122+
NEGATIVE_PREFIX: "$expectedTextAttribute3"
123+
NEGATIVE_SUFFIX: "$expectedTextAttribute4"
124+
PADDING_CHARACTER: "$expectedTextAttribute5"
125+
CURRENCY_CODE: "$expectedTextAttribute6"
126+
DEFAULT_RULESET: $expectedTextAttribute7
127+
PUBLIC_RULESETS: $expectedTextAttribute8
128+
}
129+
symbols: {
130+
DECIMAL_SEPARATOR_SYMBOL: "$expectedSymbol1"
131+
GROUPING_SEPARATOR_SYMBOL: "$expectedSymbol2"
132+
PATTERN_SEPARATOR_SYMBOL: "$expectedSymbol3"
133+
PERCENT_SYMBOL: "$expectedSymbol4"
134+
ZERO_DIGIT_SYMBOL: "$expectedSymbol5"
135+
DIGIT_SYMBOL: "$expectedSymbol6"
136+
MINUS_SIGN_SYMBOL: "$expectedSymbol7"
137+
PLUS_SIGN_SYMBOL: "$expectedSymbol8"
138+
CURRENCY_SYMBOL: "$expectedSymbol9"
139+
INTL_CURRENCY_SYMBOL: "$expectedSymbol10"
140+
MONETARY_SEPARATOR_SYMBOL: "$expectedSymbol11"
141+
EXPONENTIAL_SYMBOL: "$expectedSymbol12"
142+
PERMILL_SYMBOL: "$expectedSymbol13"
143+
PAD_ESCAPE_SYMBOL: "$expectedSymbol14"
144+
INFINITY_SYMBOL: "$expectedSymbol15"
145+
NAN_SYMBOL: "$expectedSymbol16"
146+
SIGNIFICANT_DIGIT_SYMBOL: "$expectedSymbol17"
147+
MONETARY_GROUPING_SEPARATOR_SYMBOL: "$expectedSymbol18"
148+
}
149+
}
33150
EOTXT;
34151
$this->assertDumpEquals($expected, $var);
35152
}

0 commit comments

Comments
 (0)
0