Skip to content

Conversation

jdufresne
Copy link
Contributor

Now, string values are always treated as TYPE_STRING. To store a numeric value using the type auto-detection logic, one should a numeric type.

Fixes #752

This is a new feature

Checklist:

Why this change is needed?

See issue #752

Now, string values are always treated as TYPE_STRING. To store a numeric
value using the type auto-detection logic, one should a numeric type.

There are many use cases where strings that look like numbers should be
treated as strings: telephone numbers, employee ID numbers, purchase
order numbers, etc.

Fixes #752
@@ -58,15 +58,6 @@ public static function dataTypeForValue($pValue)
} elseif (is_bool($pValue)) {
return DataType::TYPE_BOOL;
} elseif (is_float($pValue) || is_int($pValue)) {
return DataType::TYPE_NUMERIC;
} elseif (preg_match('/^[\+\-]?(\d+\\.?\d*|\d*\\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe instead of removing it, this should be moved to AdvancedValueBinder.

@PowerKiKi PowerKiKi requested a review from MarkBaker November 11, 2018 07:36
@PowerKiKi PowerKiKi added this to the 2.0 milestone Nov 11, 2018
@MarkBaker
Copy link
Member

MarkBaker commented Nov 12, 2018

I really am not comfortable with this change of behaviour.... we created the concept of binders to allow advanced users to control datatyping, while providing the default as something that simplified datatype handling for less advance users.... and I spend more than enough time explaining to people that '2018-11-12 is not a date, but a string representing a date... if we force all datatypes to be string by default, then it will simply increase that level of difficulty for people who aren't as understanding of datatypes,, and there are a lot of user out there that simply don't understand PHP or Excel datatypes, and who simply trust that simplicity to do the work for them.

@MarkBaker
Copy link
Member

The logic being removed here assesses the value of strings containing numeric values to see if they begin with a leading zero; and if so, treats them as strings.... the "non-greedy" evaluation of numeric values is supposed to handle telephone numbers, social security numbers, etc such as '0123456' as strings rather than setting them as a numeric type.
It was deliberately adopted as a compromise behaviour; to satisfy those who wanted to retain string datatyping for certain types of numeric values, while retaining the simplicity of automatic typing for those who wanted ease of use.

@jdufresne
Copy link
Contributor Author

if we force all datatypes to be string by default ...

That is not what I'm suggesting. I'm suggesting that string types be treated as strings. Integers, floating point numbers, date objects, etc. should still be stored in the document as the more obvious type.

@MarkBaker
Copy link
Member

OK, my bad.... but that does not change my opinion on this issue. That vast majority of PHP developers using this library do not fully understand datatyping and the differences between 123 and '123'. They take data from an on-screen form or from a csv file, which comes as a string (I've even seen databases that use varchar for everything) and expect it to work as a number. That is the reason for the default. For the more advanced user, who does understand those differences, then it is a single line of code to switch to using a different value binder.
I'm more than happy to include a new binder in the library that gives you string numerics as strings, but not as the default behaviour.

@PowerKiKi
Copy link
Member

Mark opinion seems to join my own.

@jdufresne if you are willing to modify this PR, or create a new one, to have a new StrictTypesBinder (or similar naming), which would be available for everyone, but not the default one, then please do so.

If you'd rather not invest more time in this, then we'll close as WONTFIX, because Mark and myself probably won't have much time to implement it ourselves.

@jdufresne
Copy link
Contributor Author

Thanks for taking the time to respond. I won't have the time to continue working on this so I'll close this PR for now. Cheers.

@jdufresne jdufresne closed this Dec 17, 2018
@jdufresne jdufresne deleted the strings-are-strings branch January 3, 2019 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants