Currently, every value gets checked if it's a number using is_numeric(). There are some types of values that pass this check depending on the concrete value. ZIP codes for instance: 12345 => number, but 01234 => string. Or color codes: 000000 => number, but ffffff => string.
This makes working with the data really difficult. If JS consumes this data using an API and I want to convert all color codes to lowercase it would look like this:
// assuming that `color` is of type `string | number`
const lowercaseColor = typeof color === 'string'
? color.toLowerCase()
: color.toString().toLowerCase()
If I knew every color was a string I could just do:
const lowercaseColor = color.toLowerCase()
I have a few ideas how to solve this:
1) Add an option whether numbers should be parsed or not
$result = SimpleXLSX::parse('file.xlsx', parseNumbers: false);
This syntax requires PHP 8.0 or later. Users of older versions would need to write:
$result = SimpleXLSX::parse('file.xlsx', false, false, false);
This could be made nicer using an options array because you can skip all options you don't want to change:
$result = SimpleXLSX::parse($data, ['debug' => true, 'is_data' => true, 'parse_numbers' => false]);
$result = SimpleXLSX::parse('file.xlsx', ['parse_numbers' => false]);
I know, the options array makes the library more complex and is only helpful for older PHP versions. So, this is totally optional IMO.
If the option is set to false numbers can still be parsed later using intval() or floatval(). So, it's still pretty flexible.
2) Store the original value in a separate field
$cell['value'] // -> 123
$cell['rawValue'] // -> "123"
Easier to implement I guess, but probably does unnecessary work and needs a bit more memory.
Currently, every value gets checked if it's a number using
is_numeric(). There are some types of values that pass this check depending on the concrete value. ZIP codes for instance:12345=>number, but01234=>string. Or color codes:000000=>number, butffffff=>string.This makes working with the data really difficult. If JS consumes this data using an API and I want to convert all color codes to lowercase it would look like this:
If I knew every color was a string I could just do:
I have a few ideas how to solve this:
1) Add an option whether numbers should be parsed or not
This syntax requires PHP 8.0 or later. Users of older versions would need to write:
This could be made nicer using an options array because you can skip all options you don't want to change:
I know, the options array makes the library more complex and is only helpful for older PHP versions. So, this is totally optional IMO.
If the option is set to
falsenumbers can still be parsed later usingintval()orfloatval(). So, it's still pretty flexible.2) Store the original value in a separate field
Easier to implement I guess, but probably does unnecessary work and needs a bit more memory.