-
Notifications
You must be signed in to change notification settings - Fork 440
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
parse a string into a money object #148
Conversation
Hmm, why is this failing on Travis while tests are succeeding on my machine? |
9ad3ac0
to
3b53d5d
Compare
Could it be that the locale is not available on the Travis setup? This gives the same (unclear) message: https://3v4l.org/1p9eF |
Travis only has en_US installed. The funny thing is that I didn't have this issue with/without nl_NL installed, I had totally different issues. I would rewrite the test examples to use en_US locale, it is probably commonly installed for others anyway. I would also skip tests on machines where en_US locale is not available. |
@sagikazarmark Right, we should do that. But I doubt if people will have same results when using the classes. That might lead to too many unexpected results. |
What might lead to ...? I think I missed an important part: we should try to set the locale to en_US at the beginning of the test. If it fails, skip. |
@sagikazarmark I agree with that. But I do not get why your results with |
I didn't pay too much attention, maybe something else was wrong. It is PHP after all, stability is not one of the keywords. Also, intl is especially something vague. |
Hmm, I don't have a lot of experience here, but if results are unreliable, I'm not sure I'd trust this to handly my money stuff. |
I think we could preserve the "original" parsing logic in a separate parser, which was less smart, but more stable. |
@barryvdh Do not get this wrong: @sagikazarmark We should look into this more before drawing conclusions. |
Agree, I didn't want to give up on intl extension, but for simpler cases, the previous logic could be useful as well, like SimpleParser. |
@sagikazarmark The PR already contains the original parser. |
👍 |
Yes I understand Money is stable, I was just referring to intl parsing by itself ;) |
Okay, but I guess this way you are able to stick to the "default/previous" strategy or come up with a better, since we have an interface for that and no pressure on which to use. |
@sagikazarmark Why don't we inject This we place behaviour of the |
I remember I had some issues with NumberFormatter itself, and this injection approach, but I can't see any better solution. Although, it would be nice to look around and find some usage examples. My biggest concern is the required configuration in of the NumberFormatter. It needs some basic config to work well (which is not necessarily configurable later). |
Yes, if But, now the intl parser and formatter implementations are instantiating the |
Okay. When constructing an $formatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
$config = [ .. ];
var_dump($config); Travis:
My Ubuntu (14.04) machine:
What is causing the default pattern to be different? |
So, now I got a green light. $formatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
$formatter->setPattern("¤#,##0.00;-¤#,##0.00");
$parser = new IntlMoneyParser($formatter);
$this->assertEquals($units, $parser->parse($string, 'USD')->getAmount()); Only setting a string with double quotes which include a pattern for positive and negative amounts via the setter (not via constructor) will give a stable result. |
@sagikazarmark So we now have two parsers.
|
Parse a string into a money object, fixes #98
Fixes #98 and provides a possible solution for #97.