Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Fix for #6300 - I18n float validator rewrite #6373

Closed
wants to merge 8 commits into from
Closed

Fix for #6300 - I18n float validator rewrite #6373

wants to merge 8 commits into from

Conversation

moderndeveloperllc
Copy link
Contributor

Fix for #6300. This is a complete rewrite of the validator to account for non-latin number systems, scientific notation, and "look-alike" characters (space for NO-BREAK SPACE, comma for ARABIC DECIMAL SEPARATOR, and single quote for ARABIC THOUSANDS SEPARATOR). Though the test class only tests against 13 locales, the logic was tested against all 709 locales in CLDR 25.

This also includes a small cleanup of the formatting on NumberFormat filter and removal of the extraneous NO-BREAK SPACE removal.

Rewrite of the test class to use programmatic values for true tests using both decimal and scientific notation.

…bers, exponential notation, and a few "look-alike" characters (space for NO_BREAK SPACE, comma for ARABIC DECIMAL SEPARATOR, and single quote for ARABIC THOUSANDS SEPARATOR).
…rmatters; Move to prefix/suffix logic due to locales like mr; Remove LRM from string and symbols
{
$valid = new FloatValidator(array('locale' => 'en_US'));
$this->assertEquals($expected, $valid->isValid($float));
Locale::setDefault('de');
Copy link
Member

Choose a reason for hiding this comment

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

Make sure to reset the locale to what it was before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The teardown() for the class resets the locale to whatever it was coming in.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, nevermind then.

'(' . $suffix . '(' . $lnum . $prefix . $exp . '(' . $dnum . '|' . $lnum . '))' . $prefix . '))';

//LEFT-TO-RIGHT MARK (U+200E) is messing up everything for the handful of locales that have it
$lnumSearch = str_replace("\xE2\x80\x8E", '', '/^' .$prefix . $lnum . $suffix . '$/'.$useUnicode);
Copy link
Member

Choose a reason for hiding this comment

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

Either use ( and ) as regex delimiters instead of /, or add the / as second parameter to all preg_quote() calls.

Copy link
Member

Choose a reason for hiding this comment

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

@DASPRiD umm... this is a str_replace, not preg_quote...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@weierophinney @DASPRiD was referring to lines 169, 178, and 186. I added the slash as suggested. Not sure why his comment still shows. Github quirk.

@DASPRiD
Copy link
Member

DASPRiD commented Jun 16, 2014

Sadly we have disabled coveralls for pull requests because of the noise. Would you mind exporting a coverage report for the validator? I just want to make sure that this is properly covered.

@moderndeveloperllc
Copy link
Contributor Author

@DASPRiD gotta admit my ignorance here. What software were you looking for me to use for the coverage report? I can say I tested this with all the locales from ICU 53 and it passed. I know that's not what you meant, but just FYI for the reviewers.

@DASPRiD
Copy link
Member

DASPRiD commented Jul 7, 2014

@moderndeveloperllc Just run phpunit --coverage-html=/some/path

{
$trueArray = array();
$testingLocales = array('ar', 'bn', 'de', 'dz', 'en', 'fr-CH', 'ja', 'ks', 'ml-IN', 'mr', 'my', 'ps', 'ru');
$testingExamples = array(1000, -2000, +398.00, 0.04, -0.5, .6, -.70, 8E10, -9.3456E-2, 10.23E6);
Copy link
Contributor

Choose a reason for hiding this comment

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

I have a few of suggestions that can be added to this test -

  • High Precision float values such as 123.1234567890987654321
  • Testing strings e.g. "123.1234567890987654321" (had a problem with this recently not passing the validator)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rahuldroy Eh, why not. Note: This will pass as decimal because we now only check that the input is in the form of a float and don't try and do any value equivalence checking like the old validator. The old validator was doing some processing of the number and then checking that the value did not change. This ran into the issue you saw - PHP and it's "lax" handling of floats that have a large number of significant digits.

Copy link
Contributor

Choose a reason for hiding this comment

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

@moderndeveloperllc cool thanks 👍 . You are right about PHPs handling of floats and I believe that is something that needs to be addressed upstream urgently. I was rewriting that part myself (only to find out that you were already working on it)

…idator, but this validator correctly passes it for all locales.
@moderndeveloperllc
Copy link
Contributor Author

@DASPRiD I put up the HTML at https://gist.github.com/moderndeveloperllc/3b63cd47dfd7c576e2bd#file-floatcoverage

It seems to be rather well covered with the exception of a few of the standard exceptions .

@ezimuel ezimuel added this to the 2.3.2 milestone Aug 6, 2014
@ezimuel
Copy link
Contributor

ezimuel commented Aug 6, 2014

@DASPRiD is this PR ready to be merged?

weierophinney added a commit that referenced this pull request Aug 7, 2014
- Concatenators should be on following line
- `||`/`&&` should be on following line
- Spaces around `.` concatenation
- Trailing whitespace
weierophinney added a commit that referenced this pull request Aug 7, 2014
@weierophinney
Copy link
Member

Cherry-picked to master for release with 2.3.2; merged to both master and develop.

}

if (strval($parsedFloat) !== $valueFiltered) {
$this->error(self::NOT_FLOAT);
Copy link
Member

Choose a reason for hiding this comment

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

According to @NickBelhomme, this change is causing trouble, as this error message is not being displayed anymore when upgrading to 2.3.3 from any previous version.

Copy link
Member

Choose a reason for hiding this comment

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

See #6647

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Ocramius Did you mean to put this message on this PR? I know there are a few others out there talking about NOT_FLOAT. Personally, I never saw the utility of the error message as this is a validator - if the method returns false the input is, by definition, not a float. I kept in the error messages that were caused my bad inputs or instantiations. But if y'all want to include errors like this, others are welcome to include them.

Copy link
Member

Choose a reason for hiding this comment

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

@moderndeveloperllc it can be changed, but only as a BC break in a point release

weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
- Concatenators should be on following line
- `||`/`&&` should be on following line
- Spaces around `.` concatenation
- Trailing whitespace
weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-i18n that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants