Crell / EnvMapper

Easily map environment variables into defined classed objects, ready for Dependency Injection.
GNU Lesser General Public License v3.0
83 stars 3 forks source link

Don't automatically typecast numeric strings #3

Closed therealgaxbo closed 1 year ago

therealgaxbo commented 1 year ago

Strings like telephone numbers are numeric but should not be modelled as integers. If there is a leading zero this will even cause data corruption.

Probably should only cast as needed to match the property type.

kAlvaro commented 1 year ago

To reproduce:

POSTAL_CODE='09001'
class Example
{
    public readonly string $postalCode;
}
$example = $mapper->map(Example::class);

Fatal error: Uncaught Crell\EnvMapper\TypeMismatch: Could not read environment variable for "postalCode" on Some\Namespace\Example. A string was expected but int provided. in C:...\vendor\crell\envmapper\src\TypeMismatch.php:15

Crell commented 1 year ago

Fixed in master. The new approach is very slightly slower, but still acceptable, especially as it's more robust. Thanks!