Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[VarDumper] add caster for NumberFormatter #28555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

jschaedl
Copy link
Contributor

@jschaedl jschaedl commented Sep 22, 2018

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 -

@nicolas-grekas
Copy link
Member

Thanks for working on this.
Would you mind rebasing to get rid of the merge commit please?
It could be interesting to also dump the attributes, text-attributes and symbol. WDYT?

@jschaedl jschaedl force-pushed the jschaedl/vd-caster-number_formatter branch 2 times, most recently from 7190720 to 33a3971 Compare September 22, 2018 18:21
@jschaedl
Copy link
Contributor Author

@nicolas-grekas

Would you mind rebasing to get rid of the merge commit please?

done

It could be interesting to also dump the attributes, text-attributes and symbol. WDYT?

I like the idea. The first possible solution which came to my mind was to show all possible values of the attributes, text-attributes and symbol as an array:

NumberFormatter {
  locale: "en"
  pattern: "#,##0.###"
  attributes: array:20 [
    "PARSE_INT_ONLY" => "."
    "GROUPING_USED" => ","
    "DECIMAL_ALWAYS_SHOWN" => ";"
    "MAX_INTEGER_DIGITS" => "%"
    "MIN_INTEGER_DIGITS" => "0"
    "INTEGER_DIGITS" => "#"
    "MAX_FRACTION_DIGITS" => "-"
    "MIN_FRACTION_DIGITS" => "+"
    "FRACTION_DIGITS" => "¤"
    "MULTIPLIER" => "¤¤"
    "GROUPING_SIZE" => "."
    "ROUNDING_MODE" => "E"
    "ROUNDING_INCREMENT" => "‰"
    "FORMAT_WIDTH" => "*"
    "PADDING_POSITION" => "∞"
    "SECONDARY_GROUPING_SIZE" => "NaN"
    "SIGNIFICANT_DIGITS_USED" => "@"
    "MIN_SIGNIFICANT_DIGITS" => ","
    "MAX_SIGNIFICANT_DIGITS" => "1"
    "LENIENT_PARSE" => "2"
  ]
  text_attributes: array:8 [
    "POSITIVE_PREFIX" => ""
    "POSITIVE_SUFFIX" => ""
    "NEGATIVE_PREFIX" => "-"
    "NEGATIVE_SUFFIX" => ""
    "PADDING_CHARACTER" => " "
    "CURRENCY_CODE" => ""
    "DEFAULT_RULESET" => false
    "PUBLIC_RULESETS" => false
  ]
  symbols: array:18 [
    "DECIMAL_SEPARATOR_SYMBOL" => "."
    "GROUPING_SEPARATOR_SYMBOL" => ","
    "PATTERN_SEPARATOR_SYMBOL" => ";"
    "PERCENT_SYMBOL" => "%"
    "ZERO_DIGIT_SYMBOL" => "0"
    "DIGIT_SYMBOL" => "#"
    "MINUS_SIGN_SYMBOL" => "-"
    "PLUS_SIGN_SYMBOL" => "+"
    "CURRENCY_SYMBOL" => "¤"
    "INTL_CURRENCY_SYMBOL" => "¤¤"
    "MONETARY_SEPARATOR_SYMBOL" => "."
    "EXPONENTIAL_SYMBOL" => "E"
    "PERMILL_SYMBOL" => "‰"
    "PAD_ESCAPE_SYMBOL" => "*"
    "INFINITY_SYMBOL" => "∞"
    "NAN_SYMBOL" => "NaN"
    "SIGNIFICANT_DIGIT_SYMBOL" => "@"
    "MONETARY_GROUPING_SEPARATOR_SYMBOL" => ","
  ]

WDYT?

@nicolas-grekas
Copy link
Member

Wow, I didn't expect so many! LGTM yes.
I'd suggest wrapping the maps in an EnumStub to make them nicer.

@jschaedl jschaedl force-pushed the jschaedl/vd-caster-number_formatter branch 4 times, most recently from 2d26d30 to f87589b Compare September 22, 2018 22:35
Caster::PREFIX_VIRTUAL.'pattern' => $c->getPattern(),
Caster::PREFIX_VIRTUAL.'attributes' => new EnumStub(
array(
'PARSE_INT_ONLY' => $c->getSymbol(\NumberFormatter::PARSE_INT_ONLY),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this use getAttribute instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it should. It is fixed now.

$a += array(
Caster::PREFIX_VIRTUAL.'locale' => $c->getLocale(),
Caster::PREFIX_VIRTUAL.'pattern' => $c->getPattern(),
Caster::PREFIX_VIRTUAL.'attributes' => new EnumStub(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before adding this key to the array, I'd suggest adding its last , $filter = 0 argument to the method and using it in a block like:

if ($filter & Caster::EXCLUDE_VERBOSE) {
    $stub->cut += 3;

    return $a;
}

),
);

if ($errorCode = $c->getErrorCode()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this "if" is going to be repeated for each caster method, let's move it in a dedicated private static method and replace all return $a by return self::thisNewMethod($a);

@jschaedl jschaedl force-pushed the jschaedl/vd-caster-number_formatter branch from b34fe65 to 4cf7feb Compare September 23, 2018 09:55
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome thanks! (just one minor comment remaining)

return self::dumpError($c, $a);
}

private static function dumpError($c, array $a): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's call it castError?

@nicolas-grekas nicolas-grekas force-pushed the jschaedl/vd-caster-number_formatter branch from acf7dce to 0bd8a0b Compare September 23, 2018 10:41
@nicolas-grekas
Copy link
Member

Thank you @jschaedl.

@nicolas-grekas nicolas-grekas merged commit 0bd8a0b into symfony:master Sep 23, 2018
nicolas-grekas added a commit that referenced this pull request Sep 23, 2018
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
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
@jschaedl jschaedl deleted the jschaedl/vd-caster-number_formatter branch February 23, 2020 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants