Provides a simple and flexible solution for managing environment variables, with easy access, type conversions, and validation handling.
composer require tiny-blocks/environment-variable
To create and work with environment variables, use the from
method to get an instance of the environment variable.
EnvironmentVariable::from(name: 'MY_VAR');
To retrieve an environment variable with the option of providing a default value in case the variable does not exist,
use the fromOrDefault
method.
If the environment variable is not found, the method will return the provided default value instead of throwing an exception.
EnvironmentVariable::fromOrDefault(name: 'MY_VAR', defaultValueIfNotFound: 'default_value');
Once you have an instance of the environment variable, you can convert its value into various types.
To convert the environment variable to a string.
$environmentVariable = EnvironmentVariable::from(name: 'MY_VAR');
$environmentVariable->toString();
To convert the environment variable to an integer.
$environmentVariable = EnvironmentVariable::from(name: 'MY_VAR');
$environmentVariable->toInteger();
To convert the environment variable to a boolean.
$environmentVariable = EnvironmentVariable::from(name: 'MY_VAR');
$environmentVariable->toBoolean();
Checks if the environment variable has a value. Values like false
, 0
, and -1
are valid and non-empty.
$environmentVariable = EnvironmentVariable::from(name: 'MY_VAR');
$environmentVariable->hasValue();
Environment variable is licensed under MIT.
Please follow the contributing guidelines to contribute to the project.